Newer
Older

Joris Vercammen
committed
<?php
/**
* @file
* Update hooks for the facets module.
*/
use Drupal\facets\Entity\Facet;
use Drupal\facets\Entity\FacetSource;

Joris Vercammen
committed
use Drupal\block\Entity\Block;

Hoi Sing Edison Wong
committed
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;

Joris Vercammen
committed

Edouard Cunibil
committed
/**
* 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,
];
}

Edouard Cunibil
committed
return $dependencies;
}

Joris Vercammen
committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* 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();

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

Joris Vercammen
committed
}
}
/**
* 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();
}
}

Jimmy Henderickx
committed
/**
* 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."

Jimmy Henderickx
committed
*/
function facets_update_8003() {
\Drupal::database()->delete('key_value')
->condition('collection', 'system.schema')
->condition('name', 'core_search_facets')
->execute();
}

Jimmy Henderickx
committed
/**
* 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',

Jimmy Henderickx
committed
];
if ($widget['config']['display_relative']) {
$settings['date_display'] = 'relative_date';
}
$facet->addProcessor([
'processor_id' => 'date_item',
'weights' => ['build' => 35],
'settings' => $settings,
]);
$facet->save();
}
}
}

Joris Vercammen
committed
/**
* 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();
}
}
}

Joris Vercammen
committed
/**
* 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')

Markus Kalkbrenner
committed
->accessCheck()

Joris Vercammen
committed
->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();
}
}

Markus Kalkbrenner
committed
/**
* Resave facets for consistent configuration export.
*/
function facets_update_8007() {

Markus Kalkbrenner
committed
// Moved to facets_update_8009().

Markus Kalkbrenner
committed
}

Markus Kalkbrenner
committed
/**
* 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);
}
}

Markus Kalkbrenner
committed
/**
* Resave facets for consistent configuration export.
*/
function facets_update_8009() {

Markus Kalkbrenner
committed
// Moved to facets_update_8011().

Markus Kalkbrenner
committed
}

Hoi Sing Edison Wong
committed
/**
* Enable facet block caching for the views with "Search API tag or time" cache.
*/
function facets_update_8010() {

Markus Kalkbrenner
committed
// Moved to facets_update_8012().
}
/**
* Resave facets for consistent configuration export.
*/
function facets_update_8011() {
$facets = Facet::loadMultiple();
foreach ($facets as $facet) {
$facet->save();
}
}
/**
* Update facet block caching for the views with "Search API tag or time" cache.
*/
function facets_update_8012() {

Hoi Sing Edison Wong
committed
$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)

Markus Kalkbrenner
committed
? sprintf('Facet caching was updated for the following views: %s.', implode(', ', $processed_views))

Hoi Sing Edison Wong
committed
: 'There are no views with search API cache plugins and facets in the same time, so nothing has been updated.';
}