You are here

function _facetapi_multiselect_disable_options in Facetapi Multiselect 7

Set any option items that have a "disabled" value to be disabled

Parameters

string $content The rendered HTML for the select item:

array $element The Drupal FAPI item:

Return value

string The updated markeup

1 string reference to '_facetapi_multiselect_disable_options'
facetapi_multiselect_form in ./facetapi_multiselect.module
Form which displays facets as a multiselect element.

File

./facetapi_multiselect.module, line 102
Displays search facets as a multiselect widget.

Code

function _facetapi_multiselect_disable_options($content, $element) {

  // Check PHP and libxml version.
  static $new_libxml;
  if (!isset($new_libxml)) {
    $new_libxml = version_compare(PHP_VERSION, '5.4', '>=') && defined('LIBXML_HTML_NOIMPLIED') && defined('LIBXML_HTML_NODEFDTD');
  }

  // Create a new DOMDocument object with our rendered HTML.
  $select = new DOMDocument();
  $select->encoding = 'utf-8';
  if ($new_libxml) {
    $select
      ->loadHTML(utf8_decode($content), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  }
  else {
    $select
      ->loadHTML(utf8_decode($content));
  }

  // Loop over our options list
  foreach ($select
    ->getElementsByTagName('option') as $option) {
    if ($option
      ->hasAttribute('value')) {

      // Get the value and see if it's set to 'disabled'. If not, skip
      $value = $option
        ->getAttribute('value');
      if ($value == 'disabled' || strpos($value, ':disabled') !== FALSE) {

        // Set the disabled attribute, remove the value attribute.
        $option
          ->setAttribute('disabled', 'disabled');
        $option
          ->removeAttribute('value');
      }
    }
  }
  return $select
    ->saveHTML();
}