public ConfigHandler::buildForm(array $form, FormStateInterface $form_state, Request $request = NULL)
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- core/modules/views_ui/src/Form/Ajax/ConfigHandler.php, line 50
Class
- ConfigHandler
- Provides a form for configuring an item in the Views UI.
Namespace
Drupal\views_ui\Form\Ajax
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | public function buildForm( array $form , FormStateInterface $form_state , Request $request = NULL) { /** @var \Drupal\views\Entity\View $view */ $view = $form_state ->get( 'view' ); $display_id = $form_state ->get( 'display_id' ); $type = $form_state ->get( 'type' ); $id = $form_state ->get( 'id' ); $form = array ( 'options' => array ( '#tree' => TRUE, '#theme_wrappers' => array ( 'container' ), '#attributes' => array ( 'class' => array ( 'scroll' ), 'data-drupal-views-scroll' => TRUE), ), ); $executable = $view ->getExecutable(); $save_ui_cache = FALSE; if (! $executable ->setDisplay( $display_id )) { $form [ 'markup' ] = array ( '#markup' => $this ->t( 'Invalid display id @display' , array ( '@display' => $display_id ))); return $form ; } $item = $executable ->getHandler( $display_id , $type , $id ); if ( $item ) { $handler = $executable ->display_handler->getHandler( $type , $id ); if ( empty ( $handler )) { $form [ 'markup' ] = array ( '#markup' => $this ->t( "Error: handler for @table > @field doesn't exist!" , array ('@table ' => $item[' table '], ' @field ' => $item[' field']))); } else { $types = ViewExecutable::getHandlerTypes(); // If this item can come from the default display, show a dropdown // that lets the user choose which display the changes should apply to. if ( $executable ->display_handler->defaultableSections( $types [ $type ][ 'plural' ])) { $section = $types [ $type ][ 'plural' ]; $form_state ->set( 'section' , $section ); views_ui_standard_display_dropdown( $form , $form_state , $section ); } // A whole bunch of code to figure out what relationships are valid for // this item. $relationships = $executable ->display_handler->getOption( 'relationships' ); $relationship_options = array (); foreach ( $relationships as $relationship ) { // relationships can't link back to self. But also, due to ordering, // relationships can only link to prior relationships. if ( $type == 'relationship' && $id == $relationship [ 'id' ]) { break ; } $relationship_handler = Views::handlerManager( 'relationship' )->getHandler( $relationship ); // ignore invalid/broken relationships. if ( empty ( $relationship_handler )) { continue ; } // If this relationship is valid for this type, add it to the list. $data = Views::viewsData()->get( $relationship [ 'table' ]); if (isset( $data [ $relationship [ 'field' ]][ 'relationship' ][ 'base' ]) && $base = $data [ $relationship [ 'field' ]][ 'relationship' ][ 'base' ]) { $base_fields = Views::viewsDataHelper()->fetchFields( $base , $type , $executable ->display_handler->useGroupBy()); if (isset( $base_fields [ $item [ 'table' ] . '.' . $item [ 'field' ]])) { $relationship_handler ->init( $executable , $executable ->display_handler, $relationship ); $relationship_options [ $relationship [ 'id' ]] = $relationship_handler ->adminLabel(); } } } if (! empty ( $relationship_options )) { // Make sure the existing relationship is even valid. If not, force // it to none. $base_fields = Views::viewsDataHelper()->fetchFields( $view ->get( 'base_table' ), $type , $executable ->display_handler->useGroupBy()); if (isset( $base_fields [ $item [ 'table' ] . '.' . $item [ 'field' ]])) { $relationship_options = array_merge ( array ( 'none' => $this ->t( 'Do not use a relationship' )), $relationship_options ); } $rel = empty ( $item [ 'relationship' ]) ? 'none' : $item [ 'relationship' ]; if ( empty ( $relationship_options [ $rel ])) { // Pick the first relationship. $rel = key( $relationship_options ); // We want this relationship option to get saved even if the user // skips submitting the form. $executable ->setHandlerOption( $display_id , $type , $id , 'relationship' , $rel ); $save_ui_cache = TRUE; // Re-initialize with new relationship. $item [ 'relationship' ] = $rel ; $handler ->init( $executable , $executable ->display_handler, $item ); } $form [ 'options' ][ 'relationship' ] = array ( '#type' => 'select' , '#title' => $this ->t( 'Relationship' ), '#options' => $relationship_options , '#default_value' => $rel , '#weight' => -500, ); } else { $form [ 'options' ][ 'relationship' ] = array ( '#type' => 'value' , '#value' => 'none' , ); } $form [ '#title' ] = $this ->t( 'Configure @type: @item' , array ( '@type' => $types [ $type ][ 'lstitle' ], '@item' => $handler ->adminLabel())); if (! empty ( $handler ->definition[ 'help' ])) { $form [ 'options' ][ 'form_description' ] = array ( '#markup' => $handler ->definition[ 'help' ], '#theme_wrappers' => array ( 'container' ), '#attributes' => array ( 'class' => array ( 'js-form-item form-item description' )), '#weight' => -1000, ); } $form [ '#section' ] = $display_id . '-' . $type . '-' . $id ; // Get form from the handler. $handler ->buildOptionsForm( $form [ 'options' ], $form_state ); $form_state ->set( 'handler' , $handler ); } $name = $form_state ->get( 'update_name' ); $view ->getStandardButtons( $form , $form_state , 'views_ui_config_item_form' , $name ); // Add a 'remove' button. $form [ 'actions' ][ 'remove' ] = array ( '#type' => 'submit' , '#value' => $this ->t( 'Remove' ), '#submit' => array ( array ( $this , 'remove' )), '#limit_validation_errors' => array ( array ( 'override' )), '#button_type' => 'danger' , ); } if ( $save_ui_cache ) { $view ->cacheSet(); } return $form ; } |
Please login to continue.