You are here

function facetapi_form_search_form_alter in Facet API 6

Implementation of hook_form_FORM_ID_alter().

File

./facetapi.module, line 1334
An abstracted facet API that can be used by various search backens.

Code

function facetapi_form_search_form_alter(&$form, &$form_state) {

  // Makes sure we are dealing with a search that has a FacetAPI adapter.
  if (!($adapter = facetapi_adapter_load($form['module']['#value']))) {
    return;
  }

  // Builds the facets as form elements.
  $elements = facetapi_realm_build($form['module']['#value'], 'fieldset');
  $children = element_children($elements);
  if (!empty($children)) {

    // If setting is enabled, expands fieldset if at least one facet is active.
    $collapsed = TRUE;
    if (facetapi_setting_get('expand_fieldset', $form['module']['#value'], 'fieldset')) {
      foreach (facetapi_enabled_facets_get($form['module']['#value'], 'fieldset') as $facet) {
        $items = $adapter
          ->getActiveItems($facet);
        if (!empty($items)) {
          $collapsed = FALSE;
          break;
        }
      }
    }
    $form['facets'] = array(
      '#type' => 'fieldset',
      '#title' => t('Faceted search'),
      '#collapsible' => TRUE,
      '#collapsed' => $collapsed,
      '#attributes' => array(
        'class' => 'search-advanced',
      ),
    );
    $form['facets'] = array_merge($form['facets'], $elements);
    $form['facets']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Search'),
      '#prefix' => '<div class="action">',
      '#suffix' => '</div>',
      '#weight' => 100,
    );

    // Adds facet data container, validate and submit handlers.
    $form['basic']['inline']['processed_facets'] = array(
      '#type' => 'value',
      '#value' => '',
    );
    $form['#validate'][] = 'facetapi_search_form_validate';
    $form['#submit'][] = 'facetapi_search_form_submit';
  }
}