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.
15 class AddHandler extends ViewsFormBase {
18 * Constructs a new AddHandler object.
20 public function __construct($type = NULL) {
21 $this->setType($type);
27 public function getFormKey() {
34 public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL) {
35 $this->setType($type);
36 return parent::getForm($view, $display_id, $js);
42 public function getFormId() {
43 return 'views_ui_add_handler_form';
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');
56 '#theme_wrappers' => ['container'],
57 '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
61 $executable = $view->getExecutable();
62 if (!$executable->setDisplay($display_id)) {
63 $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
66 $display = &$executable->displayHandlers->get($display_id);
68 $types = ViewExecutable::getHandlerTypes();
69 $ltitle = $types[$type]['ltitle'];
70 $section = $types[$type]['plural'];
72 if (!empty($types[$type]['type'])) {
73 $type = $types[$type]['type'];
76 $form['#title'] = $this->t('Add @type', ['@type' => $ltitle]);
77 $form['#section'] = $display_id . 'add-handler';
79 // Add the display override dropdown.
80 views_ui_standard_display_dropdown($form, $form_state, $section);
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'));
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']],
92 $form['override']['controls']['options_search'] = [
93 '#type' => 'textfield',
94 '#title' => $this->t('Search'),
97 $groups = ['all' => $this->t('- All -')];
98 $form['override']['controls']['group'] = [
100 '#title' => $this->t('Category'),
104 $form['options']['name'] = [
105 '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
106 '#suffix' => '</div>',
107 '#type' => 'tableselect',
109 'title' => $this->t('Title'),
110 'group' => $this->t('Category'),
111 'help' => $this->t('Description'),
113 '#js_select' => FALSE,
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']])) {
125 $copy['group'] = $alias['group'];
126 $copy['title'] = $alias['title'];
127 if (isset($alias['help'])) {
128 $copy['help'] = $alias['help'];
131 $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
132 $groups[$group] = $copy['group'];
133 $grouped_options[$group][$key . '$' . $id] = $copy;
139 foreach ($grouped_options as $group => $group_options) {
140 foreach ($group_options as $key => $option) {
141 $form['options']['name']['#options'][$key] = [
143 'class' => ['filterable-option', $group],
147 '#title' => $option['title'],
148 '#plain_text' => $option['title'],
150 'class' => ['title'],
152 'group' => $option['group'],
154 'data' => $option['help'],
155 'class' => ['description'],
161 $form['override']['controls']['group']['#options'] = $groups;
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>',
168 // Add a div to show the selected items
169 $form['selected'] = [
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'],
174 'class' => ['container-inline', 'views-add-form-selected', 'views-offset-bottom'],
175 'data-drupal-views-offset' => 'bottom',
178 $view->getStandardButtons($form, $form_state, 'views_ui_add_handler_form', $this->t('Add and configure @types', ['@types' => $ltitle]));
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');
184 $form['actions']['submit']['#submit'][] = [$view, 'submitItemAdd'];