function template_preprocess_views_ui_rearrange_filter_form

Same name in other branches
  1. 9 core/modules/views_ui/views_ui.theme.inc \template_preprocess_views_ui_rearrange_filter_form()
  2. 10 core/modules/views_ui/views_ui.theme.inc \template_preprocess_views_ui_rearrange_filter_form()
  3. 11.x core/modules/views_ui/views_ui.theme.inc \template_preprocess_views_ui_rearrange_filter_form()

Prepares variables for Views UI rearrange filter form templates.

Default template: views-ui-rearrange-filter-form.html.twig.

Parameters

array $variables: An associative array containing:

  • form: A render element representing the form.

File

core/modules/views_ui/views_ui.theme.inc, line 204

Code

function template_preprocess_views_ui_rearrange_filter_form(&$variables) {
    $form =& $variables['form'];
    $rows = $ungroupable_rows = [];
    // Enable grouping only if > 1 group.
    $variables['grouping'] = count(array_keys($form['#group_options'])) > 1;
    foreach ($form['#group_renders'] as $group_id => $contents) {
        // Header row for the group.
        if ($group_id !== 'ungroupable') {
            // Set up tabledrag so that it changes the group dropdown when rows are
            // dragged between groups.
            $options = [
                'table_id' => 'views-rearrange-filters',
                'action' => 'match',
                'relationship' => 'sibling',
                'group' => 'views-group-select',
                'subgroup' => 'views-group-select-' . $group_id,
            ];
            drupal_attach_tabledrag($form['override'], $options);
            // Title row, spanning all columns.
            $row = [];
            // Add a cell to the first row, containing the group operator.
            $row[] = [
                'class' => [
                    'group',
                    'group-operator',
                    'container-inline',
                ],
                'data' => $form['filter_groups']['groups'][$group_id],
                'rowspan' => max([
                    2,
                    count($contents) + 1,
                ]),
            ];
            // Title.
            $row[] = [
                'class' => [
                    'group',
                    'group-title',
                ],
                'data' => [
                    '#prefix' => '<span>',
                    '#markup' => $form['#group_options'][$group_id],
                    '#suffix' => '</span>',
                ],
                'colspan' => 4,
            ];
            $rows[] = [
                'class' => [
                    'views-group-title',
                ],
                'data' => $row,
                'id' => 'views-group-title-' . $group_id,
            ];
            // Row which will only appear if the group has nothing in it.
            $row = [];
            $class = 'group-' . (count($contents) ? 'populated' : 'empty');
            $instructions = '<span>' . t('No filters have been added.') . '</span> <span class="js-only">' . t('Drag to add filters.') . '</span>';
            // When JavaScript is enabled, the button for removing the group (if it's
            // present) should be hidden, since it will be replaced by a link on the
            // client side.
            if (!empty($form['remove_groups'][$group_id]['#type']) && $form['remove_groups'][$group_id]['#type'] == 'submit') {
                $form['remove_groups'][$group_id]['#attributes']['class'][] = 'js-hide';
            }
            $row[] = [
                'colspan' => 5,
                'data' => [
                    [
                        '#markup' => $instructions,
                    ],
                    $form['remove_groups'][$group_id],
                ],
            ];
            $rows[] = [
                'class' => [
                    'group-message',
                    'group-' . $group_id . '-message',
                    $class,
                ],
                'data' => $row,
                'id' => 'views-group-' . $group_id,
            ];
        }
        foreach ($contents as $id) {
            if (isset($form['filters'][$id]['name'])) {
                $row = [];
                $row[]['data'] = $form['filters'][$id]['name'];
                $form['filters'][$id]['weight']['#attributes']['class'] = [
                    'weight',
                ];
                $row[]['data'] = $form['filters'][$id]['weight'];
                $form['filters'][$id]['group']['#attributes']['class'] = [
                    'views-group-select views-group-select-' . $group_id,
                ];
                $row[]['data'] = $form['filters'][$id]['group'];
                $form['filters'][$id]['removed']['#attributes']['class'][] = 'js-hide';
                $remove_link = [
                    '#type' => 'link',
                    '#url' => Url::fromRoute('<none>'),
                    '#title' => new FormattableMarkup('<span>@text</span>', [
                        '@text' => t('Remove'),
                    ]),
                    '#weight' => '1',
                    '#options' => [
                        'attributes' => [
                            'id' => 'views-remove-link-' . $id,
                            'class' => [
                                'views-hidden',
                                'views-button-remove',
                                'views-groups-remove-link',
                                'views-remove-link',
                            ],
                            'alt' => t('Remove this item'),
                            'title' => t('Remove this item'),
                        ],
                    ],
                ];
                $row[]['data'] = [
                    $form['filters'][$id]['removed'],
                    $remove_link,
                ];
                $row = [
                    'data' => $row,
                    'class' => [
                        'draggable',
                    ],
                    'id' => 'views-row-' . $id,
                ];
                if ($group_id !== 'ungroupable') {
                    $rows[] = $row;
                }
                else {
                    $ungroupable_rows[] = $row;
                }
            }
        }
    }
    if (!$variables['grouping']) {
        $form['filter_groups']['groups'][0]['#title'] = t('Operator');
    }
    if (!empty($ungroupable_rows)) {
        $header = [
            t('Ungroupable filters'),
            t('Weight'),
            [
                'data' => t('Group'),
                'class' => [
                    'views-hide-label',
                ],
            ],
            [
                'data' => t('Remove'),
                'class' => [
                    'views-hide-label',
                ],
            ],
        ];
        $variables['ungroupable_table'] = [
            '#type' => 'table',
            '#header' => $header,
            '#rows' => $ungroupable_rows,
            '#attributes' => [
                'id' => 'views-rearrange-filters-ungroupable',
                'class' => [
                    'arrange',
                ],
            ],
            '#tabledrag' => [
                [
                    'action' => 'order',
                    'relationship' => 'sibling',
                    'group' => 'weight',
                ],
            ],
        ];
    }
    if (empty($rows)) {
        $rows[] = [
            [
                'data' => t('No fields available.'),
                'colspan' => '2',
            ],
        ];
    }
    // Set up tabledrag so that the weights are changed when rows are dragged.
    $variables['table'] = [
        '#type' => 'table',
        '#rows' => $rows,
        '#attributes' => [
            'id' => 'views-rearrange-filters',
            'class' => [
                'arrange',
            ],
        ],
        '#tabledrag' => [
            [
                'action' => 'order',
                'relationship' => 'sibling',
                'group' => 'weight',
            ],
        ],
    ];
    // When JavaScript is enabled, the button for adding a new group should be
    // hidden, since it will be replaced by a link on the client side.
    $form['actions']['add_group']['#attributes']['class'][] = 'js-hide';
}

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