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