You are here

function _global_filter_configure_form in Views Global Filter 8

Same name and namespace in other branches
  1. 7 global_filter.blocks.inc \_global_filter_configure_form()

Generates the filter configuration form for filters in a block.

Parameters

int $block_number: the block number

int $filter_key: the filter key

bool $expand_fieldset: whetor to expand or collapse the fieldset holding the form

Return value

array the $form array

1 call to _global_filter_configure_form()
global_filter_block_configure in ./global_filter.blocks.inc
Implements hook_block_configure().

File

./global_filter.blocks.inc, line 113
global_filter.block.inc

Code

function _global_filter_configure_form($block_number, $filter_key, $expand_fieldset) {
  $num_filters = count(global_filter_get_filters_for_block($block_number));
  $name = $num_filters == 0 ? '' : global_filter_get_parameter($filter_key, 'name');
  $form = array();
  $delta = GLOBAL_FILTER_FILTER_KEY_PREFIX . $filter_key;
  $form[$delta] = array(
    '#title' => isset($name) ? t('Settings for global filter %name', array(
      '%name' => $name,
    )) : t('Optional additional global filter (currently not configured)'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => $expand_fieldset,
  );
  $form[$delta]['driver'] = array(
    '#title' => t('Filter driver'),
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#attributes' => array(
      'id' => drupal_html_class('global-filter-driver-' . $filter_key),
    ),
  );
  $radios_name = $delta . '_uses_view';
  $form[$delta]['driver'][$radios_name] = array(
    '#title' => t('Select whether this global filter is to be populated by a field or by a view'),
    '#type' => 'radios',
    '#options' => array(
      0 => module_exists('location') ? t('field, node property, location proximity or search term(s)') : t('field, node property or search term(s)'),
      1 => t('view'),
    ),
    '#default_value' => (int) global_filter_get_parameter($filter_key, 'uses_view'),
  );

  // Same name for both buttons.
  $selector_radios = ':input[name="' . $radios_name . '"]';
  $field_options = global_filter_get_usable_fields();
  $form[$delta]['driver'][$delta . '_field'] = array(
    '#title' => t('Choose the field to be used as a global filter'),
    '#type' => 'select',
    '#default_value' => global_filter_get_parameter($filter_key, 'field'),
    '#options' => $field_options,
    '#description' => t('The field or node property employed to populate the widget used to filter one or more views.'),
    '#states' => array(
      'visible' => array(
        $selector_radios => array(
          'value' => 0,
        ),
      ),
    ),
  );
  $view_options = array_merge(array(
    '' => t('- None -'),
  ), global_filter_get_view_names());
  asort($view_options);
  $form[$delta]['driver'][$delta . '_view'] = array(
    '#title' => t('Choose the view to be used as a global filter'),
    '#type' => 'select',
    '#default_value' => global_filter_get_parameter($filter_key, 'view'),
    '#options' => $view_options,
    '#description' => t('The view employed to populate the widget used to filter one or more other views.'),
    '#states' => array(
      'visible' => array(
        $selector_radios => array(
          'value' => 1,
        ),
      ),
    ),
  );
  $widget_default = global_filter_get_parameter($filter_key, 'widget');
  if (global_filter_get_parameter($filter_key, 'uses_view')) {
    if (empty($widget_default) || strpos($widget_default, 'default_') === 0) {
      $widget_default = 'default';
    }
    $widget_options = array(
      'default' => 'Default',
    );
  }
  else {
    $instances = global_filter_get_field_instances($name);
    if (empty($instances)) {
      $widget_options = array(
        'default' => t('Default, i.e. inherit from field, if field-driven'),
      );
    }
    else {
      foreach ($instances as $instance) {
        $widget_type = $instance['widget']['type'];
        $widget_options['default_' . $widget_type] = t('Inherit %widget widget from field @label (on %bundle)', array(
          '%widget' => $widget_type,
          '@label' => $instance['label'],
          '%bundle' => $instance['bundle'],
        ));
      }
      if (empty($widget_default) || $widget_default == 'default') {

        // If not set inherit widget from last field instance.
        $widget_default = 'default_' . $widget_type;
      }
    }
  }
  $range_option = module_exists('contextual_range_filter') ? t('Range') : t('Range (requires <a href="!url">Contextual Range Filter</a>)', array(
    '!url' => url('http://drupal.org/project/contextual_range_filter'),
  ));
  $widget_options += array(
    'select' => t('Single choice drop-down'),
    'radios' => t('Single choice radio buttons'),
    'multiselect' => t('Multi choice select box'),
    'checkboxes' => t('Multi choice check boxes'),
    'links' => t('List of hyperlinks'),
    'textfield' => t('Text field'),
    'range' => $range_option,
    'proximity' => t('Proximity (reference location and distance)'),
  );
  $form[$delta]['widget'] = array(
    '#title' => 'Widget to render the global filter',
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#description' => t('Not all options apply in all cases, e.g. when the field is natively rendered as a textfield or date picker widget.'),
  );
  $form[$delta]['widget'][$delta . '_widget'] = array(
    '#title' => t('Widget'),
    '#type' => 'radios',
    '#default_value' => $widget_default,
    '#options' => $widget_options,
    '#description' => t("If you tick one of the multi choice widgets, make sure to tick the <strong>Allow multiple values</strong> box in the <strong>More</strong> section of the view's contextual filter. Views does not support multi choice widgets that have non-numeric keys."),
  );
  $description = t('Should be ticked for inherited slider widgets of type float. Optional for slider widgets of type integer and list. Has no effect on any other widget.');
  $note1 = module_exists('slide_with_style') ? '' : t('Requires <a href="!url">Slide with Style</a> or similar. ', array(
    '!url' => url('http://drupal.org/project/select_with_style'),
  ));
  $note2 = module_exists('contextual_range_filter') ? '' : t('Requires <a href="!url">Contextual Range Filter</a>.', array(
    '!url' => url('http://drupal.org/project/contextual_range_filter'),
  ));
  $form[$delta]['widget'][$delta . '_convert_to_range'] = array(
    '#title' => t('Convert slider to range slider'),
    '#type' => 'checkbox',
    '#default_value' => global_filter_get_parameter($filter_key, 'convert_to_range'),
    '#description' => $description . '<br/>' . $note1 . $note2,
  );
  $form[$delta]['widget'][$delta . '_label'] = array(
    '#title' => t('Widget label override'),
    '#type' => 'textfield',
    '#default_value' => global_filter_get_parameter($filter_key, 'label'),
    '#description' => t("If omitted the widget's native label will be used, which may be blank. Use <em>&ltnone&gt;</em> if you want to suppress the label."),
  );
  $option_all_text = global_filter_get_parameter($filter_key, 'option_all_text');
  $form[$delta]['widget'][$delta . '_option_all_text'] = array(
    '#title' => t('Text to appear as the "all" option (non-field widgets only)'),
    '#type' => 'textfield',
    '#default_value' => $option_all_text,
    '#description' => t('Inherited field widgets always use core defaults. Remaining widgets default to <strong>@all</strong>, if left blank.<br/>Enter <em>&lt;none&gt;</em> to omit the "all" option from the widget. <em>&lt;none&gt;</em> is recommended for multi choice widgets.', array(
      '@all' => GLOBAL_FILTER_DEF_OPTION_ALL_TEXT,
    )),
  );
  $form[$delta]['widget'][$delta . '_confirm_question'] = array(
    '#title' => t('Prompt the user to confirm their selection'),
    '#type' => 'textfield',
    '#size' => 120,
    '#default_value' => global_filter_get_parameter($filter_key, 'confirm_question'),
    '#description' => t('The text you enter here will pop up as an alert box with Cancel and OK buttons, for example: <em>Are you sure you want to change this filter ?</em> If you leave this field blank, there will be no prompt.'),
  );
  $form[$delta]['widget'][$delta . '_set_on_select'] = array(
    '#title' => t('Autosubmit: invoke widget immediately upon select'),
    '#type' => 'checkbox',
    '#default_value' => global_filter_get_parameter($filter_key, 'set_on_select'),
    '#description' => t('When ticked this does away with the Set button next to the widget. May not work on certain inherited widgets. When using the "List of hyperlinks" this option is ticked intrinsically.'),
  );
  $form[$delta]['global_defaults'] = array(
    '#title' => 'Global filter defaults',
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
  );
  $field_or_view_options = array(
    '' => ' - ' . t('No default ("all")') . ' - ',
  );
  if (global_filter_get_parameter($filter_key, 'uses_view')) {

    // Execute View to present possible default values.
    $view_name = drupal_substr(global_filter_get_parameter($filter_key, 'view'), 5);
    global_filter_add_view_results($field_or_view_options, $view_name);
  }
  else {

    // Retrieve field values.
    $field_name = global_filter_get_parameter($filter_key, 'field');
    $field = field_info_field($field_name);
    if ($field['type'] == 'taxonomy_term_reference') {
      $vocabulary_name = $field['settings']['allowed_values'][0]['vocabulary'];
      _global_filter_add_terms($field_or_view_options, $vocabulary_name);
    }
    elseif (!empty($field['settings']['allowed_values'])) {
      foreach (list_allowed_values($field) as $key => $value) {
        $field_or_view_options[$key] = $value;
      }
    }
    else {

      // Handle Location:Country.
      // Final case: free text field?
    }
  }
  $global_default = global_filter_get_parameter($filter_key, 'global_field_or_view_default');
  if (empty($global_default)) {
    $keys = array_keys($field_or_view_options);
    $first_option = next($keys);
    $global_default = $option_all_text == '<none>' ? $first_option : NULL;
  }
  $multi_choices = array(
    'multiselect',
    'checkboxes',
    'default',
  );
  $form[$delta]['global_defaults'][$delta . '_global_field_or_view_default'] = array(
    '#title' => t('Default value for the field or view selected above'),
    '#type' => 'select',
    '#multiple' => in_array(global_filter_get_parameter($filter_key, 'widget'), $multi_choices),
    '#options' => $field_or_view_options,
    '#size' => 1,
    '#description' => t('Optionally select default value(s) for the field or view above. These values will be active until the user selects another global filter value. If your widget does not have an "all" option, then you probably don\'t want to set "all" as the default.'),
    '#default_value' => $global_default,
  );
  $description = t("Enclose in PHP 'brackets'. Examples:<br/><code>&lt;?php return 'Beatles'; ?&gt;</code><br/><code>&lt;?php return '2013-01-01--2013-12-31'; ?&gt;</code> (date range, note the double hyphen)<br/>If the selected widget allows multiple choices, then return either an array or a string with choices delimited by plus signs.");
  if (!module_exists('php')) {
    $description .= t('<br/>Enable the core <strong>PHP filter</strong> module for this setting to take effect.');
  }
  $form[$delta]['global_defaults'][$delta . '_global_php_default'] = array(
    '#title' => t('Alternatively specify a default through PHP code'),
    '#type' => 'textarea',
    '#default_value' => global_filter_get_parameter($filter_key, 'global_php_default'),
    '#description' => $description,
  );
  return $form;
}