You are here

public function FacetapiMultiSelectWidget::buildOptions in Facetapi Multiselect 7

Builds an array of #options for our select element.

File

plugins/facetapi/facetapi_multiselect.inc, line 29
Defines a plugin for a multiselect facet API widget.

Class

FacetapiMultiSelectWidget
Widget that renders facets as a multiselect element.

Code

public function buildOptions($element, $options = array(), $depth = 0) {
  $settings = $this->settings->settings;
  foreach ($element as $item) {
    if (empty($item['#item_children']) || !$settings['optgroups']) {
      $key = $this
        ->getOptionKey($item);
      $markup = $item['#markup'];
      if ($settings['add_count']) {
        if (!$settings['remove_count_on_active'] || $settings['remove_count_on_active'] && !$item['#active']) {
          $markup .= ' (' . $item['#count'] . ')';
        }
      }
      $options[$key] = $markup;

      // Prepend the text with a depth indicator.
      if ($depth > 0) {
        $options[$key] = str_repeat('-', $depth) . ' ' . $options[$key];
      }

      // If the current item is active, but if we have selected the "Remove
      // selected" option, remove this items from our array, since we want to
      // hide it.
      if ($item['#active'] && $settings['remove_selected']) {
        unset($options[$key]);
      }
    }
    if ($item['#item_children']) {
      if ($settings['optgroups']) {

        // Recursively add any children of the item to the #options array (this
        // will result in them being placed inside optgroups).
        $options[$item['#markup']] = $this
          ->buildOptions($item['#item_children']);
      }
      else {

        // Not using optgroups, so put all items at the root level.
        $options = $options + $this
          ->buildOptions($item['#item_children'], $options, $depth + 1);
      }
    }
  }
  return $options;
}