255 lines
7.6 KiB
Plaintext
255 lines
7.6 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Update hooks for the facets module.
|
|
*/
|
|
|
|
use Drupal\facets\Entity\Facet;
|
|
use Drupal\facets\Entity\FacetSource;
|
|
use Drupal\block\Entity\Block;
|
|
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resave facets for consistent configuration export.
|
|
*/
|
|
function facets_update_8007() {
|
|
// Moved to facets_update_8009().
|
|
}
|
|
|
|
/**
|
|
* Support different hierarchy plugin types.
|
|
*/
|
|
function facets_update_8008() {
|
|
$config_factory = \Drupal::configFactory();
|
|
|
|
foreach ($config_factory->listAll('facets.facet.') as $facet_config_name) {
|
|
$facet = $config_factory->getEditable($facet_config_name);
|
|
$facet->set('hierarchy', ['type' => 'taxonomy', 'config' => []]);
|
|
$facet->save(TRUE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resave facets for consistent configuration export.
|
|
*/
|
|
function facets_update_8009() {
|
|
// Moved to facets_update_8011().
|
|
}
|
|
|
|
/**
|
|
* Enable facet block caching for the views with "Search API tag or time" cache.
|
|
*/
|
|
function facets_update_8010() {
|
|
$facet_storage = \Drupal::entityTypeManager()->getStorage('facets_facet');
|
|
$processed_views = [];
|
|
/** @var \Drupal\facets\FacetInterface $facet */
|
|
foreach ($facet_storage->loadMultiple() as $facet) {
|
|
if (
|
|
($source = $facet->getFacetSource())
|
|
&& $source instanceof SearchApiDisplay
|
|
&& ($view_executable = $source->getViewsDisplay())
|
|
&& !in_array($view_executable->id(), $processed_views)
|
|
&& ($cache_plugin = $view_executable->getDisplay()->getPlugin('cache'))
|
|
&& in_array(
|
|
$cache_plugin->getPluginId(),
|
|
['search_api_tag', 'search_api_time']
|
|
)
|
|
) {
|
|
$view_executable->save();
|
|
$processed_views[] = $view_executable->id();
|
|
}
|
|
}
|
|
return !empty($processed_views)
|
|
? sprintf('Facet caching was enabled for the following views: %s.', implode(', ', $processed_views))
|
|
: 'There are no views with search API cache plugins and facets in the same time, so nothing has been updated.';
|
|
}
|
|
|
|
/**
|
|
* Resave facets for consistent configuration export.
|
|
*/
|
|
function facets_update_8011() {
|
|
$facets = Facet::loadMultiple();
|
|
foreach ($facets as $facet) {
|
|
$facet->save();
|
|
}
|
|
}
|