Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Added an entity embed service. #177

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion entity_embed.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function hook_entity_embed_display_plugins_for_context_alter(array &$definitions

// For nodes, use the default option.
if ($entity instanceof \Drupal\node\NodeInterface) {
$definitions = array_intersect_key($definitions, array_flip(['entity_reference:entity_reference_entity_view']));
$definitions = array_intersect_key($definitions, array_flip([\Drupal\entity_embed\EntityEmbedInterface::DEFAULT_DISPLAY_ID]));
}
}

Expand Down
3 changes: 3 additions & 0 deletions entity_embed.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ services:
plugin.manager.entity_embed.display:
class: Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
entity_embed:
class: Drupal\entity_embed\EntityEmbed
arguments: ['@entity.manager', '@module_handler', '@renderer', '@plugin.manager.entity_embed.display']
111 changes: 111 additions & 0 deletions src/EntityEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Drupal\entity_embed;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityInterface;

class EntityEmbedService implements EntityEmbedInterface, ContainerInjectionInterface {
use EntityHelperTrait;

/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface.
*/
protected $renderer;

/**
* The display plugin manager.
*
* @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager.
*/
protected $displayPluginManager;

/**
* {@inheritdoc}
*/
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, EntityEmbedDisplayManager $display_plugin_manager) {
$this->setEntityManager($entity_manager);
$this->setModuleHandler($module_handler);
$this->renderer = $renderer;
$this->displayPluginManager = $display_plugin_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('module_handler'),
$container->get('renderer'),
$container->get('plugin.manager.entity_embed.display')
);
}

/**
* {@inheritdoc}
*/
public function renderEntityEmbed(EntityInterface $entity, array $context = array()) {
// Merge in default attributes.
$context += array(
'data-entity-id' => $entity->id(),
'data-entity-type' => $entity->getEntityTypeId(),
'data-entity-embed-display' => static::DEFAULT_DISPLAY_ID,
'data-entity-embed-settings' => array(),
);

// The default display plugin has been deprecated by the rendered entity
// field formatter.
if ($context['data-entity-embed-display'] === 'default') {
$context['data-entity-embed-display'] = static::DEFAULT_DISPLAY_ID;
}

// Support the deprecated view-mode data attribute.
if (isset($context['data-view-mode']) && $context['data-entity-embed-display'] === static::DEFAULT_DISPLAY_ID && empty($context['data-entity-embed-settings'])) {
$context['data-entity-embed-settings']['view_mode'] = &$context['data-view-mode'];
}

// Allow modules to alter the entity prior to embed rendering.
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed_context", 'entity_embed_context'), $context, $entity);

// Build and render the display plugin, allowing modules to alter the
// result before rendering.
$build = $this->renderEntityEmbedDisplayPlugin(
$entity,
$context['data-entity-embed-display'],
$context['data-entity-embed-settings'],
$context
);
// @todo Should this hook get invoked if $build is an empty array?
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context);
$entity_output = $this->renderer->render($build);

return $entity_output;
}

/**
* {@inheritdoc}
*/
public function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) {
// Build the display plugin.
/** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */
$display = $this->displayPluginManager->createInstance($plugin_id, $plugin_configuration);
$display->setContextValue('entity', $entity);
$display->setAttributes($context);

// Check if the display plugin is accessible. This also checks entity
// access, which is why we never call $entity->access() here.
if (!$display->access()) {
return array();
}

return $display->build();
}
}
71 changes: 71 additions & 0 deletions src/EntityEmbedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @file
* Contains Drupal\entity_embed\EntityEmbedInterface.
*/

namespace Drupal\entity_embed;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;

/**
* A service class for handling entity embeds.
*
* @todo Add more documentation.
*/
interface EntityEmbedInterface {

const DEFAULT_DISPLAY_ID = 'entity_reference:entity_reference_entity_view';

/**
* Constructs a EntityEmbedService object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager
* The entity embed display plugin manager.
*/
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer, EntityEmbedDisplayManager $display_plugin_manager);

/**
* Renders an embedded entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param array $context
* (optional) Array of context values, corresponding to the attributes on
* the embed HTML tag.
*
* @return string
* The HTML of the entity rendered with the display plugin.
*/
public function renderEntityEmbed(EntityInterface $entity, array $context = array());

/**
* Renders an entity using an EntityEmbedDisplay plugin.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param string $plugin_id
* The EntityEmbedDisplay plugin ID.
* @param array $plugin_configuration
* (optional) Array of plugin configuration values.
* @param array $context
* (optional) Array of additional context values, usually the embed HTML
* tag's attributes.
*
* @return array
* A render array for the display plugin.
*/
public function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array());

}
168 changes: 0 additions & 168 deletions src/EntityHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@ trait EntityHelperTrait {
*/
protected $moduleHandler;

/**
* The display plugin manager.
*
* @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager.
*/
protected $displayPluginManager;

/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface.
*/
protected $renderer;

/**
* Loads an entity from the database.
*
Expand Down Expand Up @@ -130,108 +116,6 @@ protected function canRenderEntityType($entity_type) {
return $this->entityManager()->hasHandler($entity_type, 'view_builder');
}

/**
* Returns the render array for an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param string $view_mode
* The view mode that should be used to display the entity.
* @param string $langcode
* (optional) For which language the entity should be rendered, defaults to
* the current content language.
*
* @return array
* A render array for the entity.
*/
protected function renderEntity(EntityInterface $entity, $view_mode, $langcode = NULL) {
$render_controller = $this->entityManager()->getViewBuilder($entity->getEntityTypeId());
return $render_controller->view($entity, $view_mode, $langcode);
}

/**
* Renders an embedded entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param array $context
* (optional) Array of context values, corresponding to the attributes on
* the embed HTML tag.
*
* @return string
* The HTML of the entity rendered with the display plugin.
*/
protected function renderEntityEmbed(EntityInterface $entity, array $context = array()) {
// Support the deprecated view-mode data attribute.
if (isset($context['data-view-mode']) && !isset($context['data-entity-embed-display']) && !isset($context['data-entity-embed-settings'])) {
$context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
$context['data-entity-embed-settings'] = ['view_mode' => &$context['data-view-mode']];
}

// Merge in default attributes.
$context += array(
'data-entity-id' => $entity->id(),
'data-entity-type' => $entity->getEntityTypeId(),
'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view',
'data-entity-embed-settings' => array(),
);

// The default display plugin has been deprecated by the rendered entity
// field formatter.
if ($context['data-entity-embed-display'] === 'default') {
$context['data-entity-embed-display'] = 'entity_reference:entity_reference_entity_view';
}

// Allow modules to alter the entity prior to embed rendering.
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed_context", 'entity_embed_context'), $context, $entity);

// Build and render the display plugin, allowing modules to alter the
// result before rendering.
$build = $this->renderEntityEmbedDisplayPlugin(
$entity,
$context['data-entity-embed-display'],
$context['data-entity-embed-settings'],
$context
);
// @todo Should this hook get invoked if $build is an empty array?
$this->moduleHandler()->alter(array("{$context['data-entity-type']}_embed", 'entity_embed'), $build, $entity, $context);
$entity_output = $this->renderer()->render($build);

return $entity_output;
}

/**
* Renders an entity using an EntityEmbedDisplay plugin.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be rendered.
* @param string $plugin_id
* The EntityEmbedDisplay plugin ID.
* @param array $plugin_configuration
* (optional) Array of plugin configuration values.
* @param array $context
* (optional) Array of additional context values, usually the embed HTML
* tag's attributes.
*
* @return array
* A render array for the display plugin.
*/
protected function renderEntityEmbedDisplayPlugin(EntityInterface $entity, $plugin_id, array $plugin_configuration = array(), array $context = array()) {
// Build the display plugin.
/** @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayBase $display */
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_configuration);
$display->setContextValue('entity', $entity);
$display->setAttributes($context);

// Check if the display plugin is accessible. This also checks entity
// access, which is why we never call $entity->access() here.
if (!$display->access()) {
return array();
}

return $display->build();
}

/**
* Returns the entity manager.
*
Expand Down Expand Up @@ -283,56 +167,4 @@ public function setModuleHandler(ModuleHandlerInterface $module_handler) {
$this->moduleHandler = $module_handler;
return $this;
}

/**
* Returns the display plugin manager.
*
* @return \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
* The display plugin manager.
*/
protected function displayPluginManager() {
if (!isset($this->displayPluginManager)) {
$this->displayPluginManager = \Drupal::service('plugin.manager.entity_embed.display');
}
return $this->displayPluginManager;
}

/**
* Sets the display plugin manager service.
*
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager
* The display plugin manager service.
*
* @return self
*/
public function setDisplayPluginManager(EntityEmbedDisplayManager $display_plugin_manager) {
$this->displayPluginManager = $display_plugin_manager;
return $this;
}

/**
* Returns the renderer.
*
* @return \Drupal\Core\Render\RendererInterface
* The renderer.
*/
protected function renderer() {
if (!isset($this->renderer)) {
$this->renderer = \Drupal::service('renderer');
}
return $this->renderer;
}

/**
* Sets the renderer.
*
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
*
* @return self
*/
public function setRenderer(RendererInterface $renderer) {
$this->renderer = $renderer;
return $this;
}
}
Loading