You are here

function facetapi_facet_display_form in Facet API 7

Same name and namespace in other branches
  1. 6.3 facetapi.admin.inc \facetapi_facet_display_form()
  2. 7.2 facetapi.admin.inc \facetapi_facet_display_form()

Form constructor for the facet display settings form.

Parameters

array $realm: The realm definition.

array $facet: The facet definition.

See also

facetapi_facet_display_form_submit()

1 string reference to 'facetapi_facet_display_form'
facetapi_menu in ./facetapi.module
Implements hook_menu().

File

./facetapi.admin.inc, line 380
Admin page callbacks for the Facet API module.

Code

function facetapi_facet_display_form($form, &$form_state, FacetapiAdapter $adapter, array $realm, array $facet) {
  $path = drupal_get_path('module', 'facetapi');
  drupal_add_css($path . '/facetapi.admin.css', array(
    'weight' => CSS_THEME - 1,
  ));
  drupal_add_js($path . '/facetapi.admin.js');
  ctools_include('plugins');

  // We have to set the title due to contextual link magic.
  // @see http://drupal.org/node/1147588#comment-4428940
  drupal_set_title(t('Configure facet display for @label', array(
    '@label' => $facet['label'],
  )));

  // Captures variables and settings for code readability.
  $searcher = $adapter
    ->getSearcher();
  $facet_settings = $adapter
    ->getFacet($facet)
    ->getSettings($realm);
  $global_settings = $adapter
    ->getFacet($facet)
    ->getSettings();
  $form['#facetapi'] = array(
    'adapter' => $adapter,
    'realm' => $realm,
    'facet' => $facet,
    'sort_info' => array(),
    'excluded_values' => array(
      'form_build_id',
      'form_token',
      'form_id',
      'op',
      'submit',
      'submit_list',
      'submit_realm',
      'settings__active_tab',
    ),
  );

  ////

  ////

  //// Widget settings

  ////

  ////
  $form['widget'] = array(
    '#type' => 'fieldset',
    '#title' => t('Display settings'),
  );

  // Builds select options for widgets, allows widgets to add settings.
  $widget_options = array();
  foreach (facetapi_get_widgets($realm, $facet) as $id => $plugin) {
    $widget_options[$id] = $plugin['handler']['label'];
    $class = $plugin['handler']['class'];
    $plugin = new $class($id, $realm, $adapter
      ->getFacet($facet), $facet_settings);
    $plugin
      ->settingsForm($form, $form_state);
  }
  $form['widget']['title_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Override Facet Title %default_title', array(
      '%default_title' => '(' . t('Default') . ': ' . $facet['label'] . ')',
    )),
    '#default_value' => $facet_settings->settings['title_override'],
    '#weight' => -12,
  );
  $form['widget']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Facet Title'),
    '#description' => t('To remove or alter the default title prefix: "Filter by @title:", implement the following hook in your theme: !url.', array(
      '!url' => l('THEME_facetapi_title()', 'http://drupalcontrib.org/api/drupal/contributions!facetapi!facetapi.theme.inc/function/theme_facetapi_title/7', array(
        'attributes' => array(
          'target' => '_blank',
        ),
      )),
    )),
    '#default_value' => $facet_settings->settings['title'],
    '#states' => array(
      'visible' => array(
        'input[name="title_override"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#weight' => -11,
  );
  $form['widget']['widget'] = array(
    '#type' => 'select',
    '#title' => t('Display widget'),
    '#default_value' => $facet_settings->settings['widget'],
    '#options' => $widget_options,
    '#weight' => -10,
    '#description' => t('Select the display widget used to render this facet.'),
  );

  ////

  ////

  //// Sort settings

  ////

  ////
  $form['widget']['sort'] = array(
    '#prefix' => '<div class="facetapi-sort-table">',
    '#suffix' => '</div>',
    '#weight' => -10,
  );
  $form['widget']['sort']['table'] = array(
    '#theme' => 'facetapi_sort_settings_table',
    '#facetapi' => &$form['#facetapi'],
    'sort_weight' => array(
      '#tree' => TRUE,
    ),
    'sort_order' => array(
      '#tree' => TRUE,
    ),
  );

  // Initializes sorts with default settings, orders by default weight. Ordering
  // the weights allows us to iterate over them in order when building the
  // form elements in the foreach() loop below.
  $sort_weight = $facet_settings->settings['sort_weight'];
  $sort_order = $facet_settings->settings['sort_order'];
  foreach (facetapi_get_available_sorts($realm, $facet) as $sort_name => $sort_info) {
    $weight = isset($sort_weight[$sort_name]) ? $sort_weight[$sort_name] : 0;
    $form['#facetapi']['sort_info'][$sort_name] = $sort_info;
    $form['#facetapi']['sort_info'][$sort_name]['weight'] = $weight;
  }

  // Orders the sorts by the default weights set above.
  uasort($form['#facetapi']['sort_info'], 'drupal_sort_weight');

  // Builds checkbox options and weight dropboxes.
  $sort_options = array();
  foreach ($form['#facetapi']['sort_info'] as $sort_name => $sort) {
    $sort_options[$sort_name] = '';
    $order_default = isset($sort_order[$sort_name]) ? $sort_order[$sort_name] : SORT_ASC;
    $form['widget']['sort']['table']['sort_order'][$sort_name] = array(
      '#type' => 'select',
      '#title' => t('Order for sort @title', array(
        '@title' => $sort['label'],
      )),
      '#title_display' => 'invisible',
      '#options' => array(
        SORT_DESC => t('Descending'),
        SORT_ASC => t('Ascending'),
      ),
      '#default_value' => $order_default,
    );
    $weight_default = isset($sort_weight[$sort_name]) ? $sort_weight[$sort_name] : 0;
    $form['widget']['sort']['table']['sort_weight'][$sort_name] = array(
      '#type' => 'weight',
      '#title' => t('Weight for sort @title', array(
        '@title' => $sort['label'],
      )),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => $weight_default,
      '#attributes' => array(
        'class' => array(
          'facetapi-sort-weight',
        ),
      ),
    );
  }
  $form['widget']['sort']['table']['active_sorts'] = array(
    '#type' => 'checkboxes',
    '#options' => $sort_options,
    '#default_value' => $facet_settings->settings['active_sorts'],
  );
  $form['widget']['empty'] = array(
    '#prefix' => '<div class="facetapi-empty-setting">',
    '#suffix' => '</div>',
    '#weight' => 10,
  );
  $empty_options = array();
  foreach (ctools_get_plugins('facetapi', 'empty_behaviors') as $id => $plugin) {
    $empty_options[$id] = $plugin['handler']['label'];
    $class = $plugin['handler']['class'];
    $plugin = new $class($facet_settings);
    $plugin
      ->settingsForm($form, $form_state);
  }
  $form['widget']['empty']['empty_behavior'] = array(
    '#type' => 'select',
    '#title' => t('Empty facet behavior'),
    '#default_value' => $facet_settings->settings['empty_behavior'],
    '#options' => $empty_options,
    '#weight' => -10,
    '#description' => t('The action to take when a facet has no items.'),
  );
  $form['widget']['display_count'] = array(
    '#type' => 'checkbox',
    '#prefix' => '<div class="facetapi-display-count-setting">',
    '#suffix' => '</div>',
    '#title' => t('Display the number of matching items'),
    '#description' => t('If checked, the number of matching items will be output in brackets.'),
    '#default_value' => $facet_settings->settings['display_count'],
  );
  $form['widget']['facet_more_text'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div class="facetapi-more-text-setting">',
    '#suffix' => '</div>',
    '#title' => t('Facet "more" link text'),
    '#default_value' => $facet_settings->settings['facet_more_text'],
  );
  $form['widget']['facet_fewer_text'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div class="facetapi-fewer-text-setting">',
    '#suffix' => '</div>',
    '#title' => t('Facet "fewer" link text'),
    '#default_value' => $facet_settings->settings['facet_fewer_text'],
  );

  ////

  ////

  //// Global settings

  ////

  ////
  $form['global'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => t('Global settings'),
    '#description' => t('The configuration options below apply to this facet across <em>all</em> realms.'),
  );

  // Loop through the query types so the key is the same as the value
  $query_types = drupal_map_assoc($facet['query types'], 'check_plain');
  $form['global']['query_type'] = array(
    '#type' => 'select',
    '#access' => count($facet['query types']) > 1,
    '#title' => t('Query type'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['query_type'],
    '#options' => $query_types,
    '#description' => t('Select the query type this facet will use.'),
  );
  $all_options = array(
    FACETAPI_OPERATOR_AND => t('AND'),
    FACETAPI_OPERATOR_OR => t('OR'),
  );
  $options = array_intersect_key($all_options, array_filter($facet['allowed operators']));
  $form['global']['operator'] = array(
    '#type' => 'radios',
    '#access' => count($options) > 1,
    '#title' => t('Operator'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['operator'],
    '#options' => $options,
    '#description' => t('AND filters are exclusive and narrow the result set. OR filters are inclusive and widen the result set.'),
  );
  $hard_limit_options = drupal_map_assoc(array(
    3,
    5,
    10,
    15,
    20,
    30,
    40,
    50,
    75,
    100,
  ));
  $hard_limit_options[-1] = t('No limit');
  $form['global']['hard_limit'] = array(
    '#type' => 'select',
    '#title' => t('Hard limit'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['hard_limit'],
    '#options' => $hard_limit_options,
    '#description' => t('Display no more than this number of facet items.'),
  );
  $form['global']['flatten'] = array(
    '#type' => 'radios',
    '#access' => $facet['hierarchy callback'],
    '#title' => t('Flatten hierarchy'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['flatten'],
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#description' => t('Do not process hierarchical relationships and display facet items as a flat list.'),
  );
  $form['global']['individual_parent'] = array(
    '#type' => 'radios',
    '#access' => $facet['hierarchy callback'],
    '#title' => t('Treat parent items as individual facet items'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['individual_parent'],
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#description' => t('Parent items will not be active when selecting a children and if active, they will not clear their children facet selection.'),
    '#states' => array(
      'visible' => array(
        ':input[name="global[flatten]"]' => array(
          'value' => 0,
        ),
      ),
    ),
  );
  $form['global']['facet_mincount'] = array(
    '#type' => 'textfield',
    '#access' => $facet['facet mincount allowed'] && $adapter
      ->supportsFacetMincount(),
    '#title' => t('Minimum facet count'),
    '#size' => 5,
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['facet_mincount'],
    '#description' => t('Only display facets that are matched in at least this many documents.'),
    '#element_validate' => array(
      'facetapi_element_validate_integer',
    ),
  );
  $form['global']['facet_missing'] = array(
    '#type' => 'radios',
    '#access' => $facet['facet missing allowed'] && $adapter
      ->supportsFacetMissing(),
    '#title' => t('Add facet for missing values'),
    '#prefix' => '<div class="facetapi-global-setting">',
    '#suffix' => '</div>',
    '#default_value' => $global_settings->settings['facet_missing'],
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#description' => t('Adds an extra facet matching documents with no value at all for this field.'),
  );

  ////

  ////

  //// Finalizes form

  ////

  ////
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 20,
  );

  // Gets destination from query string which is set when the page is navigated
  // to via a contextual link. Builds messages based on where user came from.
  if (isset($_GET['destination']) && !url_is_external($_GET['destination'])) {
    $submit_text = t('Save and go back to search page');
    $cancel_title = t('Return to the search page without saving configuration changes.');
    $url = drupal_parse_url($_GET['destination']);
  }
  else {
    $submit_text = t('Save configuration');
    $cancel_title = t('Return to the realm settings page without saving configuration changes.');
    $url = array();
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $submit_text,
  );

  // Do not show the button if the page was navigated to via a contextual link
  // because it would redirect the user back to the search page.
  $form['actions']['submit_realm'] = array(
    '#type' => 'submit',
    '#access' => !$url,
    '#value' => t('Save and go back to realm settings'),
  );
  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => !$url ? $adapter
      ->getPath($realm['name']) : $url['path'],
    '#options' => !$url ? array() : array(
      'query' => $url['query'],
    ),
    '#attributes' => array(
      'title' => $cancel_title,
    ),
  );

  // Adds form submissions and validation handlers.
  $form['#submit'][] = 'facetapi_facet_display_form_submit';
  $form['#validate'][] = 'facetapi_facet_display_form_validate';

  // Allow the realm, adapter, and URL  to alter the form.
  if ($realm['settings callback']) {
    $realm['settings callback']($form, $form_state);
  }
  $adapter
    ->settingsForm($form, $form_state);
  $adapter
    ->getUrlProcessor()
    ->settingsForm($form, $form_state);
  return $form;
}