"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/views_ui/src/Form/Ajax/ConfigHandler.php" (26 Nov 2020, 10192 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ConfigHandler.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\views_ui\Form\Ajax;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Drupal\views\ViewEntityInterface;
    7 use Drupal\views\ViewExecutable;
    8 use Drupal\views\Views;
    9 use Symfony\Component\HttpFoundation\Request;
   10 
   11 /**
   12  * Provides a form for configuring an item in the Views UI.
   13  *
   14  * @internal
   15  */
   16 class ConfigHandler extends ViewsFormBase {
   17 
   18   /**
   19    * Constructs a new ConfigHandler object.
   20    */
   21   public function __construct($type = NULL, $id = NULL) {
   22     $this->setType($type);
   23     $this->setID($id);
   24   }
   25 
   26   /**
   27    * {@inheritdoc}
   28    */
   29   public function getFormKey() {
   30     return 'handler';
   31   }
   32 
   33   /**
   34    * {@inheritdoc}
   35    */
   36   public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
   37     $this->setType($type);
   38     $this->setID($id);
   39     return parent::getForm($view, $display_id, $js);
   40   }
   41 
   42   /**
   43    * {@inheritdoc}
   44    */
   45   public function getFormId() {
   46     return 'views_ui_config_item_form';
   47   }
   48 
   49   /**
   50    * {@inheritdoc}
   51    */
   52   public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
   53     /** @var \Drupal\views\Entity\View $view */
   54     $view = $form_state->get('view');
   55     $display_id = $form_state->get('display_id');
   56     $type = $form_state->get('type');
   57     $id = $form_state->get('id');
   58 
   59     $form = [
   60       'options' => [
   61         '#tree' => TRUE,
   62         '#theme_wrappers' => ['container'],
   63         '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
   64       ],
   65     ];
   66     $executable = $view->getExecutable();
   67     $save_ui_cache = FALSE;
   68     if (!$executable->setDisplay($display_id)) {
   69       $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
   70       return $form;
   71     }
   72     $item = $executable->getHandler($display_id, $type, $id);
   73 
   74     if ($item) {
   75       $handler = $executable->display_handler->getHandler($type, $id);
   76       if (empty($handler)) {
   77         $form['markup'] = ['#markup' => $this->t("Error: handler for @table > @field doesn't exist!", ['@table' => $item['table'], '@field' => $item['field']])];
   78       }
   79       else {
   80         $types = ViewExecutable::getHandlerTypes();
   81 
   82         // If this item can come from the default display, show a dropdown
   83         // that lets the user choose which display the changes should apply to.
   84         if ($executable->display_handler->defaultableSections($types[$type]['plural'])) {
   85           $section = $types[$type]['plural'];
   86           $form_state->set('section', $section);
   87           views_ui_standard_display_dropdown($form, $form_state, $section);
   88         }
   89 
   90         // A whole bunch of code to figure out what relationships are valid for
   91         // this item.
   92         $relationships = $executable->display_handler->getOption('relationships');
   93         $relationship_options = [];
   94 
   95         foreach ($relationships as $relationship) {
   96           // relationships can't link back to self. But also, due to ordering,
   97           // relationships can only link to prior relationships.
   98           if ($type == 'relationship' && $id == $relationship['id']) {
   99             break;
  100           }
  101           $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
  102           // ignore invalid/broken relationships.
  103           if (empty($relationship_handler)) {
  104             continue;
  105           }
  106 
  107           // If this relationship is valid for this type, add it to the list.
  108           $data = Views::viewsData()->get($relationship['table']);
  109           if (isset($data[$relationship['field']]['relationship']['base']) && $base = $data[$relationship['field']]['relationship']['base']) {
  110             $base_fields = Views::viewsDataHelper()->fetchFields($base, $type, $executable->display_handler->useGroupBy());
  111             if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  112               $relationship_handler->init($executable, $executable->display_handler, $relationship);
  113               $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
  114             }
  115           }
  116         }
  117 
  118         if (!empty($relationship_options)) {
  119           // Make sure the existing relationship is even valid. If not, force
  120           // it to none.
  121           $base_fields = Views::viewsDataHelper()->fetchFields($view->get('base_table'), $type, $executable->display_handler->useGroupBy());
  122           if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
  123             $relationship_options = array_merge(['none' => $this->t('Do not use a relationship')], $relationship_options);
  124           }
  125           $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
  126           if (empty($relationship_options[$rel])) {
  127             // Pick the first relationship.
  128             $rel = key($relationship_options);
  129             // We want this relationship option to get saved even if the user
  130             // skips submitting the form.
  131             $executable->setHandlerOption($display_id, $type, $id, 'relationship', $rel);
  132             $save_ui_cache = TRUE;
  133             // Re-initialize with new relationship.
  134             $item['relationship'] = $rel;
  135             $handler->init($executable, $executable->display_handler, $item);
  136           }
  137 
  138           $form['options']['relationship'] = [
  139             '#type' => 'select',
  140             '#title' => $this->t('Relationship'),
  141             '#options' => $relationship_options,
  142             '#default_value' => $rel,
  143             '#weight' => -500,
  144           ];
  145         }
  146         else {
  147           $form['options']['relationship'] = [
  148             '#type' => 'value',
  149             '#value' => 'none',
  150           ];
  151         }
  152 
  153         $form['#title'] = $this->t('Configure @type: @item', ['@type' => $types[$type]['lstitle'], '@item' => $handler->adminLabel()]);
  154 
  155         if (!empty($handler->definition['help'])) {
  156           $form['options']['form_description'] = [
  157             '#markup' => $handler->definition['help'],
  158             '#theme_wrappers' => ['container'],
  159             '#attributes' => ['class' => ['js-form-item form-item description']],
  160             '#weight' => -1000,
  161           ];
  162         }
  163 
  164         $form['#section'] = $display_id . '-' . $type . '-' . $id;
  165 
  166         // Get form from the handler.
  167         $handler->buildOptionsForm($form['options'], $form_state);
  168         $form_state->set('handler', $handler);
  169       }
  170 
  171       $name = $form_state->get('update_name');
  172 
  173       $view->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
  174       // Add a 'remove' button.
  175       $form['actions']['remove'] = [
  176         '#type' => 'submit',
  177         '#value' => $this->t('Remove'),
  178         '#submit' => [[$this, 'remove']],
  179         '#limit_validation_errors' => [['override']],
  180         '#button_type' => 'danger',
  181       ];
  182     }
  183 
  184     if ($save_ui_cache) {
  185       $view->cacheSet();
  186     }
  187 
  188     return $form;
  189   }
  190 
  191   /**
  192    * {@inheritdoc}
  193    */
  194   public function validateForm(array &$form, FormStateInterface $form_state) {
  195     $form_state->get('handler')->validateOptionsForm($form['options'], $form_state);
  196 
  197     if ($form_state->getErrors()) {
  198       $form_state->set('rerender', TRUE);
  199     }
  200   }
  201 
  202   /**
  203    * {@inheritdoc}
  204    */
  205   public function submitForm(array &$form, FormStateInterface $form_state) {
  206     $view = $form_state->get('view');
  207     $display_id = $form_state->get('display_id');
  208     $id = $form_state->get('id');
  209     $handler = $form_state->get('handler');
  210 
  211     // Run it through the handler's submit function.
  212     $handler->submitOptionsForm($form['options'], $form_state);
  213     $item = $handler->options;
  214     $types = ViewExecutable::getHandlerTypes();
  215 
  216     // For footer/header $handler_type is area but $type is footer/header.
  217     // For all other handle types it's the same.
  218     $handler_type = $type = $form_state->get('type');
  219     if (!empty($types[$type]['type'])) {
  220       $handler_type = $types[$type]['type'];
  221     }
  222 
  223     $override = NULL;
  224     $executable = $view->getExecutable();
  225     if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
  226       if (empty($executable->query)) {
  227         $executable->initQuery();
  228       }
  229       $aggregate = $executable->query->getAggregationInfo();
  230       if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
  231         $override = $aggregate[$item['group_type']]['handler'][$type];
  232       }
  233     }
  234 
  235     // Create a new handler and unpack the options from the form onto it. We
  236     // can use that for storage.
  237     $handler = Views::handlerManager($handler_type)->getHandler($item, $override);
  238     $handler->init($executable, $executable->display_handler, $item);
  239 
  240     // Add the incoming options to existing options because items using
  241     // the extra form may not have everything in the form here.
  242     $options = $handler->submitFormCalculateOptions($handler->options, $form_state->getValue('options', []));
  243 
  244     // This unpacks only options that are in the definition, ensuring random
  245     // extra stuff on the form is not sent through.
  246     $handler->unpackOptions($handler->options, $options, NULL, FALSE);
  247 
  248     // Store the item back on the view
  249     $executable->setHandler($display_id, $type, $id, $handler->options);
  250 
  251     // Ensure any temporary options are removed.
  252     if (isset($view->temporary_options[$type][$id])) {
  253       unset($view->temporary_options[$type][$id]);
  254     }
  255 
  256     // Write to cache
  257     $view->cacheSet();
  258   }
  259 
  260   /**
  261    * Submit handler for removing an item from a view
  262    */
  263   public function remove(&$form, FormStateInterface $form_state) {
  264     $view = $form_state->get('view');
  265     $display_id = $form_state->get('display_id');
  266     $type = $form_state->get('type');
  267     $id = $form_state->get('id');
  268     // Store the item back on the view
  269     list($was_defaulted, $is_defaulted) = $view->getOverrideValues($form, $form_state);
  270     $executable = $view->getExecutable();
  271     // If the display selection was changed toggle the override value.
  272     if ($was_defaulted != $is_defaulted) {
  273       $display = &$executable->displayHandlers->get($display_id);
  274       $display->optionsOverride($form, $form_state);
  275     }
  276     $executable->removeHandler($display_id, $type, $id);
  277 
  278     // Write to cache
  279     $view->cacheSet();
  280   }
  281 
  282 }