You are here

facets.install in Facets 8

Update hooks for the facets module.

File

facets.install
View source
<?php

/**
 * @file
 * Update hooks for the facets module.
 */
use Drupal\facets\Entity\Facet;
use Drupal\facets\Entity\FacetSource;
use Drupal\block\Entity\Block;

/**
 * Implements hook_update_dependencies().
 */
function facets_update_dependencies() {
  $dependencies = [];
  if (version_compare(\Drupal::VERSION, '8.6', '>=') && \Drupal::service('module_handler')
    ->moduleExists('block_content')) {

    // block_content_update_8600() adds some fields to Blocks that makes
    // facets_update_8006() fail if upgraded at the same time.
    $dependencies['facets'][8006] = [
      'block_content' => 8600,
    ];
  }
  return $dependencies;
}

/**
 * Convert facets on Search Api facet sources to use the display plugin.
 */
function facets_update_8001() {

  // We changed the way we work with search api facet sources, we're now using
  // the SearchApiDisplay plugins that search api ships with. This consolidates
  // the external points for facets, sorts, autocomplete and others. This
  // refactor made us a better member of the Search API family. It also makes it
  // easier for other modules that provide a display to support facets, for
  // example, for the search_api_page module.
  //
  // This only works for the 3 default plugins that we previously shipped. So
  // only views that have a page, block, or rest display. The id will get
  // replaced from views_page:foo to search_api:views_page__foo.
  $old_ids = [
    'views_page',
    'views_block',
    'views_rest',
  ];

  /** @var \Drupal\facets\FacetInterface[] $entities */
  $entities = Facet::loadMultiple();
  foreach ($entities as $entity) {
    $facetSourceId = $entity
      ->getFacetSourceId();
    foreach ($old_ids as $id) {
      if (strpos($facetSourceId, $id) !== FALSE) {
        $new_id = str_replace($id . ':', 'search_api:' . $id . '__', $facetSourceId);
        $entity
          ->setFacetSourceId($new_id);
        $entity
          ->save();
      }
    }
  }

  /** @var \Drupal\facets\FacetSourceInterface[] $facetsources */
  $facetsources = FacetSource::loadMultiple();
  foreach ($facetsources as $facetsource) {
    $as_array = $facetsource
      ->toArray();

    // Replace id and name to new naming scheme.
    foreach ($old_ids as $id) {
      if (strpos($as_array['id'], $id) !== FALSE) {
        $as_array['id'] = str_replace($id . '__', 'search_api__' . $id . '__', $as_array['id']);
        $as_array['name'] = str_replace($id . ':', 'search_api:' . $id . '__', $as_array['name']);
      }
    }

    // Create new source.
    unset($as_array['uuid']);
    $existing = FacetSource::load($as_array['id']);
    if (!$existing) {
      FacetSource::create($as_array)
        ->save();

      // Delete old facet source.
      $facetsource
        ->delete();
    }
  }
}

/**
 * Remove 'other_facet' plugin for older versions of facets.
 */
function facets_update_8002() {
  $database = \Drupal::database();
  $query = $database
    ->query("SELECT * FROM {config} WHERE data LIKE '%other_facet%'");
  $results = $query
    ->fetchAll();
  foreach ($results as $result) {
    $data = unserialize($result->data);
    if (isset($data['visibility']['other_facet'])) {
      unset($data['visibility']['other_facet']);
    }
    $database
      ->update('config')
      ->fields([
      'data' => serialize($data),
    ])
      ->condition('name', $result->name)
      ->execute();
  }
}

/**
 * WARNING: Facets core search support has been moved into a separate project.
 *
 * If you are using this feature, you need do download the "facets_core_search"
 * module from drupal.org."
 */
function facets_update_8003() {
  \Drupal::database()
    ->delete('key_value')
    ->condition('collection', 'system.schema')
    ->condition('name', 'core_search_facets')
    ->execute();
}

/**
 * Migrate facets with date widget to use date processor and links widget.
 */
function facets_update_8004() {
  foreach (Facet::loadMultiple() as $facet) {
    $widget = $facet
      ->getWidget();
    if ($widget['type'] === 'datebasic') {

      // Set widget to use links instead.
      $facet
        ->setWidget('links', [
        'show_numbers' => $widget['config']['show_numbers'],
      ]);

      // Migrate widget to processor settings and enable date_item processor.
      $settings = [
        'date_format' => $widget['config']['date_display'],
        'granularity' => $widget['config']['granularity'],
        'date_display' => 'actual_date',
      ];
      if ($widget['config']['display_relative']) {
        $settings['date_display'] = 'relative_date';
      }
      $facet
        ->addProcessor([
        'processor_id' => 'date_item',
        'weights' => [
          'build' => 35,
        ],
        'settings' => $settings,
      ]);
      $facet
        ->save();
    }
  }
}

/**
 * Migrate facets with granular widget to use date processors + links widget.
 */
function facets_update_8005() {
  foreach (Facet::loadMultiple() as $facet) {
    $widget = $facet
      ->getWidget();
    if ($widget['type'] === 'numericgranular') {

      // Set widget to use links instead.
      $facet
        ->setWidget('links', [
        'show_numbers' => $widget['config']['show_numbers'],
      ]);

      // Migrate widget to processor settings and enable date_item processor.
      $settings = [
        'granularity' => $widget['config']['granularity'],
      ];
      $facet
        ->addProcessor([
        'processor_id' => 'granularity_item',
        'weights' => [
          'build' => 35,
        ],
        'settings' => $settings,
      ]);
      $facet
        ->save();
    }
  }
}

/**
 * Update facet blocks configuration with a block id used for AJAX support.
 */
function facets_update_8006() {
  $query = \Drupal::entityQuery('block')
    ->condition('plugin', 'facet_block', 'STARTS_WITH')
    ->execute();
  foreach ($query as $block_id) {
    $block = Block::load($block_id);
    $configuration = $block
      ->get('settings');
    $configuration['block_id'] = $block_id;
    $block
      ->set('settings', $configuration);
    $block
      ->save();
  }
}

Functions

Namesort descending Description
facets_update_8001 Convert facets on Search Api facet sources to use the display plugin.
facets_update_8002 Remove 'other_facet' plugin for older versions of facets.
facets_update_8003 WARNING: Facets core search support has been moved into a separate project.
facets_update_8004 Migrate facets with date widget to use date processor and links widget.
facets_update_8005 Migrate facets with granular widget to use date processors + links widget.
facets_update_8006 Update facet blocks configuration with a block id used for AJAX support.
facets_update_dependencies Implements hook_update_dependencies().