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

Convert plugins to use ContextAwarePluginBase #176

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
55 changes: 19 additions & 36 deletions src/EntityEmbedDisplayBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,22 @@

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\Core\Session\AccountInterface;

/**
* Defines a base display implementation that most display plugins will extend.
*
* @ingroup entity_embed_api
*/
abstract class EntityEmbedDisplayBase extends PluginBase implements EntityEmbedDisplayInterface {
abstract class EntityEmbedDisplayBase extends ContextAwarePluginBase implements EntityEmbedDisplayInterface {

/**
* The entity being embedded.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
public $entity;

/**
* The context for the embedded entity.
*
* @var array
*/
public $context = array();
/**
* The attributes on the embedded entity.
*
* @var array
*/
public $attributes = array();

/**
* {@inheritdoc}
Expand All @@ -41,32 +34,22 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->setConfiguration($configuration);
}

public function setEntity(EntityInterface $entity) {
$this->entity = $entity;
return $this;
}

public function getEntity() {
return $this->entity;
}

public function setContext(array $context) {
$this->context = $context;
return $this;
public function getConfigurationValue($name, $default = NULL) {
$configuration = $this->getConfiguration();
return array_key_exists($name, $configuration) ? $configuration[$name] : $default;
}

public function getContext() {
return $this->context;
public function setAttributes(array $attributes) {
$this->attributes = $attributes;
}

public function getContextValue($name, $default = NULL) {
$context = $this->getContext();
return array_key_exists($name, $context) ? $context[$name] : $default;
public function getAttributes() {
return $this->attributes;
}

public function getConfigurationValue($name, $default = NULL) {
$configuration = $this->getConfiguration();
return array_key_exists($name, $configuration) ? $configuration[$name] : $default;
public function getAttributeValue($name, $default = NULL) {
$attributes = $this->getAttributes();
return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
}

/**
Expand All @@ -82,7 +65,7 @@ public function access(AccountInterface $account = NULL) {
}

// Check that the entity itself can be viewed by the user.
return $this->entity->access('view', $account);
return $this->getContextValue('entity')->access('view', $account);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/EntityEmbedDisplayManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Component\Plugin\Exception\PluginException;

/**
* Provides an Entity embed display plugin manager.
Expand Down Expand Up @@ -69,9 +70,14 @@ public function processDefinition(&$definition, $plugin_id) {
public function getDefinitionsByEntity(EntityInterface $entity) {
$definitions = $this->getDefinitions();
$valid_ids = array_filter(array_keys($definitions), function ($id) use ($entity) {
$display = $this->createInstance($id);
$display->setEntity($entity);
return $display->access();
try {
$display = $this->createInstance($id);
$display->setContextValue('entity', $entity);
return $display->access();
}
catch (PluginException $e) {
return FALSE;
}
});
return array_intersect_key($definitions, array_flip($valid_ids));
}
Expand Down
6 changes: 3 additions & 3 deletions src/FieldFormatterEntityEmbedDisplayBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getFormatter(FieldDefinition $definition = NULL) {
}

// Ensure that the field name is unique each time this is run.
$definition->setName('_entity_embed_' . $this->getContextValue('token'));
$definition->setName('_entity_embed_' . $this->getAttributeValue('token'));

$display = array(
'type' => $this->getDerivativeId(),
Expand Down Expand Up @@ -61,7 +61,7 @@ public function build() {
// Field name is only set to avoid broken CSS classes.
$definition = $this->getFieldDefinition();
// Ensure that the field name is unique each time this is run.
$definition->setName('_entity_embed_' . $this->getContextValue('token'));
$definition->setName('_entity_embed_' . $this->getAttributeValue('token'));

/* @var \Drupal\Core\Field\FieldItemListInterface $items $items */
// Create a field item list object, 1 is the value, array('target_id' => 1)
Expand All @@ -74,7 +74,7 @@ public function build() {
$node
);

if ($langcode = $this->getContextValue('langcode')) {
if ($langcode = $this->getAttributeValue('langcode')) {
$items->setLangcode($langcode);
}

Expand Down
15 changes: 11 additions & 4 deletions src/Plugin/EntityEmbedDisplay/EntityEmbedDefaultDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
*
* @EntityEmbedDisplay(
* id = "default",
* label = @Translation("Default")
* label = @Translation("Default"),
* context = {
* "entity" = {
* "type" = "entity"
* }
* }
* )
*
* @todo Should this use a derivative? http://cgit.drupalcode.org/page_manager/tree/src/Plugin/Deriver/EntityViewDeriver.php
*/
class EntityEmbedDefaultDisplay extends EntityEmbedDisplayBase {

Expand All @@ -37,7 +44,7 @@ public function buildConfigurationForm(array $form, array &$form_state) {
$form['view_mode'] = array(
'#type' => 'select',
'#title' => t('View mode'),
'#options' => \Drupal::entityManager()->getDisplayModeOptions('view_mode', $this->entity->getEntityTypeId()),
'#options' => \Drupal::entityManager()->getDisplayModeOptions('view_mode', $this->getAttributeValue('entity-type')),
'#default_value' => $this->getConfigurationValue('view_mode'),
'#required' => TRUE,
);
Expand All @@ -52,10 +59,10 @@ public function build() {
// Clone the entity since we're going to set some additional properties we
// don't want kept around afterwards.
$entity = clone $this->entity;
$entity->entity_embed_context = $this->getContext();
$entity->entity_embed_attributes = $this->getAttributes();

// Build the rendered entity.
$build = entity_view($this->entity, $this->getConfigurationValue('view_mode'), $this->getContextValue('langcode'));
$build = entity_view($this->entity, $this->getConfigurationValue('view_mode'), $this->getAttributeValue('langcode'));

// Hide entity links by default.
if (isset($build['links'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* @EntityEmbedDisplay(
* id = "entity_reference",
* label = @Translation("Entity Reference"),
* context = {
* "entity" = {
* "type" = "entity"
* }
* },
* derivative = "Drupal\entity_embed\Plugin\Derivative\FieldFormatterDeriver",
* field_type = "entity_reference",
* provider = "entity_reference"
Expand All @@ -28,15 +33,15 @@ class EntityReferenceFieldFormatter extends FieldFormatterEntityEmbedDisplayBase
*/
public function getFieldDefinition() {
$field = FieldDefinition::create('entity_reference');
$field->setSetting('target_type', $this->getContextValue('entity-type'));
$field->setSetting('target_type', $this->getAttributeValue('entity-type'));
return $field;
}

/**
* {@inheritdoc}
*/
public function getFieldValue(FieldDefinition $definition) {
return array('target_id' => $this->entity->id());
return array('target_id' => $this->getContextValue('entity')->id());
}

}
14 changes: 12 additions & 2 deletions src/Plugin/EntityEmbedDisplay/FileFieldFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* @EntityEmbedDisplay(
* id = "file",
* label = @Translation("File"),
* context = {
* "entity" = {
* "type" = "entity:file"
* }
* },
* entity_types = {"file"},
* derivative = "Drupal\entity_embed\Plugin\Derivative\FieldFormatterDeriver",
* field_type = "file",
Expand All @@ -37,25 +42,30 @@ public function getFieldDefinition() {
*/
public function getFieldValue(FieldDefinition $definition) {
$value = parent::getFieldValue($definition);
$value += array_intersect_key($this->getContext(), array('description' => ''));
$value += array_intersect_key($this->getAttributes(), array('description' => ''));
return $value;
}

/**
* {@inheritdoc}
*/
public function access(AccountInterface $account = NULL) {
<<<<<<< HEAD
// Due to issues with access checking with file entities in core, we must
// manually check hook_file_download to see if the user can access the file.
=======
if (!$this->isValidEntityType()) {
return FALSE;
}

// Due to issues with access checking with file entities in core, we cannot
// actually use Entity::access() which would have been called by
// parent::access().
>>>>>>> embed-display
// @see https://drupal.org/node/2128791
// @see https://drupal.org/node/2148353
// @see https://drupal.org/node/2078473
switch (file_uri_scheme($this->entity->getFileUri())) {
switch (file_uri_scheme($this->getContextValue('entity')->getFileUri())) {
case 'public':
return TRUE;

Expand Down
14 changes: 10 additions & 4 deletions src/Plugin/EntityEmbedDisplay/ImageFieldFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* @EntityEmbedDisplay(
* id = "image",
* label = @Translation("Image"),
* context = {
* "entity" = {
* "type" = "entity:file"
* }
* },
* entity_types = {"file"},
* derivative = "Drupal\entity_embed\Plugin\Derivative\FieldFormatterDeriver",
* field_type = "image",
Expand All @@ -39,7 +44,7 @@ public function getFieldValue(FieldDefinition $definition) {
$value = parent::getFieldValue($definition);
// File field support descriptions, but images do not.
unset($value['description']);
$value += array_intersect_key($this->getContext(), array('alt' => '', 'title' => ''));
$value += array_intersect_key($this->getAttributes(), array('alt' => '', 'title' => ''));
return $value;
}

Expand All @@ -51,7 +56,8 @@ public function access(AccountInterface $account = NULL) {
return FALSE;
}

$image = \Drupal::service('image.factory')->get($this->entity->getFileUri());
$uri = $this->getContextValue('entity')->getFileUri();
$image = \Drupal::service('image.factory')->get($uri);
return $image->isSupported();
}

Expand All @@ -74,7 +80,7 @@ public function buildConfigurationForm(array $form, array &$form_state) {
$form['alt'] = array(
'#type' => 'textfield',
'#title' => $this->t('Alternate text'),
'#default_value' => $this->getContextValue('alt', ''),
'#default_value' => $this->getAttributeValue('alt', ''),
'#description' => $this->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'),
'#parents' => array('attributes', 'alt'),
// @see http://www.gawds.org/show.php?contentid=28
Expand All @@ -83,7 +89,7 @@ public function buildConfigurationForm(array $form, array &$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#default_value' => $this->getContextValue('title', ''),
'#default_value' => $this->getAttributeValue('title', ''),
'#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
'#parents' => array('attributes', 'title'),
'#maxlength' => 1024,
Expand Down
4 changes: 2 additions & 2 deletions src/Plugin/Filter/EntityEmbedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public static function postRender(array $element, array $context) {
if ($entity = entity_load($context['entity-type'], $context['entity-id'])) {
$manager = \Drupal::service('plugin.manager.entity_embed.display');
$display = $manager->createInstance($context['entity-embed-display'], $context['entity-embed-settings']);
$display->setEntity($entity);
$display->setContext($context);
$display->setContextValue('entity', $entity);
$display->setAttributes($context);
if ($display->access()) {
$build = $display->build();
// Allow modules to alter the rendered embedded entity.
Expand Down