You are here

class SearchApiRangesWidgetLinks in Search API ranges 7

Widget that renders facets as a series of text links.

Hierarchy

Expanded class hierarchy of SearchApiRangesWidgetLinks

1 string reference to 'SearchApiRangesWidgetLinks'
search_api_ranges_facetapi_widgets in ./search_api_ranges.module
Implements hook_facetapi_widgets().

File

plugins/facetapi/widget_links.inc, line 11
Widgets for facets rendered as test link ranges.

View source
class SearchApiRangesWidgetLinks extends FacetapiWidgetLinks {

  /**
   * Renders the links.
   */
  public function execute() {
    $element =& $this->build[$this->facet['field alias']];

    // Get Search API stuff
    $searcher = $this->facet
      ->getAdapter()
      ->getSearcher();
    $index_id = explode('@', $searcher);
    $index = search_api_index_load($index_id[1]);
    list($query, $results) = $this->facet
      ->getAdapter()
      ->getCurrentSearch();
    $range_field = $this->facet['field alias'];

    // Get facet path field/alias
    if (module_exists('facetapi_pretty_paths')) {
      $processor = new FacetapiUrlProcessorPrettyPaths($this->facet
        ->getAdapter());
      $range_field = $processor
        ->getFacetPrettyPathsAlias($this->facet
        ->getFacet());
    }

    // Prepare variables for min/max query
    $variables = array(
      'element' => $element,
      'index' => $index,
      'range_field' => $range_field,
      'target' => $this->facet
        ->getAdapter()
        ->getSearchPath(),
      'query' => $query,
      'prefix' => $this->settings->settings['prefix'],
      'suffix' => $this->settings->settings['suffix'],
    );

    // Generate the ranges to the be used for the text links
    if (strlen($this->settings->settings['range_advanced']) == 0) {
      $element = search_api_ranges_generate_ranges_simple($variables, $this->settings->settings['range_simple']);
    }
    else {
      $element = search_api_ranges_generate_ranges_advanced($variables, $this->settings->settings['range_advanced']);
    }

    // Create pretty path
    if (module_exists('facetapi_pretty_paths')) {
      foreach ($element as $key => $e) {
        if (isset($e['#query']['f'][0])) {
          $element[$key]['#query']['f'][0] = str_replace(':', '/', $e['#query']['f'][0]);

          // Remove the queries from the f array and add them to the path
          if (isset($e['#path']) && substr_count($e['#path'], '/' . $element[$key]['#query']['f'][0]) > 0) {
            $element[$key]['#active'] = TRUE;
            $element[$key]['#path'] = str_replace('/' . $element[$key]['#query']['f'][0], '', $e['#path']);
          }
          else {
            $element[$key]['#path'] = $element[$key]['#path'] . '/' . $element[$key]['#query']['f'][0];
          }
          unset($element[$key]['#query']['f']);
        }
      }
    }

    // Sets each item's theme hook, builds item list.
    $this
      ->setThemeHooks($element);
    $items_build = $this
      ->buildListItems($element);
    $element = array(
      '#theme' => 'item_list',
      '#items' => $items_build,
      '#attributes' => $this->build['#attributes'],
    );
  }

  /**
   * Allows the widget to provide additional settings to the form.
   */
  function settingsForm(&$form, &$form_state) {
    $form['widget']['widget_settings']['links'][$this->id]['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $this->settings->settings['name'],
      '#description' => t('The name of the range field.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
        'enabled' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['prefix'] = array(
      '#type' => 'textfield',
      '#title' => t('Prefix'),
      '#default_value' => $this->settings->settings['prefix'],
      '#description' => t('Adds a prefix to the text links, e.g. $, #.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
        'enabled' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['suffix'] = array(
      '#type' => 'textfield',
      '#title' => t('Suffix'),
      '#default_value' => $this->settings->settings['suffix'],
      '#description' => t('Adds a suffix to the text links, e.g. €, pcs., etc.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
        'enabled' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['range_simple'] = array(
      '#type' => 'textfield',
      '#title' => t('Simple range'),
      '#default_value' => $this->settings->settings['range_simple'],
      '#description' => t('Add a fixed range to create "range facet".
        For example, "10" on a price field will create 10 - 20, 20 - 30 etc.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
        'enabled' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
    $form['widget']['widget_settings']['links'][$this->id]['range_advanced'] = array(
      '#type' => 'textarea',
      '#title' => t('Advanced range'),
      '#default_value' => $this->settings->settings['range_advanced'],
      '#description' => t('Add a pre-defined list of ranges to create
        "range facets". For examples, refer to README in search api range.'),
      '#states' => array(
        'visible' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
        'enabled' => array(
          'select[name="widget"]' => array(
            'value' => $this->id,
          ),
        ),
      ),
    );
  }

  /**
   * Returns defaults for the settings this widget provides.
   */
  function getDefaultSettings() {
    return array(
      'name' => '',
      'prefix' => '',
      'suffix' => '',
      'range_simple' => 10,
      'range_advanced' => '',
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiRangesWidgetLinks::execute public function Renders the links.
SearchApiRangesWidgetLinks::getDefaultSettings function Returns defaults for the settings this widget provides.
SearchApiRangesWidgetLinks::settingsForm function Allows the widget to provide additional settings to the form.