1 views_ui.admin.inc views_ui_add_item_form($form, &$form_state)

Form to add_item items in the views UI.

File

core/modules/views_ui/views_ui.admin.inc, line 3738
Admin page callbacks for the Views UI module.

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'), 'data-backdrop-views-scroll' => TRUE),
    ),
  );

  if (!$view->set_display($display_id)) {
    views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
  }
  $display = &$view->display[$display_id];

  $types = views_object_types();
  $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 base tables allowed based upon what the relationships provide.
  $base_tables = $view->get_base_tables();
  $fields = views_fetch_fields(array_keys($base_tables), $type, $display->handler->use_group_by());

  $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'),
  );

  $form['options']['controls']['group'] = array(
    '#type' => 'select',
    '#title' => t('Filter'),
    '#options' => array(),
    '#empty_value' => 'all',
    '#empty_option' => t('- All -'),
  );

  $form['options']['name'] = array(
    '#prefix' => '<div class="views-radio-box form-checkboxes views-filterable-options">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  // Build the table header.
  $header = array(
    'title' => array('data' => t('Title'), 'sort' => 'asc'),
    'group' => array('data' => t('Category'), 'sort' => 'asc'),
    'help' => array('data' => t('Description')),
  );
  $groups = array();
  foreach ($fields as $key => $field) {
    $group = preg_replace('/[^a-z0-9]/', '-', strtolower($field['group']));
    $groups[$group] = $field['group'];
    $fields[$key]['#attributes'] = array(
      'class' => array(
        'views-filterable-option',
        $group,
      ),
    );
    $fields[$key]['help'] = array(
      'data' => $field['help'],
      'class' => array('description'),
    );
    // Here we generate what will be the ID of the checkbox for this $key.
    // This allows us to tie the label to the checkbox for better accessibility.
    // We can't use backdrop_html_id() because it would generate a unique ID
    // and we need this to match the ID that is generated separately for the
    // checkbox.
    $checkbox_id = backdrop_html_class('edit-name-fields-' . $key);
    $fields[$key]['title'] = array(
      'data' => '<label for="' . $checkbox_id . '">' . $field['title'] . '</label>',
      'class' => array('title'),
    );
    $fields[$key]['group'] = array(
      'data' => $field['group'],
      'class' => array('group'),
    );
    if (!empty($field['aliases']) && is_array($field['aliases'])) {
      foreach ($field['aliases'] as $id => $alias) {
        if (empty($alias['base']) || !empty($base_tables[$alias['base']])) {
          $copy = $field;
          $groups[$group] = $alias['group'];
          $copy['group'] = array(
            'data' => $alias['group'],
            'class' => array('group'),
          );
          $copy['title'] = array(
            'data' => $alias['title'],
            'class' => array('title'),
          );
          if (isset($alias['help'])) {
            $copy['help'] = array(
              'data' => $alias['help'],
              'class' => array('description'),
            );
          }
          $group = preg_replace('/[^a-z0-9]/', '-', strtolower($copy['group']['data']));
          $fields[$key . '$' . $id] = $copy;
          $fields[$key . '$' . $id]['#attributes'] = array(
            'class' => array(
              'views-filterable-option',
              $group,
            ),
          );
        }
      }
    }
  }
  $form['options']['name']['fields'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $fields,
    '#empty' => t('There are no @types available to add.', array(
      '@types' => $ltitle,
    )),
    '#js_select' => FALSE,
  );
  $form['options']['controls']['group']['#options'] = $groups;

  // 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_container'),
    '#attributes' => array(
      'class' => array('container-inline', 'views-add-form-selected'),
      'data-views-offset' => 'bottom',
    ),
  );
  views_ui_standard_form_buttons($form, $form_state, 'views_ui_add_item_form', t('Add and configure @types', array('@types' => $ltitle)));

  // Remove the default submit function.
  $form['actions']['submit']['#submit'] = array_diff($form['actions']['submit']['#submit'], array('views_ui_standard_submit'));
  $form['actions']['submit']['#submit'][] = 'views_ui_add_item_form_submit';

  return $form;
}