function RearrangeFilter::buildForm

Same name and namespace in other branches
  1. 9 core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php \Drupal\views_ui\Form\Ajax\RearrangeFilter::buildForm()
  2. 8.9.x core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php \Drupal\views_ui\Form\Ajax\RearrangeFilter::buildForm()
  3. 11.x core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php \Drupal\views_ui\Form\Ajax\RearrangeFilter::buildForm()

Overrides FormInterface::buildForm

File

core/modules/views_ui/src/Form/Ajax/RearrangeFilter.php, line 40

Class

RearrangeFilter
Provides a rearrange form for Views filters.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $view = $form_state->get('view');
    $display_id = $form_state->get('display_id');
    $type = 'filter';
    $types = ViewExecutable::getHandlerTypes();
    $executable = $view->getExecutable();
    if (!$executable->setDisplay($display_id)) {
        $form['markup'] = [
            '#markup' => $this->t('Invalid display id @display', [
                '@display' => $display_id,
            ]),
        ];
        return $form;
    }
    $display = $executable->displayHandlers
        ->get($display_id);
    $form['#title'] = Html::escape($display->display['display_title']) . ': ';
    $form['#title'] .= $this->t('Rearrange @type', [
        '@type' => $types[$type]['ltitle'],
    ]);
    $form['#section'] = $display_id . 'rearrange-item';
    if ($display->defaultableSections($types[$type]['plural'])) {
        $section = $types[$type]['plural'];
        $form_state->set('section', $section);
        views_ui_standard_display_dropdown($form, $form_state, $section);
    }
    if (!empty($view->form_cache)) {
        $groups = $view->form_cache['groups'];
        $handlers = $view->form_cache['handlers'];
    }
    else {
        $groups = $display->getOption('filter_groups');
        $handlers = $display->getOption($types[$type]['plural']);
    }
    $count = 0;
    // Get relationship labels
    $relationships = [];
    foreach ($display->getHandlers('relationship') as $id => $handler) {
        $relationships[$id] = $handler->adminLabel();
    }
    $group_options = [];
    
    /**
     * Filter groups is an array that contains:
     * [
     *   'operator' => 'and' || 'or',
     *   'groups' => [
     *     $group_id => 'and' || 'or',
     *   ],
     * ];
     */
    $grouping = count(array_keys($groups['groups'])) > 1;
    $form['filter_groups']['#tree'] = TRUE;
    $form['filter_groups']['operator'] = [
        '#type' => 'select',
        '#options' => [
            'AND' => $this->t('And'),
            'OR' => $this->t('Or'),
        ],
        '#default_value' => $groups['operator'],
        '#attributes' => [
            'class' => [
                'warning-on-change',
            ],
        ],
        '#title' => $this->t('Operator to use on all groups'),
        '#description' => $this->t('Either "group 0 AND group 1 AND group 2" or "group 0 OR group 1 OR group 2", etc'),
        '#access' => $grouping,
    ];
    $form['remove_groups']['#tree'] = TRUE;
    foreach ($groups['groups'] as $id => $group) {
        $form['filter_groups']['groups'][$id] = [
            '#title' => $this->t('Operator'),
            '#type' => 'select',
            '#options' => [
                'AND' => $this->t('And'),
                'OR' => $this->t('Or'),
            ],
            '#default_value' => $group,
            '#attributes' => [
                'class' => [
                    'warning-on-change',
                ],
            ],
        ];
        // To prevent a notice.
        $form['remove_groups'][$id] = [];
        if ($id != 1) {
            $form['remove_groups'][$id] = [
                '#type' => 'submit',
                '#value' => $this->t('Remove group @group', [
                    '@group' => $id,
                ]),
                '#id' => "views-remove-group-{$id}",
                '#attributes' => [
                    'class' => [
                        'views-remove-group',
                    ],
                ],
                '#group' => $id,
                '#ajax' => [
                    'url' => NULL,
                ],
            ];
        }
        $group_options[$id] = $id == 1 ? $this->t('Default group') : $this->t('Group @group', [
            '@group' => $id,
        ]);
        $form['#group_renders'][$id] = [];
    }
    $form['#group_options'] = $group_options;
    $form['#groups'] = $groups;
    // We don't use getHandlers() because we want items without handlers to
    // appear and show up as 'broken' so that the user can see them.
    $form['filters'] = [
        '#tree' => TRUE,
    ];
    foreach ($handlers as $id => $field) {
        // If the group does not exist, move the filters to the default group.
        if (empty($field['group']) || empty($groups['groups'][$field['group']])) {
            $field['group'] = 1;
        }
        $handler = $display->getHandler($type, $id);
        if ($grouping && $handler && !$handler->canGroup()) {
            $field['group'] = 'ungroupable';
        }
        // If not grouping and the handler is set ungroupable, move it back to
        // the default group to prevent weird errors from having it be in its
        // own group:
        if (!$grouping && $field['group'] == 'ungroupable') {
            $field['group'] = 1;
        }
        // Place this item into the proper group for rendering.
        $form['#group_renders'][$field['group']][] = $id;
        $form['filters'][$id]['weight'] = [
            '#title' => $this->t('Weight for @id', [
                '@id' => $id,
            ]),
            '#title_display' => 'invisible',
            '#type' => 'textfield',
            '#default_value' => ++$count,
            '#size' => 8,
        ];
        $form['filters'][$id]['group'] = [
            '#title' => $this->t('Group for @id', [
                '@id' => $id,
            ]),
            '#title_display' => 'invisible',
            '#type' => 'select',
            '#options' => $group_options,
            '#default_value' => $field['group'],
            '#attributes' => [
                'class' => [
                    'views-region-select',
                    'views-region-' . $id,
                ],
            ],
            '#access' => $field['group'] !== 'ungroupable',
        ];
        if ($handler) {
            $name = $handler->adminLabel() . ' ' . $handler->adminSummary();
            if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
                $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
            }
            $form['filters'][$id]['name'] = [
                '#markup' => $name,
            ];
        }
        else {
            $form['filters'][$id]['name'] = [
                '#markup' => $this->t('Broken field @id', [
                    '@id' => $id,
                ]),
            ];
        }
        $form['filters'][$id]['removed'] = [
            '#title' => $this->t('Remove @id', [
                '@id' => $id,
            ]),
            '#title_display' => 'invisible',
            '#type' => 'checkbox',
            '#id' => 'views-removed-' . $id,
            '#attributes' => [
                'class' => [
                    'views-remove-checkbox',
                ],
            ],
            '#default_value' => 0,
        ];
    }
    $view->getStandardButtons($form, $form_state, 'views_ui_rearrange_filter_form');
    $form['actions']['add_group'] = [
        '#type' => 'submit',
        '#value' => $this->t('Create new filter group'),
        '#id' => 'views-add-group',
        '#group' => 'add',
        '#attributes' => [
            'class' => [
                'views-add-group',
            ],
        ],
        '#ajax' => [
            'url' => NULL,
        ],
    ];
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.