function views_ui_config_item_form

Same name in other branches
  1. 7.x-3.x includes/admin.inc \views_ui_config_item_form()

Form to config_item items in the views UI.

1 string reference to 'views_ui_config_item_form'
views_ui_ajax_forms in includes/admin.inc

File

includes/admin.inc, line 3130

Code

function views_ui_config_item_form(&$form_state) {
    $view =& $form_state['view'];
    $display_id = $form_state['display_id'];
    $type = $form_state['type'];
    $id = $form_state['id'];
    $form = array(
        'options' => array(
            '#tree' => TRUE,
        ),
    );
    if (!$view->set_display($display_id)) {
        views_ajax_render(t('Invalid display id @display', array(
            '@display' => $display_id,
        )));
    }
    $item = $view->get_item($display_id, $type, $id);
    if ($item) {
        $handler = $view->display_handler
            ->get_handler($type, $id);
        if (empty($handler)) {
            $form['markup'] = array(
                '#value' => t("Error: handler for @table > @field doesn't exist!", array(
                    '@table' => $item['table'],
                    '@field' => $item['field'],
                )),
            );
        }
        else {
            $types = views_object_types();
            if ($view->display_handler
                ->defaultable_sections($types[$type]['plural'])) {
                $form_state['section'] = $types[$type]['plural'];
                $view->display_handler
                    ->add_override_button($form['options'], $form_state, $form_state['section']);
            }
            // A whole bunch of code to figure out what relationships are valid for
            // this item.
            $relationships = $view->display_handler
                ->get_option('relationships');
            $relationship_options = array();
            foreach ($relationships as $relationship) {
                // relationships can't link back to self. But also, due to ordering,
                // relationships can only link to prior relationships.
                if ($type == 'relationship' && $id == $relationship['id']) {
                    break;
                }
                $relationship_handler = views_get_handler($relationship['table'], $relationship['field'], 'relationship');
                // ignore invalid/broken relationships.
                if (empty($relationship_handler)) {
                    continue;
                }
                // If this relationship is valid for this type, add it to the list.
                $data = views_fetch_data($relationship['table']);
                $base = $data[$relationship['field']]['relationship']['base'];
                $base_fields = views_fetch_fields($base, $form_state['type'], $view->display_handler
                    ->use_group_by());
                if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
                    $relationship_handler->init($view, $relationship);
                    $relationship_options[$relationship['id']] = $relationship_handler->label();
                }
            }
            if (!empty($relationship_options)) {
                // Make sure the existing relationship is even valid. If not, force
                // it to none.
                $base_fields = views_fetch_fields($view->base_table, $form_state['type'], $view->display_handler
                    ->use_group_by());
                if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
                    $relationship_options = array_merge(array(
                        'none' => t('Do not use a relationship'),
                    ), $relationship_options);
                }
                $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
                if (empty($relationship_options[$rel])) {
                    // Pick the first relationship.
                    $rel = key($relationship_options);
                    // We want this relationship option to get saved even if the user
                    // skips submitting the form.
                    $view->set_item_option($display_id, $type, $id, 'relationship', $rel);
                    $temp_view = $view->clone_view();
                    views_ui_cache_set($temp_view);
                }
                $form['options']['relationship'] = array(
                    '#type' => 'select',
                    '#title' => t('Relationship'),
                    '#options' => $relationship_options,
                    '#default_value' => $rel,
                );
            }
            else {
                $form['options']['relationship'] = array(
                    '#type' => 'value',
                    '#value' => 'none',
                );
            }
            $form['#title'] = check_plain($view->display[$display_id]->display_title) . ': ';
            $form['#title'] .= t('Configure @type %item', array(
                '@type' => $types[$type]['lstitle'],
                '%item' => $handler->ui_name(),
            ));
            $form['form_description'] = array(
                '#type' => 'markup',
                '#weight' => -1000,
                '#prefix' => '<div class="form-item description">',
                '#suffix' => '</div>',
                '#value' => $handler->definition['help'],
            );
            $form['#section'] = $display_id . '-' . $type . '-' . $id;
            // Get form from the handler.
            $handler->options_form($form['options'], $form_state);
            $form_state['handler'] =& $handler;
        }
        $name = NULL;
        if (isset($form_state['update_name'])) {
            $name = $form_state['update_name'];
        }
        views_ui_standard_form_buttons($form, $form_state, 'views_ui_config_item_form', $name, t('Remove'), 'remove');
    }
    return $form;
}