function facets_entity_predelete in Facets 8
Implements hook_entity_predelete().
We implement this hook to make sure that facet source plugins are cleared when a view is deleted. It also deletes facets that are created on those plugins.
File
- ./
facets.module, line 152 - Contains facets.module.
Code
function facets_entity_predelete(EntityInterface $entity) {
if ($entity instanceof View) {
$facet_source_plugin_manager = \Drupal::getContainer()
->get('plugin.manager.facets.facet_source');
$definitions = $facet_source_plugin_manager
->getDefinitions();
if (!is_array($definitions)) {
return;
}
foreach ($definitions as $plugin_id => $definition) {
if (strpos($plugin_id, 'search_api:' . $entity
->id() . '__') !== FALSE) {
try {
$facetManager = \Drupal::getContainer()
->get('facets.manager');
} catch (ServiceNotFoundException $e) {
\Drupal::logger('facets')
->log(RfcLogLevel::DEBUG, 'Facet manager not found on trying to delete a view.');
return;
}
$facets = $facetManager
->getFacetsByFacetSourceId($plugin_id);
foreach ($facets as $facet) {
$facet
->delete();
}
}
}
// Clear cached plugin definitions for facet source to make sure we don't
// show stale data.
$facet_source_plugin_manager
->clearCachedDefinitions();
}
}