function 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 includes/admin.inc
Various subforms for editing the pieces of a view.

File

includes/admin.inc, line 4173

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',
                ),
            ),
        ),
    );
    ctools_add_js('dependent');
    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 all the base tables allowed based upon what the relationships
    // provide.
    $base_tables = $view->get_base_tables();
    $options = views_fetch_fields(array_keys($base_tables), $type, $display->handler
        ->use_group_by());
    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(),
            '#attributes' => array(
                'class' => array(
                    'ctools-master-dependent',
                ),
            ),
        );
        $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 DOM objects that Views
        // dependent JS will act upon.
        $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) {
            $form['options']['name'][$group . '_start']['#markup'] = '<div class="ctools-dependent-all ctools-dependent-' . $group . '">';
            $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' => check_plain($option['group']),
                        '!field' => check_plain($option['title']),
                    )),
                    '#description' => filter_xss_admin($option['help']),
                    '#return_value' => $key,
                    '#prefix' => "<div class='{$zebra_class} filterable-option'>",
                    '#suffix' => '</div>',
                );
                $zebra++;
            }
            $form['options']['name'][$group . '_end']['#markup'] = '</div>';
        }
        $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_container',
        ),
        '#attributes' => array(
            'class' => array(
                'container-inline',
                'views-add-form-selected',
            ),
        ),
    );
    ctools_include('dependent');
    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['buttons']['submit']['#submit'] = array_diff($form['buttons']['submit']['#submit'], array(
        'views_ui_standard_submit',
    ));
    $form['buttons']['submit']['#submit'][] = 'views_ui_add_item_form_submit';
    return $form;
}