8e71123389985a0fa6739bfdf8a4bc3883254934
[yaffs-website] / web / core / modules / views_ui / src / Form / Ajax / ConfigHandlerExtra.php
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
9 /**
10  * Provides a form for configuring extra information for a Views UI item.
11  *
12  * @internal
13  */
14 class ConfigHandlerExtra extends ViewsFormBase {
15
16   /**
17    * Constructs a new ConfigHandlerExtra object.
18    */
19   public function __construct($type = NULL, $id = NULL) {
20     $this->setType($type);
21     $this->setID($id);
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getFormKey() {
28     return 'handler-extra';
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
35     $this->setType($type);
36     $this->setID($id);
37     return parent::getForm($view, $display_id, $js);
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getFormId() {
44     return 'views_ui_config_item_extra_form';
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function buildForm(array $form, FormStateInterface $form_state) {
51     $view = $form_state->get('view');
52     $display_id = $form_state->get('display_id');
53     $type = $form_state->get('type');
54     $id = $form_state->get('id');
55
56     $form = [
57       'options' => [
58         '#tree' => TRUE,
59         '#theme_wrappers' => ['container'],
60         '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
61       ],
62     ];
63     $executable = $view->getExecutable();
64     if (!$executable->setDisplay($display_id)) {
65       $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
66       return $form;
67     }
68     $item = $executable->getHandler($display_id, $type, $id);
69
70     if ($item) {
71       $handler = $executable->display_handler->getHandler($type, $id);
72       if (empty($handler)) {
73         $form['markup'] = ['#markup' => $this->t("Error: handler for @table > @field doesn't exist!", ['@table' => $item['table'], '@field' => $item['field']])];
74       }
75       else {
76         $handler->init($executable, $executable->display_handler, $item);
77         $types = ViewExecutable::getHandlerTypes();
78
79         $form['#title'] = $this->t('Configure extra settings for @type %item', ['@type' => $types[$type]['lstitle'], '%item' => $handler->adminLabel()]);
80
81         $form['#section'] = $display_id . '-' . $type . '-' . $id;
82
83         // Get form from the handler.
84         $handler->buildExtraOptionsForm($form['options'], $form_state);
85         $form_state->set('handler', $handler);
86       }
87
88       $view->getStandardButtons($form, $form_state, 'views_ui_config_item_extra_form');
89     }
90     return $form;
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function validateForm(array &$form, FormStateInterface $form_state) {
97     $form_state->get('handler')->validateExtraOptionsForm($form['options'], $form_state);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function submitForm(array &$form, FormStateInterface $form_state) {
104     $view = $form_state->get('view');
105     $handler = $form_state->get('handler');
106     // Run it through the handler's submit function.
107     $handler->submitExtraOptionsForm($form['options'], $form_state);
108     $item = $handler->options;
109
110     // Store the data we're given.
111     foreach ($form_state->getValue('options') as $key => $value) {
112       $item[$key] = $value;
113     }
114
115     // Store the item back on the view
116     $view->getExecutable()->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item);
117
118     // Write to cache
119     $view->cacheSet();
120   }
121
122 }