You are here

function facetapi_facet_dependencies_form in Facet API 7.2

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

Form constructor for the facet dependency settings form.

Parameters

FacetapiAdapter $adapter: The adapter object the settings apply to.

array $realm: The realm definition.

array $dependencies: An array of dependencies.

See also

facetapi_facet_dependencies_form_submit()

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

File

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

Code

function facetapi_facet_dependencies_form($form, &$form_state, FacetapiAdapter $adapter, array $realm, array $dependencies) {

  // Ensure that the CTools plugin component is loaded.
  // @see https://www.drupal.org/node/2122119
  module_load_include('inc', 'ctools', 'includes/plugins');

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

  // Adds Facet API settings, excluded values aren't saved.
  $form['#facetapi'] = array(
    'adapter' => $adapter,
    'realm' => $realm,
    'settings' => FALSE,
    'defaults' => array(),
  );
  $form['description'] = array(
    '#prefix' => '<div class="facetapi-realm-description">',
    '#markup' => t('Dependencies are conditions that must be met in order for the facet to be processed by the server and displayed to the user. Hiding facets via the core block system or through the Drupal forms API will not prevent the server from processing the facets.'),
    '#suffix' => "</div>\n",
  );
  $form['plugins'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 10,
  );

  // Iterates over plugins, adds settings as vertical tabs.
  $plugins = ctools_get_plugins('facetapi', 'dependencies');
  foreach ($dependencies as $plugin) {

    // Only gets settings once.
    if (!$form['#facetapi']['settings']) {
      $settings = $adapter
        ->getFacet($plugin
        ->getFacet())
        ->getSettings();
      $form['#facetapi']['settings'] = $settings;
    }

    // Initializes vertical tab.
    $id = $plugin
      ->getId();
    $form[$id] = array(
      '#type' => 'fieldset',
      '#title' => check_plain($plugins[$id]['handler']['label']),
      '#group' => 'plugins',
    );

    // Allows plugin to add settings to the form, adds defaults.
    $plugin
      ->settingsForm($form, $form_state);
    $form['#facetapi']['defaults'] += $plugin
      ->getDefaultSettings();

    // Removes vertical tab if nothing was added.
    if (!element_children($form[$id])) {
      unset($form[$id]);
    }
  }
  $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_dependencies_form_submit';
  return $form;
}