function views_ui_rearrange_filter_form

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

Form to rearrange items in the views UI.

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

File

includes/admin.inc, line 2641

Code

function views_ui_rearrange_filter_form(&$form_state) {
    $view =& $form_state['view'];
    $display_id = $form_state['display_id'];
    $type = $form_state['type'];
    $types = views_object_types();
    if (!$view->set_display($display_id)) {
        views_ajax_render(t('Invalid display id @display', array(
            '@display' => $display_id,
        )));
    }
    $display =& $view->display[$display_id];
    $form['#title'] = check_plain($display->display_title) . ': ';
    $form['#title'] .= t('Rearrange @type', array(
        '@type' => $types[$type]['ltitle'],
    ));
    $form['#section'] = $display_id . 'rearrange-item';
    if ($display->handler
        ->defaultable_sections($types[$type]['plural'])) {
        $form_state['section'] = $types[$type]['plural'];
        $display->handler
            ->add_override_button($form, $form_state, $form_state['section']);
    }
    if (!empty($view->form_cache)) {
        $groups = $view->form_cache['groups'];
        $handlers = $view->form_cache['handlers'];
    }
    else {
        $groups = $display->handler
            ->get_option('filter_groups');
        $handlers = $display->handler
            ->get_option($types[$type]['plural']);
    }
    $count = 0;
    // Get relationship labels
    $relationships = array();
    foreach ($display->handler
        ->get_handlers('relationship') as $id => $handler) {
        $relationships[$id] = $handler->label();
    }
    $group_options = array();
    
    /**
     * Filter groups is an array that contains:
     * array(
     *   'operator' => 'and' || 'or',
     *   'groups' => array(
     *     $group_id => 'and' || 'or',
     *   ),
     * );
     */
    $grouping = count(array_keys($groups['groups'])) > 1;
    $form['filter_groups']['#tree'] = TRUE;
    $form['filter_groups']['operator'] = array(
        '#type' => 'select',
        '#options' => array(
            'AND' => t('And'),
            'OR' => t('Or'),
        ),
        '#default_value' => $groups['operator'],
        '#attributes' => array(
            'class' => 'warning-on-change',
        ),
        '#title' => t('Operator to use on all groups'),
        '#description' => t('Either "group 0 AND group 1 AND group 2" or "group 0 OR group 1 OR group 2", etc'),
        '#access' => $grouping,
    );
    $form['remove_groups']['#tree'] = TRUE;
    foreach ($groups['groups'] as $id => $group) {
        $form['filter_groups']['groups'][$id] = array(
            '#title' => t('Group operator'),
            '#type' => 'select',
            '#options' => array(
                'AND' => t('And'),
                'OR' => t('Or'),
            ),
            '#default_value' => $group,
            '#attributes' => array(
                'class' => 'warning-on-change',
            ),
        );
        $form['remove_groups'][$id] = array();
        // to prevent a notice
        if ($id != 1) {
            $form['remove_groups'][$id] = array(
                '#type' => 'submit',
                '#value' => t('Remove group @group', array(
                    '@group' => $id,
                )),
                '#group' => $id,
            );
        }
        $group_options[$id] = $id == 1 ? t('Default group') : t('Group @group', array(
            '@group' => $id,
        ));
        $form['#group_renders'][$id] = array();
    }
    $form['#group_options'] = $group_options;
    $form['#groups'] = $groups;
    // We don't use get_handlers() because we want items without handlers to
    // appear and show up as 'broken' so that the user can see them.
    $form['filters'] = array(
        '#tree' => TRUE,
    );
    foreach ($handlers as $id => $field) {
        // If the group does not exist, move the filters to the default group.
        if (empty($field['group']) || empty($groups['groups'][$field['group']])) {
            $field['group'] = 1;
        }
        $handler = $display->handler
            ->get_handler($type, $id);
        if ($grouping && $handler && !$handler->can_group()) {
            $field['group'] = 'ungroupable';
        }
        // If not grouping and the handler is set ungroupable, move it back to
        // the default group to prevent weird errors from having it be in its
        // own group:
        if (!$grouping && $field['group'] == 'ungroupable') {
            $field['group'] = 1;
        }
        // Place this item into the proper group for rendering.
        $form['#group_renders'][$field['group']][] = $id;
        $form['filters'][$id]['weight'] = array(
            '#type' => 'textfield',
            '#default_value' => ++$count,
        );
        $form['filters'][$id]['group'] = array(
            '#type' => 'select',
            '#options' => $group_options,
            '#default_value' => $field['group'],
            '#attributes' => array(
                'class' => 'views-region-select views-region-' . $id,
            ),
            '#access' => $field['group'] !== 'ungroupable',
        );
        if ($handler) {
            $name = $handler->ui_name() . ' ' . $handler->admin_summary();
            if (!empty($field['relationship']) && !empty($relationships[$field['relationship']])) {
                $name = '(' . $relationships[$field['relationship']] . ') ' . $name;
            }
            $form['filters'][$id]['name'] = array(
                '#value' => $name,
            );
        }
        else {
            $form['filters'][$id]['name'] = array(
                '#value' => t('Broken field @id', array(
                    '@id' => $id,
                )),
            );
        }
        $form['filters'][$id]['removed'] = array(
            '#type' => 'checkbox',
            '#id' => 'views-removed-' . $id,
            '#attributes' => array(
                'class' => 'views-remove-checkbox',
            ),
            '#default_value' => 0,
        );
    }
    // Add javascript settings that will be added via $.extend for tabledragging
    // Equivalent: drupal_add_tabledrag('arrange', 'order', 'sibling', 'weight');
    $form['#js']['tableDrag']['arrange']['weight'][0] = array(
        'target' => 'weight',
        'source' => NULL,
        'relationship' => 'sibling',
        'action' => 'order',
        'hidden' => TRUE,
        'limit' => 0,
    );
    $form['#js']['tableDrag']['ungroupable_arrange']['weight'][0] = array(
        'target' => 'weight',
        'source' => NULL,
        'relationship' => 'sibling',
        'action' => 'order',
        'hidden' => TRUE,
        'limit' => 0,
    );
    foreach ($form['#group_renders'] as $group_id => $title) {
        // Add javascript settings that will be added via $.extend for tabledragging
        // Equivalent: drupal_add_tabledrag('arrange', 'match', 'sibling', 'views-group-select', 'views-group-' . $group_id);
        $form['#js']['tableDrag']['arrange']['views-group-select'][] = array(
            'target' => 'views-group-' . $group_id,
            'source' => 'views-group-' . $group_id,
            'relationship' => 'sibling',
            'action' => 'match',
            'hidden' => FALSE,
            'limit' => 0,
        );
    }
    if (isset($form_state['update_name'])) {
        $name = $form_state['update_name'];
    }
    views_ui_standard_form_buttons($form, $form_state, 'views_ui_rearrange_filter_form');
    $form['buttons']['add_group'] = array(
        '#type' => 'submit',
        '#value' => t('Add new group'),
        '#id' => 'views-add-group',
        '#group' => 'add',
    );
    return $form;
}