function template_preprocess_views_ui_rearrange_filter_form

template_preprocess_views_ui_rearrange_filter_form(&$variables)

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 178
Preprocessors and theme functions for the Views UI.

Code

function template_preprocess_views_ui_rearrange_filter_form(&$variables) {
  $form = &$variables['form'];
  $rows = $ungroupable_rows = array();
  // 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 = array(
        '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 = array();
      // Add a cell to the first row, containing the group operator.
      $row[] = array(
        'class' => array('group', 'group-operator', 'container-inline'),
        'data' => $form['filter_groups']['groups'][$group_id],
        'rowspan' => max(array(2, count($contents) + 1)),
      );
      // Title.
      $row[] = array(
        'class' => array('group', 'group-title'),
        'data' => array(
          '#prefix' => '<span>',
          '#markup' => $form['#group_options'][$group_id],
          '#suffix' => '</span>',
        ),
        'colspan' => 4,
      );
      $rows[] = array(
        'class' => array('views-group-title'),
        'data' => $row,
        'id' => 'views-group-title-' . $group_id,
      );

      // Row which will only appear if the group has nothing in it.
      $row = array();
      $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[] = array(
        'colspan' => 5,
        'data' => array(
          array('#markup' => $instructions),
          $form['remove_groups'][$group_id],
        ),
      );
      $rows[] = array(
        'class' => array(
          '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 = array();
        $row[]['data'] = $form['filters'][$id]['name'];
        $form['filters'][$id]['weight']['#attributes']['class'] = array('weight');
        $row[]['data'] = $form['filters'][$id]['weight'];
        $form['filters'][$id]['group']['#attributes']['class'] = array('views-group-select views-group-select-' . $group_id);
        $row[]['data'] = $form['filters'][$id]['group'];
        $form['filters'][$id]['removed']['#attributes']['class'][] = 'js-hide';

        $remove_link = array(
          '#type' => 'link',
          '#url' => Url::fromRoute('<none>'),
          '#title' => SafeMarkup::format('<span>@text</span>', array('@text' => t('Remove'))),
          '#weight' => '1',
          '#options' => array(
            'attributes' => array(
              'id' => 'views-remove-link-' . $id,
              'class' => array(
                'views-hidden',
                'views-button-remove',
                'views-groups-remove-link',
                'views-remove-link',
              ),
              'alt' => t('Remove this item'),
              'title' => t('Remove this item'),
            ),
          ),
        );
        $row[]['data'] = array(
          $form['filters'][$id]['removed'],
          $remove_link,
        );

        $row = array(
          'data' => $row,
          'class' => array('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 = array(
      t('Ungroupable filters'),
      t('Weight'),
      array(
        'data' => t('Group'),
        'class' => array('views-hide-label'),
      ),
      array(
        'data' => t('Remove'),
        'class' => array('views-hide-label'),
      ),
    );
    $variables['ungroupable_table'] = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $ungroupable_rows,
      '#attributes' => array(
        'id' => 'views-rearrange-filters-ungroupable',
        'class' => array('arrange'),
      ),
      '#tabledrag' => array(
        array(
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'weight',
        )
      ),
    );
  }

  if (empty($rows)) {
    $rows[] = array(array('data' => t('No fields available.'), 'colspan' => '2'));
  }

  // Set up tabledrag so that the weights are changed when rows are dragged.
  $variables['table'] = array(
    '#type' => 'table',
    '#rows' => $rows,
    '#attributes' => array(
      'id' => 'views-rearrange-filters',
      'class' => array('arrange'),
    ),
    '#tabledrag' => array(
      array(
        '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';

}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!views_ui!views_ui.theme.inc/function/template_preprocess_views_ui_rearrange_filter_form/8.1.x