This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from drupal-media/2247779-post-render-callback…
…-service Added an EntityEmbedPostRenderCache service class in response to https://drupal.org/project/2247779 committed to core.
- Loading branch information
Showing
4 changed files
with
218 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* @file Contains Drupal\entity_embed\EntityEmbedPostRenderCache. | ||
*/ | ||
|
||
namespace Drupal\entity_embed; | ||
|
||
use Drupal\Core\Entity\EntityManagerInterface; | ||
use Drupal\Core\Extension\ModuleHandlerInterface; | ||
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager; | ||
|
||
/** | ||
* Defines a service for embedded entity post render cache callbacks. | ||
*/ | ||
class EntityEmbedPostRenderCache { | ||
use EntityHelperTrait; | ||
|
||
/** | ||
* Constructs a EntityEmbedPostRenderCache object. | ||
* | ||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | ||
* The entity manager service. | ||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | ||
* The Module Handler. | ||
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager | ||
* The Module Handler. | ||
*/ | ||
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, EntityEmbedDisplayManager $plugin_manager) { | ||
$this->setEntityManager($entity_manager); | ||
$this->setModuleHandler($module_handler); | ||
$this->setDisplayPluginManager($plugin_manager); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('entity.manager'), | ||
$container->get('module_handler'), | ||
$container->get('plugin.manager.entity_embed.display') | ||
); | ||
} | ||
|
||
/** | ||
* #post_render_cache callback; renders an embedded entity. | ||
* | ||
* Replaces the #post_render_cache placeholder with an embedded entity. | ||
* | ||
* @param array $element | ||
* The renderable array that contains the to be replaced placeholder. | ||
* @param array $context | ||
* An array with the following keys: | ||
* - entity-type: The entity type. | ||
* - entity-id: The entity ID. | ||
* - token: The placeholder token generated in buildPlaceholder(). | ||
* | ||
* @return array | ||
* A renderable array representing the placeholder replaced with the | ||
* rendered entity. | ||
*/ | ||
public function renderEmbed(array $element, array $context) { | ||
$callback = 'entity_embed.post_render_cache:renderEmbed'; | ||
$placeholder = drupal_render_cache_generate_placeholder($callback, $context); | ||
|
||
// Do not bother rendering the entity if the placeholder cannot be found. | ||
if (strpos($element['#markup'], $placeholder) === FALSE) { | ||
return $element; | ||
} | ||
|
||
$entity_output = ''; | ||
try { | ||
if ($entity = $this->loadEntity($context['entity-type'], $context['entity-id'])) { | ||
$entity_output = $this->renderEntityEmbedDisplayPlugin( | ||
$entity, | ||
$context['entity-embed-display'], | ||
$context['entity-embed-settings'], | ||
$context | ||
); | ||
} | ||
} | ||
catch (\Exception $e) { | ||
watchdog_exception('entity_embed', $e); | ||
} | ||
|
||
$element['#markup'] = str_replace($placeholder, $entity_output, $element['#markup']); | ||
return $element; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.