You are here

function views_ui_add_item_form in Views (for Drupal 7) 8.3

Same name and namespace in other branches
  1. 6.3 includes/admin.inc \views_ui_add_item_form()
  2. 6.2 includes/admin.inc \views_ui_add_item_form()
  3. 7.3 includes/admin.inc \views_ui_add_item_form()

Form to add_item items in the views UI.

1 string reference to 'views_ui_add_item_form'
views_ui_ajax_forms in views_ui/admin.inc
Returns information about subforms for editing the pieces of a view.

File

views_ui/admin.inc, line 1520
Provides the Views' administrative interface.

Code

function views_ui_add_item_form($form, &$form_state) {
  $view =& $form_state['view'];
  $display_id = $form_state['display_id'];
  $type = $form_state['type'];
  $form = array(
    'options' => array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#attributes' => array(
        'class' => array(
          'scroll',
        ),
      ),
    ),
  );
  if (!$view
    ->setDisplay($display_id)) {
    views_ajax_error(t('Invalid display id @display', array(
      '@display' => $display_id,
    )));
  }
  $display =& $view->displayHandlers[$display_id];
  $types = ViewUI::viewsHandlerTypes();
  $ltitle = $types[$type]['ltitle'];
  $section = $types[$type]['plural'];
  if (!empty($types[$type]['type'])) {
    $type = $types[$type]['type'];
  }
  $form['#title'] = t('Add @type', array(
    '@type' => $ltitle,
  ));
  $form['#section'] = $display_id . 'add-item';

  // Add the display override dropdown.
  views_ui_standard_display_dropdown($form, $form_state, $section);

  // Figure out all the base tables allowed based upon what the relationships provide.
  $base_tables = $view
    ->getBaseTables();
  $options = views_fetch_fields(array_keys($base_tables), $type, $display
    ->useGroupBy(), $form_state['type']);
  if (!empty($options)) {
    $form['options']['controls'] = array(
      '#theme_wrappers' => array(
        'container',
      ),
      '#id' => 'views-filterable-options-controls',
      '#attributes' => array(
        'class' => array(
          'container-inline',
        ),
      ),
    );
    $form['options']['controls']['options_search'] = array(
      '#type' => 'textfield',
      '#title' => t('Search'),
    );
    $groups = array(
      'all' => t('- All -'),
    );
    $form['options']['controls']['group'] = array(
      '#type' => 'select',
      '#title' => t('Filter'),
      '#options' => array(),
    );
    $form['options']['name'] = array(
      '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
      '#default_value' => 'all',
    );

    // Group options first to simplify the usage of #states.
    $grouped_options = array();
    foreach ($options as $key => $option) {
      $group = preg_replace('/[^a-z0-9]/', '-', strtolower($option['group']));
      $groups[$group] = $option['group'];
      $grouped_options[$group][$key] = $option;
      if (!empty($option['aliases']) && is_array($option['aliases'])) {
        foreach ($option['aliases'] as $id => $alias) {
          if (empty($alias['base']) || !empty($base_tables[$alias['base']])) {
            $copy = $option;
            $copy['group'] = $alias['group'];
            $copy['title'] = $alias['title'];
            if (isset($alias['help'])) {
              $copy['help'] = $alias['help'];
            }
            $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']));
            $groups[$group] = $copy['group'];
            $grouped_options[$group][$key . '$' . $id] = $copy;
          }
        }
      }
    }
    foreach ($grouped_options as $group => $group_options) {
      $zebra = 0;
      foreach ($group_options as $key => $option) {
        $zebra_class = $zebra % 2 ? 'odd' : 'even';
        $form['options']['name'][$key] = array(
          '#type' => 'checkbox',
          '#title' => t('!group: !field', array(
            '!group' => $option['group'],
            '!field' => $option['title'],
          )),
          '#description' => $option['help'],
          '#return_value' => $key,
          '#prefix' => "<div class='{$zebra_class} filterable-option'>",
          '#suffix' => '</div>',
          '#states' => array(
            'visible' => array(
              array(
                ':input[name="group"]' => array(
                  'value' => 'all',
                ),
              ),
              array(
                ':input[name="group"]' => array(
                  'value' => $group,
                ),
              ),
            ),
          ),
        );
        $zebra++;
      }
    }
    $form['options']['controls']['group']['#options'] = $groups;
  }
  else {
    $form['options']['markup'] = array(
      '#markup' => '<div class="form-item">' . t('There are no @types available to add.', array(
        '@types' => $ltitle,
      )) . '</div>',
    );
  }

  // Add a div to show the selected items
  $form['selected'] = array(
    '#type' => 'item',
    '#markup' => '<div class="views-selected-options"></div>',
    '#title' => t('Selected') . ':',
    '#theme_wrappers' => array(
      'form_element',
      'views_ui_container',
    ),
    '#attributes' => array(
      'class' => array(
        'container-inline',
        'views-add-form-selected',
      ),
    ),
  );
  $view
    ->getStandardButtons($form, $form_state, 'views_ui_add_item_form', t('Add and configure @types', array(
    '@types' => $ltitle,
  )));

  // Remove the default submit function.
  $form['buttons']['submit']['#submit'] = array_diff($form['buttons']['submit']['#submit'], array(
    array(
      $view,
      'standardSubmit',
    ),
  ));
  $form['buttons']['submit']['#submit'][] = array(
    $view,
    'submitItemAdd',
  );
  return $form;
}