You are here

public function FacetapiAjaxWidgetRanges::execute in Ajax facets 7.3

Implements FacetapiWidget::execute().

Transforms the render array into something that can be themed by theme_item_list().

Overrides FacetapiAjaxWidget::execute

See also

FacetapiWidgetLinks::setThemeHooks()

FacetapiWidgetLinks::buildListItems()

File

plugins/facetapi/ajax_widget_ranges.inc, line 62
The facetapi_links and facetapi_checkbox_links widget plugin classes.

Class

FacetapiAjaxWidgetRanges
Widget that renders facets as a range of values.

Code

public function execute() {
  $element =& $this->build[$this->facet['field alias']];
  $items = $this
    ->buildListItems($element);
  $slider_attrs = [
    'data-facet-name' => rawurlencode($this->settings->facet),
    'data-raw-facet-name' => $this->settings->facet,
    'class' => [
      'slider-wrapper',
    ],
  ];

  // Get min and max values.
  $raw_values = array_keys($element);
  $raw_values = array_filter($raw_values, 'is_numeric');

  // Check that we have values for the facet widget.
  if (!empty($raw_values)) {
    $global_min = min($raw_values);
    $global_max = max($raw_values);
    $slider_attrs['data-min'] = $global_min;
    $slider_attrs['data-max'] = $global_max;

    // Get selected min and max for active value.
    if (!empty($items['active_value'])) {
      $values = explode(' TO ', $items['active_value']);
      $slider_attrs['data-min-val'] = preg_replace('/[^0-9\\.]/', '', $values[0]);
      $slider_attrs['data-max-val'] = preg_replace('/[^0-9\\.]/', '', $values[1]);
    }
    else {
      $slider_attrs['data-min-val'] = $global_min;
      $slider_attrs['data-max-val'] = $global_max;
    }

    // @todo move it into the other place?
    drupal_add_library('system', 'ui.slider');
    $min_input = theme('ajax_facets_ranges_input', [
      'title' => t('From:'),
      'attributes' => [
        'class' => 'ajax-facets-slider-amount-min',
      ],
      'value' => $slider_attrs['data-min-val'],
    ]);
    $max_input = theme('ajax_facets_ranges_input', [
      'title' => t('To:'),
      'attributes' => [
        'class' => 'ajax-facets-slider-amount-max',
      ],
      'value' => $slider_attrs['data-max-val'],
    ]);

    // We cannot use drupal_html_id to save the same id for each facet.
    $wrapper_id = $this->build['#attributes']['id'] . '-wrapper';
    $element = [
      '#markup' => '<div id="' . $wrapper_id . '">' . $this
        ->getResetLink() . '<div class="ajax-facets-controls-wrapper">' . $min_input . $max_input . '<div ' . drupal_attributes($slider_attrs) . '></div>' . '</div>' . '</div>',
    ];
    ajax_facets_add_ajax_js($this->facet);
  }
  else {
    $element = [];
  }
}