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