views_faceted_filters_js-0.0.18/views_faceted_filters_js.module

views_faceted_filters_js.module
<?php

/**
 * @file
 * Primary module hooks for Views faceted filters js module.
 */

use Drupal\Component\Utility\Html;

/**
 * Helper function to modify view style variables for our needs.
 *
 * Separated to be available for different view styles.
 *
 * @param array $variables
 *   The template variables to modify by reference.
 */
function _views_faceted_filters_preprocess_views_view_style(array &$variables): void {
  $view = $variables['view'];
  $options = $view->style_plugin->options;

  // Preprocess the facets given from the view style settings to match the
  // expected csff format:
  $options['facets'] = _views_faceted_filters_preprocess_views_csff_facets($options['facets'], $variables);

  // DANGER: This is very "Grid" style specific!
  // Grid uses 'items', not rows. If implementing this for other styles, we have
  // to split this out into a helper function or sth. like that.
  if (!empty($variables['items'])) {
    $items = $variables['items'];
    // We have to count the items ourselves.
    $itemCount = 0;
    if (!empty($items)) {
      foreach ($items as $row_id => $item) {
        if (!empty($variables['items'][$row_id]['content'])) {
          foreach ($variables['items'][$row_id]['content'] as $content_id => $content) {
            foreach ($options['facets'] as $facet) {
              if (!empty($facet['enabled'])) {
                $facet_id = $facet['id'];
                // Add csff-item on the cell
                // (condition required due to row/col confusion here):
                $variables['items'][$row_id]['content'][$content_id]['attributes']->addClass('csff-item');

                // Determine data values:
                $value = $variables['view']->style_plugin->getFacetValue($facet_id, $itemCount);
                if ($value !== '') {
                  $variables['items'][$row_id]['content'][$content_id]['attributes']->setAttribute('data-csff-facet-' . $facet_id . '--value', Html::escape($value));
                }
              }
            }
            $itemCount++;
          }
        }
      }
    }
  }

  // Attach library and settings:
  $variables['#attached']['library'][] = 'views_faceted_filters_js/views_faceted_filters_js.module';
  $variables['#attached']['drupalSettings']['views_faceted_filters_js']['targetSelector'] = '.js-view-dom-id-' . $variables['view']->dom_id;
  $variables['#attached']['drupalSettings']['views_faceted_filters_js']['options'] = $options;
}

/**
 * Helper function to preprocess the facets array.
 *
 * The array is expected to:
 * - Only contain enabled facets.
 * - Be an array, no object.
 * - Contain the field_id as id property.
 *
 * @param array $facets
 *   The facets array.
 * @param array $variables
 *   Additional variables.
 *
 * @return array
 *   The processed facets array
 */
function _views_faceted_filters_preprocess_views_csff_facets(array $facets, array $variables) {
  $enabledFacets = [];
  if (!empty($facets)) {
    foreach ($facets as $field_id => $facet_field) {
      // Remove facet if not enabled:
      if (!empty($facet_field['enabled'])) {
        // Move into array, not hash, as expected by JS:
        $facet = $facet_field;
        $facet['id'] = $field_id;
        $enabledFacets[] = $facet;
      }
    }
  }
  // Allow other modules to modify the facets definitions:
  \Drupal::moduleHandler()->alter('views_faceted_filters_js_facets', $enabledFacets, $variables);
  return $enabledFacets;
}

/**
 * Prepares variables for views-view-faceted-filters-js-grid.html.twig template.
 */
function template_preprocess_views_view_faceted_filters_js_grid(&$variables) {
  \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme');

  // Use the original preprocess hook:
  template_preprocess_views_view_grid($variables);

  // Add our custom variables to the render array:
  _views_faceted_filters_preprocess_views_view_style($variables);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc