You are here

function facetapi_facet_filters_form in Facet API 7.2

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

Form constructor for the facet filter settings form.

Parameters

FacetapiAdapter $adapter: The adapter object.

array $realm: The realm definition.

array $filter_options: The available filter options.

See also

facetapi_facet_filters_form_submit()

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

File

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

Code

function facetapi_facet_filters_form($form, &$form_state, FacetapiAdapter $adapter, array $realm, array $filters) {

  // Loads variables for code readability.
  $facet = $filters['facet'];
  $settings = $adapter
    ->getFacet($facet)
    ->getSettings($realm);

  // 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 filters for @label', array(
    '@label' => $facet['label'],
  )));

  // Adds Facet API settings, excluded values aren't saved.
  $form['#facetapi'] = array(
    'adapter' => $adapter,
    'realm' => $realm,
    'facet' => $facet,
    'settings' => $settings,
  );

  // Filter order (tabledrag).
  $form['settings']['filters']['table'] = array(
    '#type' => 'item',
    '#title' => t('Active filters'),
    '#theme' => 'facetapi_filter_settings_table',
  );
  $form['settings']['filters']['filter_settings_title'] = array(
    '#type' => 'item',
    '#title' => t('Filter settings'),
    '#prefix' => '<div id="filters-status-wrapper">',
    '#suffix' => '</div>',
    '#weight' => 10,
  );
  $form['settings']['filters']['filter_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 20,
  );

  // Builds plugins.
  $weight = -50;
  $plugins = array();
  foreach ($filters['plugins'] as $id => $plugin) {
    if ($class = ctools_plugin_load_class('facetapi', 'filters', $id, 'handler')) {

      // Gets filter defaults.
      $filter_plugin = new $class($id, $adapter, $settings);
      $default = $filter_plugin
        ->getDefaultSettings() + array(
        'status' => 0,
        'weight' => $weight++,
      );

      // Merges in default settings.
      if (empty($settings->settings['filters'][$id])) {
        $settings->settings['filters'][$id] = array();
      }
      $settings->settings['filters'][$id] += $default;

      // Adds weight to plugin so we can sort it.
      $plugins[$id] = $plugin + array(
        'weight' => $settings->settings['filters'][$id]['weight'],
      );
    }
  }
  uasort($plugins, 'drupal_sort_weight');

  // Builds table, adds settings to vertical tabs.
  $has_settings = FALSE;
  foreach ($plugins as $id => $plugin) {

    // Instantiates filter plugins.
    $filter_plugin = new $class($id, $adapter, $settings);
    $defaults = $filter_plugin
      ->getDefaultSettings();

    // Table elements.
    $form['settings']['filters']['table'][$id]['status'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable @title', array(
        '@title' => $plugin['handler']['label'],
      )),
      '#title_display' => 'invisible',
      '#default_value' => $settings->settings['filters'][$id]['status'],
      '#parents' => array(
        'settings',
        'filters',
        $id,
        'status',
      ),
    );
    $form['settings']['filters']['table'][$id]['filter'] = array(
      '#markup' => filter_xss_admin($plugin['handler']['label']),
    );
    $form['settings']['filters']['table'][$id]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array(
        '@title' => $plugin['handler']['label'],
      )),
      '#title_display' => 'invisible',
      '#delta' => 50,
      '#default_value' => $settings->settings['filters'][$id]['weight'],
      '#parents' => array(
        'settings',
        'filters',
        $id,
        'weight',
      ),
      '#weight' => 0,
    );

    // Initializes vertical tab.
    $form['settings']['filters']['filter_settings'][$id] = array(
      '#type' => 'fieldset',
      '#title' => check_plain($plugin['handler']['label']),
      '#group' => 'settings',
      '#tree' => TRUE,
    );

    // Allows plugin to add settings to the form.
    if ($class = ctools_plugin_load_class('facetapi', 'filters', $id, 'handler')) {
      $filter = new $class($id, $adapter, $settings);
      $filter
        ->settingsForm($form['settings']['filters']['filter_settings'][$id], $form_state);
    }

    // Removes vertical tab if no settings were added.
    if (element_children($form['settings']['filters']['filter_settings'][$id])) {
      $has_settings = TRUE;
    }
    else {
      unset($form['settings']['filters']['filter_settings'][$id]);
    }
  }

  // Removes title and fieldset if there aren't any settings.
  if (!$has_settings) {
    unset($form['settings']['filters']['filter_settings']);
    unset($form['settings']['filters']['filter_settings_title']);
  }
  $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,
    ),
  );
  $form['#submit'][] = 'facetapi_facet_filters_form_submit';
  return $form;
}