From e0ccf6885cb2fe94c1c742f63645e162448c4de0 Mon Sep 17 00:00:00 2001 From: Axel Rutz Date: Sat, 29 Apr 2017 02:30:08 +0200 Subject: [PATCH] Issue #2871874: Obsolete hook_asset_injector_asset_info --- asset_injector.api.php | 13 --------- asset_injector.module | 71 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/asset_injector.api.php b/asset_injector.api.php index 27b3435..3d18a54 100644 --- a/asset_injector.api.php +++ b/asset_injector.api.php @@ -16,16 +16,3 @@ function hook_asset_injector_library_info_build_alter(array &$library) { } - -/** - * Use to add additional asset entity types. - * - * @return array - * Values of asset entities. - */ -function hook_asset_injector_asset_info() { - return [ - 'css_injector', - 'js_injector', - ]; -} diff --git a/asset_injector.module b/asset_injector.module index 7507180..5593fc4 100644 --- a/asset_injector.module +++ b/asset_injector.module @@ -6,7 +6,9 @@ */ use Drupal\asset_injector\AssetFileStorage; -use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\asset_injector\AssetInjectorInterface; +use Drupal\Core\Config\Entity\ConfigEntityType; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Asset\AttachedAssetsInterface; @@ -27,20 +29,24 @@ function asset_injector_help($route_name, RouteMatchInterface $route_match) { /** * Implements hook_entity_type_build(). + * + * Mark our entities for use in @see asset_injector_get_entity_types(). + * This way contrib modules can add additional assets by implementing + * \Drupal\asset_injector\AssetInjectorInterface. */ function asset_injector_entity_type_build(array &$entity_types) { - $entity_types['css_injector']->setLinkTemplate('duplicate-form', '/admin/config/development/asset-injector/css/{css_injector}/duplicate'); - $entity_types['js_injector']->setLinkTemplate('duplicate-form', '/admin/config/development/asset-injector/js/{js_injector}/duplicate'); -} - -/** - * Implements hook_asset_injector_asset_info(). - */ -function asset_injector_asset_injector_asset_info() { - return [ - 'css_injector', - 'js_injector', - ]; + foreach ($entity_types as $entity_type_id => $entity_type) { + if ($entity_type instanceof ConfigEntityType) { + $interfaces = class_implements($entity_type->getClass()); + if (isset($interfaces[AssetInjectorInterface::class])) { + // Mark as ours. + $entity_type->set('asset_injector_entity_type', TRUE); + // Add our duplicate-form. + $path = $entity_type->getLinkTemplate('canonical'); + $entity_type->setLinkTemplate('duplicate-form', "$path/duplicate"); + } + } + } } /** @@ -104,7 +110,7 @@ function asset_injector_library_info_build() { */ function asset_injector_page_attachments(array &$attachments) { /** @var RendererInterface $renderer */ - $renderer = Drupal::service('renderer'); + $renderer = \Drupal::service('renderer'); foreach (asset_injector_get_assets() as $asset) { if ($asset->isActive()) { $attachments['#attached']['library'][] = 'asset_injector/' . $asset->libraryNameSuffix(); @@ -120,22 +126,41 @@ function asset_injector_page_attachments(array &$attachments) { * Assets from css & js injectors. */ function asset_injector_get_assets() { - $entities = \Drupal::ModuleHandler()->invokeAll('asset_injector_asset_info'); + /** @var EntityTypeManagerInterface $entity_type_manager */ + $entity_type_manager = \Drupal::entityTypeManager(); $assets = []; - foreach ($entities as $entity_type) { - /** @var ConfigEntityInterface $class */ - $class = Drupal::entityTypeManager() - ->getDefinition($entity_type) - ->getClass(); - $rule_ids = Drupal::entityQuery($entity_type)->execute(); - foreach ($class::loadMultiple($rule_ids) as $rule) { - $assets[] = $rule; + foreach (asset_injector_get_entity_types() as $entity_type_id => $entity_type) { + $entity_type_storage = $entity_type_manager->getStorage($entity_type_id); + $asset_ids = $entity_type_storage->getQuery()->execute(); + foreach ($entity_type_storage->loadMultiple($asset_ids) as $asset) { + $assets[] = $asset; } } return $assets; } /** + * Get asset entity types. + * + * @see asset_injector_entity_type_build(). + * + * @return \Drupal\Core\Entity\EntityTypeInterface[] + */ +function asset_injector_get_entity_types() { + $asset_entity_types = &drupal_static(__FUNCTION__); + if (!isset($asset_entity_types)) { + $entity_types = \Drupal::entityManager()->getDefinitions(); + $asset_entity_types = []; + foreach ($entity_types as $entity_type_id => $entity_type) { + if ($entity_type->get('asset_injector_entity_type')) { + $asset_entity_types[$entity_type_id] = $entity_type; + } + } + } + return $asset_entity_types; +} + +/** * Implements hook_cache_flush(): Delete all asset files. */ function asset_injector_cache_flush() { -- 2.7.4