Skip to content
Snippets Groups Projects

Issue #3319001 by moshe weitzman, dydave: Restrict entity CRUD hooks to config...

1 file
+ 34
23
Compare changes
  • Side-by-side
  • Inline
@@ -5,9 +5,11 @@
* Provides extra menu links for the core drupal toolbar.
*/
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\system\Entity\Menu;
/**
* Implements hook_toolbar().
@@ -60,40 +62,49 @@ function admin_toolbar_tools_help($route_name, RouteMatchInterface $route_match)
* Implements hook_entity_insert().
*/
function admin_toolbar_tools_entity_insert(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
// Rebuild menu links if the created entity is a config entity bundle or menu.
admin_toolbar_tools_rebuild_entity_bundle_links($entity);
}
/**
* Implements hook_entity_update().
*/
function admin_toolbar_tools_entity_update(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
// Rebuild menu links if the updated entity is a config entity bundle or menu.
admin_toolbar_tools_rebuild_entity_bundle_links($entity);
}
/**
* Implements hook_entity_delete().
*/
function admin_toolbar_tools_entity_delete(EntityInterface $entity) {
// Skip rebuild during config sync because rebuild should
// always be a post-sync step.
if (!\Drupal::isConfigSyncing()) {
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
// Rebuild menu links if the deleted entity is a config entity bundle or menu.
admin_toolbar_tools_rebuild_entity_bundle_links($entity);
}
/**
* Helper function for CRUD hooks to rebuild menu links for bundles and menus.
*
* Rebuild menu links only if the entity that was changed (added, deleted or
* updated) is a config entity bundle (such as a content type, media type,
* etc...) or a menu.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that was created, updated, or deleted.
*
* @see \Drupal\admin_toolbar_tools\AdminToolbarToolsHelper::getRebuildEntityTypes()
*/
function admin_toolbar_tools_rebuild_entity_bundle_links(EntityInterface $entity) {
// Skip rebuilding menu links if the provided entity is *not* a config entity
// bundle (for example, node type) and not a menu, or during config sync
// because rebuild should always be a post-sync step.
if ((!($entity instanceof ConfigEntityBundleBase) && !($entity instanceof Menu)) || \Drupal::isConfigSyncing()) {
return;
}
// Rebuild menu links if the updated entity is a config entity bundle or menu.
$entities = \Drupal::service('admin_toolbar_tools.helper')->getRebuildEntityTypes();
if (in_array($entity->getEntityTypeId(), $entities)) {
\Drupal::service('plugin.manager.menu.link')->rebuild();
}
}
Loading