3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewExecutable;
7 use Drupal\views\ViewEntityInterface;
8 use Drupal\views\Views;
11 * Provides a form for adding an item in the Views UI.
13 class AddHandler extends ViewsFormBase {
16 * Constructs a new AddHandler object.
18 public function __construct($type = NULL) {
19 $this->setType($type);
25 public function getFormKey() {
32 public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
33 $this->setType($type);
34 return parent::getForm($view, $display_id, $js);
40 public function getFormId() {
41 return 'views_ui_add_handler_form';
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');
54 '#theme_wrappers' => ['container'],
55 '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
59 $executable = $view->getExecutable();
60 if (!$executable->setDisplay($display_id)) {
61 $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
64 $display = &$executable->displayHandlers->get($display_id);
66 $types = ViewExecutable::getHandlerTypes();
67 $ltitle = $types[$type]['ltitle'];
68 $section = $types[$type]['plural'];
70 if (!empty($types[$type]['type'])) {
71 $type = $types[$type]['type'];
74 $form['#title'] = $this->t('Add @type', ['@type' => $ltitle]);
75 $form['#section'] = $display_id . 'add-handler';
77 // Add the display override dropdown.
78 views_ui_standard_display_dropdown($form, $form_state, $section);
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'));
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']],
90 $form['override']['controls']['options_search'] = [
91 '#type' => 'textfield',
92 '#title' => $this->t('Search'),
95 $groups = ['all' => $this->t('- All -')];
96 $form['override']['controls']['group'] = [
98 '#title' => $this->t('Category'),
102 $form['options']['name'] = [
103 '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
104 '#suffix' => '</div>',
105 '#type' => 'tableselect',
107 'title' => $this->t('Title'),
108 'group' => $this->t('Category'),
109 'help' => $this->t('Description'),
111 '#js_select' => FALSE,
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']])) {
123 $copy['group'] = $alias['group'];
124 $copy['title'] = $alias['title'];
125 if (isset($alias['help'])) {
126 $copy['help'] = $alias['help'];
129 $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
130 $groups[$group] = $copy['group'];
131 $grouped_options[$group][$key . '$' . $id] = $copy;
137 foreach ($grouped_options as $group => $group_options) {
138 foreach ($group_options as $key => $option) {
139 $form['options']['name']['#options'][$key] = [
141 'class' => ['filterable-option', $group],
145 '#title' => $option['title'],
146 '#plain_text' => $option['title'],
148 'class' => ['title'],
150 'group' => $option['group'],
152 'data' => $option['help'],
153 'class' => ['description'],
159 $form['override']['controls']['group']['#options'] = $groups;
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>',
166 // Add a div to show the selected items
167 $form['selected'] = [
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'],
172 'class' => ['container-inline', 'views-add-form-selected', 'views-offset-bottom'],
173 'data-drupal-views-offset' => 'bottom',
176 $view->getStandardButtons($form, $form_state, 'views_ui_add_handler_form', $this->t('Add and configure @types', ['@types' => $ltitle]));
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');
182 $form['actions']['submit']['#submit'][] = [$view, 'submitItemAdd'];