8b53772e8c007441b0879027892c5d0f4b241f75
[yaffs-website] / web / core / modules / views_ui / src / Form / Ajax / AddHandler.php
1 <?php
2
3 namespace Drupal\views_ui\Form\Ajax;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\ViewEntityInterface;
8 use Drupal\views\Views;
9
10 /**
11  * Provides a form for adding an item in the Views UI.
12  *
13  * @internal
14  */
15 class AddHandler extends ViewsFormBase {
16
17   /**
18    * Constructs a new AddHandler object.
19    */
20   public function __construct($type = NULL) {
21     $this->setType($type);
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getFormKey() {
28     return 'add-handler';
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
35     $this->setType($type);
36     return parent::getForm($view, $display_id, $js);
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getFormId() {
43     return 'views_ui_add_handler_form';
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function buildForm(array $form, FormStateInterface $form_state) {
50     $view = $form_state->get('view');
51     $display_id = $form_state->get('display_id');
52     $type = $form_state->get('type');
53
54     $form = [
55       'options' => [
56         '#theme_wrappers' => ['container'],
57         '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
58       ],
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     $display = &$executable->displayHandlers->get($display_id);
67
68     $types = ViewExecutable::getHandlerTypes();
69     $ltitle = $types[$type]['ltitle'];
70     $section = $types[$type]['plural'];
71
72     if (!empty($types[$type]['type'])) {
73       $type = $types[$type]['type'];
74     }
75
76     $form['#title'] = $this->t('Add @type', ['@type' => $ltitle]);
77     $form['#section'] = $display_id . 'add-handler';
78
79     // Add the display override dropdown.
80     views_ui_standard_display_dropdown($form, $form_state, $section);
81
82     // Figure out all the base tables allowed based upon what the relationships provide.
83     $base_tables = $executable->getBaseTables();
84     $options = Views::viewsDataHelper()->fetchFields(array_keys($base_tables), $type, $display->useGroupBy(), $form_state->get('type'));
85
86     if (!empty($options)) {
87       $form['override']['controls'] = [
88         '#theme_wrappers' => ['container'],
89         '#id' => 'views-filterable-options-controls',
90         '#attributes' => ['class' => ['form--inline', 'views-filterable-options-controls']],
91       ];
92       $form['override']['controls']['options_search'] = [
93         '#type' => 'textfield',
94         '#title' => $this->t('Search'),
95       ];
96
97       $groups = ['all' => $this->t('- All -')];
98       $form['override']['controls']['group'] = [
99         '#type' => 'select',
100         '#title' => $this->t('Category'),
101         '#options' => [],
102       ];
103
104       $form['options']['name'] = [
105         '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
106         '#suffix' => '</div>',
107         '#type' => 'tableselect',
108         '#header' => [
109           'title' => $this->t('Title'),
110           'group' => $this->t('Category'),
111           'help' => $this->t('Description'),
112         ],
113         '#js_select' => FALSE,
114       ];
115
116       $grouped_options = [];
117       foreach ($options as $key => $option) {
118         $group = preg_replace('/[^a-z0-9]/', '-', strtolower($option['group']));
119         $groups[$group] = $option['group'];
120         $grouped_options[$group][$key] = $option;
121         if (!empty($option['aliases']) && is_array($option['aliases'])) {
122           foreach ($option['aliases'] as $id => $alias) {
123             if (empty($alias['base']) || !empty($base_tables[$alias['base']])) {
124               $copy = $option;
125               $copy['group'] = $alias['group'];
126               $copy['title'] = $alias['title'];
127               if (isset($alias['help'])) {
128                 $copy['help'] = $alias['help'];
129               }
130
131               $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
132               $groups[$group] = $copy['group'];
133               $grouped_options[$group][$key . '$' . $id] = $copy;
134             }
135           }
136         }
137       }
138
139       foreach ($grouped_options as $group => $group_options) {
140         foreach ($group_options as $key => $option) {
141           $form['options']['name']['#options'][$key] = [
142             '#attributes' => [
143               'class' => ['filterable-option', $group],
144             ],
145             'title' => [
146               'data' => [
147                 '#title' => $option['title'],
148                 '#plain_text' => $option['title'],
149               ],
150               'class' => ['title'],
151             ],
152             'group' => $option['group'],
153             'help' => [
154               'data' => $option['help'],
155               'class' => ['description'],
156             ],
157           ];
158         }
159       }
160
161       $form['override']['controls']['group']['#options'] = $groups;
162     }
163     else {
164       $form['options']['markup'] = [
165         '#markup' => '<div class="js-form-item form-item">' . $this->t('There are no @types available to add.', ['@types' => $ltitle]) . '</div>',
166       ];
167     }
168     // Add a div to show the selected items
169     $form['selected'] = [
170       '#type' => 'item',
171       '#markup' => '<span class="views-ui-view-title">' . $this->t('Selected:') . '</span> ' . '<div class="views-selected-options"></div>',
172       '#theme_wrappers' => ['form_element', 'views_ui_container'],
173       '#attributes' => [
174         'class' => ['container-inline', 'views-add-form-selected', 'views-offset-bottom'],
175         'data-drupal-views-offset' => 'bottom',
176       ],
177     ];
178     $view->getStandardButtons($form, $form_state, 'views_ui_add_handler_form', $this->t('Add and configure @types', ['@types' => $ltitle]));
179
180     // Remove the default submit function.
181     $form['actions']['submit']['#submit'] = array_filter($form['actions']['submit']['#submit'], function ($var) {
182       return !(is_array($var) && isset($var[1]) && $var[1] == 'standardSubmit');
183     });
184     $form['actions']['submit']['#submit'][] = [$view, 'submitItemAdd'];
185
186     return $form;
187   }
188
189 }