Loading custom_elements.services.yml +1 −1 Original line number Diff line number Diff line services: custom_elements.generator: class: Drupal\custom_elements\CustomElementGenerator arguments: ['@module_handler'] arguments: ['@module_handler', '@entity.repository'] tags: - { name: service_collector, tag: custom_elements_processor, call: addProcessor } custom_elements.normalizer: Loading src/CustomElementGenerator.php +18 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\custom_elements; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\custom_elements\Processor\CustomElementProcessorInterface; Loading @@ -18,6 +19,13 @@ class CustomElementGenerator { */ protected $moduleHandler; /** * Entity repository. * * @var \Drupal\Core\Entity\EntityRepositoryInterface */ protected $entityRepository; /** * Array of all processors and their priority. * Loading @@ -37,9 +45,12 @@ class CustomElementGenerator { * * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler * The module handler. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository * The entity repository. */ public function __construct(ModuleHandlerInterface $moduleHandler) { public function __construct(ModuleHandlerInterface $moduleHandler, EntityRepositoryInterface $entity_repository) { $this->moduleHandler = $moduleHandler; $this->entityRepository = $entity_repository; } /** Loading Loading @@ -84,12 +95,17 @@ class CustomElementGenerator { * Entity to process. * @param string $viewMode * View mode used for rendering field values into slots. * @param string|null $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. * * @return \Drupal\custom_elements\CustomElement * Extracted custom elements containing data attributes and slots. */ public function generate(ContentEntityInterface $entity, $viewMode) { public function generate(ContentEntityInterface $entity, string $viewMode, string $langcode = NULL) { $custom_element = new CustomElement(); // Get desired entity translation. $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); $custom_element->addCacheableDependency($entity); // By default output tags like drupal-node, drupal-comment and for Loading Loading
custom_elements.services.yml +1 −1 Original line number Diff line number Diff line services: custom_elements.generator: class: Drupal\custom_elements\CustomElementGenerator arguments: ['@module_handler'] arguments: ['@module_handler', '@entity.repository'] tags: - { name: service_collector, tag: custom_elements_processor, call: addProcessor } custom_elements.normalizer: Loading
src/CustomElementGenerator.php +18 −2 Original line number Diff line number Diff line Loading @@ -3,6 +3,7 @@ namespace Drupal\custom_elements; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\custom_elements\Processor\CustomElementProcessorInterface; Loading @@ -18,6 +19,13 @@ class CustomElementGenerator { */ protected $moduleHandler; /** * Entity repository. * * @var \Drupal\Core\Entity\EntityRepositoryInterface */ protected $entityRepository; /** * Array of all processors and their priority. * Loading @@ -37,9 +45,12 @@ class CustomElementGenerator { * * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler * The module handler. * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository * The entity repository. */ public function __construct(ModuleHandlerInterface $moduleHandler) { public function __construct(ModuleHandlerInterface $moduleHandler, EntityRepositoryInterface $entity_repository) { $this->moduleHandler = $moduleHandler; $this->entityRepository = $entity_repository; } /** Loading Loading @@ -84,12 +95,17 @@ class CustomElementGenerator { * Entity to process. * @param string $viewMode * View mode used for rendering field values into slots. * @param string|null $langcode * (optional) For which language the entity should be rendered, defaults to * the current content language. * * @return \Drupal\custom_elements\CustomElement * Extracted custom elements containing data attributes and slots. */ public function generate(ContentEntityInterface $entity, $viewMode) { public function generate(ContentEntityInterface $entity, string $viewMode, string $langcode = NULL) { $custom_element = new CustomElement(); // Get desired entity translation. $entity = $this->entityRepository->getTranslationFromContext($entity, $langcode); $custom_element->addCacheableDependency($entity); // By default output tags like drupal-node, drupal-comment and for Loading