You are here

public function ViewUI::getFormBucket in Views (for Drupal 7) 8.3

Add information about a section to a display.

1 call to ViewUI::getFormBucket()
ViewUI::getDisplayDetails in views_ui/lib/Drupal/views_ui/ViewUI.php
Helper function to get the display details section of the edit UI.

File

views_ui/lib/Drupal/views_ui/ViewUI.php, line 897
Definition of Drupal\views_ui\ViewUI.

Class

ViewUI
Stores UI related temporary settings.

Namespace

Drupal\views_ui

Code

public function getFormBucket($type, $display) {
  $build = array(
    '#theme_wrappers' => array(
      'views_ui_display_tab_bucket',
    ),
  );
  $types = static::viewsHandlerTypes();
  $build['#overridden'] = FALSE;
  $build['#defaulted'] = FALSE;
  $build['#name'] = $build['#title'] = $types[$type]['title'];

  // Different types now have different rearrange forms, so we use this switch
  // to get the right one.
  switch ($type) {
    case 'filter':
      $rearrange_url = "admin/structure/views/nojs/rearrange-{$type}/{$this->storage->name}/{$display['id']}/{$type}";
      $rearrange_text = t('And/Or, Rearrange');

      // TODO: Add another class to have another symbol for filter rearrange.
      $class = 'icon compact rearrange';
      break;
    case 'field':

      // Fetch the style plugin info so we know whether to list fields or not.
      $style_plugin = $this->displayHandlers[$display['id']]
        ->getPlugin('style');
      $uses_fields = $style_plugin && $style_plugin
        ->usesFields();
      if (!$uses_fields) {
        $build['fields'][] = array(
          '#markup' => t('The selected style or row format does not utilize fields.'),
          '#theme_wrappers' => array(
            'views_ui_container',
          ),
          '#attributes' => array(
            'class' => array(
              'views-display-setting',
            ),
          ),
        );
        return $build;
      }
    default:
      $rearrange_url = "admin/structure/views/nojs/rearrange/{$this->storage->name}/{$display['id']}/{$type}";
      $rearrange_text = t('Rearrange');
      $class = 'icon compact rearrange';
  }

  // Create an array of actions to pass to theme_links
  $actions = array();
  $count_handlers = count($this->displayHandlers[$display['id']]
    ->getHandlers($type));
  $actions['add'] = array(
    'title' => t('Add'),
    'href' => "admin/structure/views/nojs/add-item/{$this->storage->name}/{$display['id']}/{$type}",
    'attributes' => array(
      'class' => array(
        'icon compact add',
        'views-ajax-link',
      ),
      'title' => t('Add'),
      'id' => 'views-add-' . $type,
    ),
    'html' => TRUE,
  );
  if ($count_handlers > 0) {
    $actions['rearrange'] = array(
      'title' => $rearrange_text,
      'href' => $rearrange_url,
      'attributes' => array(
        'class' => array(
          $class,
          'views-ajax-link',
        ),
        'title' => t('Rearrange'),
        'id' => 'views-rearrange-' . $type,
      ),
      'html' => TRUE,
    );
  }

  // Render the array of links
  $build['#actions'] = array(
    '#type' => 'dropbutton',
    '#links' => $actions,
    '#attributes' => array(
      'class' => array(
        'views-ui-settings-bucket-operations',
      ),
    ),
  );
  if (!$this->displayHandlers[$display['id']]
    ->isDefaultDisplay()) {
    if (!$this->displayHandlers[$display['id']]
      ->isDefaulted($types[$type]['plural'])) {
      $build['#overridden'] = TRUE;
    }
    else {
      $build['#defaulted'] = TRUE;
    }
  }

  // If there's an options form for the bucket, link to it.
  if (!empty($types[$type]['options'])) {
    $build['#title'] = l($build['#title'], "admin/structure/views/nojs/config-type/{$this->storage->name}/{$display['id']}/{$type}", array(
      'attributes' => array(
        'class' => array(
          'views-ajax-link',
        ),
        'id' => 'views-title-' . $type,
      ),
    ));
  }
  static $relationships = NULL;
  if (!isset($relationships)) {

    // Get relationship labels
    $relationships = array();
    foreach ($this->displayHandlers[$display['id']]
      ->getHandlers('relationship') as $id => $handler) {
      $relationships[$id] = $handler
        ->label();
    }
  }

  // Filters can now be grouped so we do a little bit extra:
  $groups = array();
  $grouping = FALSE;
  if ($type == 'filter') {
    $group_info = $this->display_handler
      ->getOption('filter_groups');

    // If there is only one group but it is using the "OR" filter, we still
    // treat it as a group for display purposes, since we want to display the
    // "OR" label next to items within the group.
    if (!empty($group_info['groups']) && (count($group_info['groups']) > 1 || current($group_info['groups']) == 'OR')) {
      $grouping = TRUE;
      $groups = array(
        0 => array(),
      );
    }
  }
  $build['fields'] = array();
  foreach ($this->displayHandlers[$display['id']]
    ->getOption($types[$type]['plural']) as $id => $field) {

    // Build the option link for this handler ("Node: ID = article").
    $build['fields'][$id] = array();
    $build['fields'][$id]['#theme'] = 'views_ui_display_tab_setting';
    $handler = $this->displayHandlers[$display['id']]
      ->getHandler($type, $id);
    if (empty($handler)) {
      $build['fields'][$id]['#class'][] = 'broken';
      $field_name = t('Broken/missing handler: @table > @field', array(
        '@table' => $field['table'],
        '@field' => $field['field'],
      ));
      $build['fields'][$id]['#link'] = l($field_name, "admin/structure/views/nojs/config-item/{$this->storage->name}/{$display['id']}/{$type}/{$id}", array(
        'attributes' => array(
          'class' => array(
            'views-ajax-link',
          ),
        ),
        'html' => TRUE,
      ));
      continue;
    }
    $field_name = check_plain($handler
      ->adminLabel(TRUE));
    if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
      $field_name = '(' . $relationships[$field['relationship']] . ') ' . $field_name;
    }
    $description = filter_xss_admin($handler
      ->adminSummary());
    $link_text = $field_name . (empty($description) ? '' : " ({$description})");
    $link_attributes = array(
      'class' => array(
        'views-ajax-link',
      ),
    );
    if (!empty($field['exclude'])) {
      $link_attributes['class'][] = 'views-field-excluded';
    }
    $build['fields'][$id]['#link'] = l($link_text, "admin/structure/views/nojs/config-item/{$this->storage->name}/{$display['id']}/{$type}/{$id}", array(
      'attributes' => $link_attributes,
      'html' => TRUE,
    ));
    $build['fields'][$id]['#class'][] = drupal_clean_css_identifier($display['id'] . '-' . $type . '-' . $id);
    if ($this->displayHandlers[$display['id']]
      ->useGroupBy() && $handler
      ->usesGroupBy()) {
      $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Aggregation settings') . '</span>', "admin/structure/views/nojs/config-item-group/{$this->storage->name}/{$display['id']}/{$type}/{$id}", array(
        'attributes' => array(
          'class' => 'views-button-configure views-ajax-link',
          'title' => t('Aggregation settings'),
        ),
        'html' => TRUE,
      ));
    }
    if ($handler
      ->hasExtraOptions()) {
      $build['fields'][$id]['#settings_links'][] = l('<span class="label">' . t('Settings') . '</span>', "admin/structure/views/nojs/config-item-extra/{$this->storage->name}/{$display['id']}/{$type}/{$id}", array(
        'attributes' => array(
          'class' => array(
            'views-button-configure',
            'views-ajax-link',
          ),
          'title' => t('Settings'),
        ),
        'html' => TRUE,
      ));
    }
    if ($grouping) {
      $gid = $handler->options['group'];

      // Show in default group if the group does not exist.
      if (empty($group_info['groups'][$gid])) {
        $gid = 0;
      }
      $groups[$gid][] = $id;
    }
  }

  // If using grouping, re-order fields so that they show up properly in the list.
  if ($type == 'filter' && $grouping) {
    $store = $build['fields'];
    $build['fields'] = array();
    foreach ($groups as $gid => $contents) {

      // Display an operator between each group.
      if (!empty($build['fields'])) {
        $build['fields'][] = array(
          '#theme' => 'views_ui_display_tab_setting',
          '#class' => array(
            'views-group-text',
          ),
          '#link' => $group_info['operator'] == 'OR' ? t('OR') : t('AND'),
        );
      }

      // Display an operator between each pair of filters within the group.
      $keys = array_keys($contents);
      $last = end($keys);
      foreach ($contents as $key => $pid) {
        if ($key != $last) {
          $store[$pid]['#link'] .= '&nbsp;&nbsp;' . ($group_info['groups'][$gid] == 'OR' ? t('OR') : t('AND'));
        }
        $build['fields'][$pid] = $store[$pid];
      }
    }
  }
  return $build;
}