facets.module 4.08 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains facets.module
6 7
 */

8
use Drupal\Component\Utility\Html;
9
use Drupal\Core\Routing\RouteMatchInterface;
10
use Drupal\facets\Entity\Facet;
11
use Drupal\facets\Entity\FacetSource;
Nick_vh's avatar
Nick_vh committed
12
use Drupal\search_api\Query\QueryInterface;
13
use Drupal\views\Entity\View;
14
use Drupal\Core\Entity\EntityInterface;
15 16 17 18

/**
 * Implements hook_help().
 */
19
function facets_help($route_name, RouteMatchInterface $route_match) {
20
  switch ($route_name) {
21 22
    // Main module help for the facets module.
    case 'help.page.facets':
23 24
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
25
      $output .= '<p>' . t('Facets test') . '</p>';
26
      return $output;
27
    case 'facets.overview':
28
      return t('Below is a list of facets grouped by facetsources they are associated with. A facetsource is the instance where the facet does the actual filtering, for example a View on a Search API index.');
29 30 31
  }
}

32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * Implements hook_theme().
 */
function facets_theme($existing, $type, $theme, $path) {
  $result_item_variables = [
    'variables' => ['value' => '', 'show_count' => FALSE, 'count' => NULL],
  ];
  return [
    'facets_result_item' => $result_item_variables,
    'facets_result_item_active' => $result_item_variables,
  ];
}

Nick_vh's avatar
Nick_vh committed
45 46 47 48 49
/**
 * Implementation of hook_search_api_query_alter.
 *
 * @param \Drupal\search_api\Query\QueryInterface $query
 */
50
function facets_search_api_query_alter(QueryInterface &$query) {
51 52 53
  if ($query->getIndex()->getServerInstance()->supportsFeature('search_api_facets')) {
    /** @var \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager */
    $facet_manager = \Drupal::service('facets.manager');
Nick_vh's avatar
Nick_vh committed
54

55
    $search_id = $query->getOption('search id');
Nick_vh's avatar
Nick_vh committed
56

57 58 59
    // Add the active filters.
    $facet_manager->alterQuery($query, $search_id);
  }
Nick_vh's avatar
Nick_vh committed
60
}
61 62 63 64 65 66 67

/**
 * Implements hook_entity_presave().
 *
 * We implement this to make sure that a facet gets removed on view updates, so
 * we don't get broken facet blocks.
 */
68
function facets_entity_presave(EntityInterface $entity) {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  // Make sure that we only react on view entities with changed displays.
  if ($entity instanceof View && !empty($entity->original)) {
    if ($entity->original->get('display') != $entity->get('display')) {

      /** @var \Drupal\facets\FacetSource\FacetSourcePluginManager $facet_source_plugin_manager */
      $facet_source_plugin_manager = \Drupal::getContainer()
        ->get('plugin.manager.facets.facet_source');
      $definitions = $facet_source_plugin_manager->getDefinitions();

      // Setup an array of sources that are deleted.
      $sources = [];
      foreach ($entity->original->get('display') as $k => $display) {
        // Check if the current display is also a facet source plugin and that
        // is removed from the view.
        $test = 'search_api_views:' . $entity->id() . ':' . $display['id'];
        if (array_key_exists($test, $definitions) && !array_key_exists($k, $entity->get('display'))) {
          $entity_id = str_replace(':', '__', $test);
          $source_entity = FacetSource::load($entity_id);
          if (!is_null($source_entity)) {
            $source_entity->delete();
            $sources[] = $test;
          }
        }
      }


      // Loop over all deleted sources and delete the facets that were linked to
      // that source.
      if (count($sources) > 0) {
        /** @var \Drupal\facets\FacetManager\DefaultFacetManager $fm */
        $fm = \Drupal::getContainer()->get('facets.manager');
        foreach ($sources as $source) {
          $facets = $fm->getFacetsByFacetSourceId($source);
          foreach ($facets as $facet) {
            $facet->delete();
          }
        }
      }
      $facet_source_plugin_manager->clearCachedDefinitions();
    }
  }

}
112 113 114 115 116 117 118 119 120 121 122 123 124

/**
 * Implements hook_preprocess_block().
 *
 * Adds a class for the widget to the facet block to allow for more specific
 * styling.
 */
function facets_preprocess_block(&$variables) {
  if ($variables['configuration']['provider'] == 'facets') {
    $facet = Facet::load($variables['derivative_plugin_id']);
    $variables['attributes']['class'][] = 'block-facet-widget--' . Html::cleanCssIdentifier($facet->getWidget()['type']);
  }
}