Commit cc17745d authored by git's avatar git Committed by slashrsm

Issue #2760407 by Denchev, slashrsm, samuel.mortenson: Clean up...

Issue #2760407 by Denchev, slashrsm, samuel.mortenson: Clean up EntityHelperTrait and covert it to a service.
parent 37574368
......@@ -4,6 +4,9 @@ services:
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler']
entity_embed.twig.entity_embed_twig_extension:
class: Drupal\entity_embed\Twig\EntityEmbedTwigExtension
arguments: ['@entity.manager', '@module_handler', '@plugin.manager.entity_embed.display']
arguments: ['@entity.manager', '@entity_embed.builder']
tags:
- { name: twig.extension }
entity_embed.builder:
class: Drupal\entity_embed\EntityEmbedBuilder
arguments: ['@module_handler', '@plugin.manager.entity_embed.display']
<?php
namespace Drupal\entity_embed;
use Drupal\Core\Entity\EntityInterface;
/**
* Defines services that are responsible for building of embedded entities.
*
* @internal
*/
interface EntityEmbedBuilderInterface {
/**
* Builds the render array for 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 array
* A render array.
*
* @todo improve documentation
*/
public function buildEntityEmbed(EntityInterface $entity, array $context = []);
}
......@@ -4,13 +4,14 @@ namespace Drupal\entity_embed\EntityEmbedDisplay;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_embed\EntityHelperTrait;
use Drupal\entity_embed\EntityEmbedHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -24,7 +25,20 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @ingroup entity_embed_api
*/
abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface {
use EntityHelperTrait;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface $language_manager
*/
protected $languageManager;
/**
* The context for the plugin.
......@@ -43,13 +57,16 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac
/**
* {@inheritdoc}
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
$this->setEntityManager($entity_manager);
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
/**
......@@ -60,7 +77,8 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')
$container->get('entity_type.manager'),
$container->get('language_manager')
);
}
......@@ -92,7 +110,7 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac
// First, determine whether or not the entity type id is valid. Return FALSE
// if the specified id is not valid.
$entity_type = $this->getEntityTypeFromContext();
if (!$this->entityManager()->getDefinition($entity_type)) {
if (!$this->entityTypeManager->getDefinition($entity_type)) {
return FALSE;
}
......@@ -308,8 +326,9 @@ abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFac
public function getLangcode() {
$langcode = $this->getAttributeValue('data-langcode');
if (empty($langcode)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
$langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
}
return $langcode;
}
}
......@@ -3,10 +3,11 @@
namespace Drupal\entity_embed\EntityEmbedDisplay;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\PluginDependencyTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\TypedDataManager;
......@@ -47,19 +48,20 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa
/**
* Constructs a FieldFormatterEntityEmbedDisplayBase object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
* The field formatter plugin manager.
* @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager
* The typed data manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, LanguageManagerInterface $language_manager) {
$this->formatterPluginManager = $formatter_plugin_manager;
$this->setConfiguration($configuration);
$this->setEntityManager($entity_manager);
$this->typedDataManager = $typed_data_manager;
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $language_manager);
}
/**
......@@ -70,9 +72,10 @@ abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBa
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('entity_type.manager'),
$container->get('plugin.manager.field.formatter'),
$container->get('typed_data_manager')
$container->get('typed_data_manager'),
$container->get('language_manager')
);
}
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Form\EntityEmbedDialog.
*/
namespace Drupal\entity_embed\Form;
use Drupal\Component\Utility\Html;
......@@ -13,8 +8,10 @@ use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\SetDialogTitleCommand;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -24,7 +21,6 @@ use Drupal\embed\EmbedButtonInterface;
use Drupal\entity_browser\Events\Events;
use Drupal\entity_browser\Events\RegisterJSCallbacks;
use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
use Drupal\entity_embed\EntityHelperTrait;
use Drupal\Component\Serialization\Json;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -33,7 +29,13 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
* Provides a form to embed entities by specifying data attributes.
*/
class EntityEmbedDialog extends FormBase {
use EntityHelperTrait;
/**
* The entity embed display manager.
*
* @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
*/
protected $entityEmbedDisplayManager;
/**
* The form builder.
......@@ -63,6 +65,20 @@ class EntityEmbedDialog extends FormBase {
*/
protected $entityBrowser;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entityFieldManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity browser settings from the entity embed button.
*/
......@@ -71,7 +87,7 @@ class EntityEmbedDialog extends FormBase {
/**
* Constructs a EntityEmbedDialog object.
*
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $plugin_manager
* @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $entity_embed_display_manager
* The Module Handler.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The Form Builder.
......@@ -79,12 +95,18 @@ class EntityEmbedDialog extends FormBase {
* The entity type manager service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(EntityEmbedDisplayManager $plugin_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher) {
$this->setDisplayPluginManager($plugin_manager);
public function __construct(EntityEmbedDisplayManager $entity_embed_display_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler) {
$this->entityEmbedDisplayManager = $entity_embed_display_manager;
$this->formBuilder = $form_builder;
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
$this->entityFieldManager = $entity_field_manager;
$this->moduleHandler = $module_handler;
}
/**
......@@ -95,7 +117,9 @@ class EntityEmbedDialog extends FormBase {
$container->get('plugin.manager.entity_embed.display'),
$container->get('form_builder'),
$container->get('entity_type.manager'),
$container->get('event_dispatcher')
$container->get('event_dispatcher'),
$container->get('entity_field.manager'),
$container->get('module_handler')
);
}
......@@ -137,7 +161,9 @@ class EntityEmbedDialog extends FormBase {
'data-entity-embed-settings' => array(),
);
$form_state->set('entity_element', $entity_element);
$form_state->set('entity', $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']));
$entity = $this->entityTypeManager->getStorage($entity_element['data-entity-type'])
->loadByProperties(['uuid' => $entity_element['data-entity-uuid']]);
$form_state->set('entity', current($entity) ?: NULL);
if (!$form_state->get('step')) {
// If an entity has been selected, then always skip to the embed options.
......@@ -199,7 +225,7 @@ class EntityEmbedDialog extends FormBase {
// the label field definition.
$entity_type = $this->entityTypeManager->getDefinition($entity_element['data-entity-type']);
if ($entity_type->isSubclassOf('\Drupal\Core\Entity\FieldableEntityInterface') && $entity_type->hasKey('label')) {
$field_definitions = $this->entityManager()->getBaseFieldDefinitions($entity_type->id());
$field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type->id());
if (isset($field_definitions[$entity_type->getKey('label')])) {
$label = $field_definitions[$entity_type->getKey('label')]->getLabel();
}
......@@ -414,7 +440,7 @@ class EntityEmbedDialog extends FormBase {
if (is_string($entity_element['data-entity-embed-settings'])) {
$entity_element['data-entity-embed-settings'] = Json::decode($entity_element['data-entity-embed-settings']);
}
$display = $this->displayPluginManager()->createInstance($plugin_id, $entity_element['data-entity-embed-settings']);
$display = $this->entityEmbedDisplayManager->createInstance($plugin_id, $entity_element['data-entity-embed-settings']);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$form['attributes']['data-entity-embed-settings'] += $display->buildConfigurationForm($form, $form_state);
......@@ -510,7 +536,8 @@ class EntityEmbedDialog extends FormBase {
}
$entity_type = $form_state->getValue(['attributes', 'data-entity-type']);
if ($entity = $this->loadEntity($entity_type, $id)) {
if ($entity = $this->entityTypeManager->getStorage($entity_type)->load($id)) {
if (!$entity->access('view')) {
$form_state->setError($element, $this->t('Unable to access @type entity @id.', array('@type' => $entity_type, '@id' => $id)));
}
......@@ -550,10 +577,12 @@ class EntityEmbedDialog extends FormBase {
public function validateEmbedStep(array $form, FormStateInterface $form_state) {
// Validate configuration forms for the Entity Embed Display plugin used.
$entity_element = $form_state->getValue('attributes');
$entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']);
$entity = $this->entityTypeManager->getStorage($entity_element['data-entity-type'])
->loadByProperties(['uuid' => $entity_element['data-entity-uuid']]);
$entity = current($entity) ?: NULL;
$plugin_id = $entity_element['data-entity-embed-display'];
$plugin_settings = $entity_element['data-entity-embed-settings'] ?: array();
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings);
$display = $this->entityEmbedDisplayManager->createInstance($plugin_id, $plugin_settings);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$display->validateConfigurationForm($form, $form_state);
......@@ -705,10 +734,12 @@ class EntityEmbedDialog extends FormBase {
// Submit configuration form the selected Entity Embed Display plugin.
$entity_element = $form_state->getValue('attributes');
$entity = $this->loadEntity($entity_element['data-entity-type'], $entity_element['data-entity-uuid']);
$entity = $this->entityTypeManager->getStorage($entity_element['data-entity-type'])
->loadByProperties(['uuid' => $entity_element['data-entity-uuid']]);
$entity = current($entity);
$plugin_id = $entity_element['data-entity-embed-display'];
$plugin_settings = $entity_element['data-entity-embed-settings'] ?: array();
$display = $this->displayPluginManager()->createInstance($plugin_id, $plugin_settings);
$display = $this->entityEmbedDisplayManager->createInstance($plugin_id, $plugin_settings);
$display->setContextValue('entity', $entity);
$display->setAttributes($entity_element);
$display->submitConfigurationForm($form, $form_state);
......@@ -735,7 +766,7 @@ class EntityEmbedDialog extends FormBase {
});
// Allow other modules to alter the values before getting submitted to the WYSIWYG.
$this->moduleHandler()->alter('entity_embed_values', $values, $entity, $display, $form_state);
$this->moduleHandler->alter('entity_embed_values', $values, $entity, $display, $form_state);
$response->addCommand(new EditorDialogSave($values));
$response->addCommand(new CloseModalDialogCommand());
......@@ -769,7 +800,7 @@ class EntityEmbedDialog extends FormBase {
* List of allowed Entity Embed Display plugins.
*/
public function getDisplayPluginOptions(EmbedButtonInterface $embed_button, EntityInterface $entity) {
$plugins = $this->displayPluginManager->getDefinitionOptionsForEntity($entity);
$plugins = $this->entityEmbedDisplayManager->getDefinitionOptionsForEntity($entity);
if ($allowed_plugins = $embed_button->getTypeSetting('display_plugins')) {
$plugins = array_intersect_key($plugins, array_flip($allowed_plugins));
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter.
*/
namespace Drupal\entity_embed\Plugin\Filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\entity_embed\EntityHelperTrait;
use Drupal\entity_embed\EntityEmbedBuilderInterface;
use Drupal\entity_embed\Exception\EntityNotFoundException;
use Drupal\entity_embed\Exception\RecursiveRenderingException;
use Drupal\filter\FilterProcessResult;
......@@ -33,7 +27,7 @@ use Drupal\embed\DomHelperTrait;
* )
*/
class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInterface {
use EntityHelperTrait;
use DomHelperTrait;
/**
......@@ -43,6 +37,20 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte
*/
protected $renderer;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity embed builder service.
*
* @var \Drupal\entity_embed\EntityEmbedBuilderInterface
*/
protected $builder;
/**
* Constructs a EntityEmbedFilter object.
*
......@@ -52,16 +60,18 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The Module Handler.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer.
* @param \Drupal\entity_embed\EntityEmbedBuilderInterface $builder
* The entity embed builder service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, RendererInterface $renderer) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer, EntityEmbedBuilderInterface $builder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setEntityManager($entity_manager);
$this->setModuleHandler($module_handler);
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
$this->builder = $builder;
}
/**
......@@ -72,9 +82,9 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('module_handler'),
$container->get('renderer')
$container->get('entity_type.manager'),
$container->get('renderer'),
$container->get('entity_embed.builder')
);
}
......@@ -96,8 +106,17 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte
try {
// Load the entity either by UUID (preferred) or ID.
$id = $node->getAttribute('data-entity-uuid') ?: $node->getAttribute('data-entity-id');
$entity = $this->loadEntity($entity_type, $id);
$id = NULL;
$entity = NULL;
if ($id = $node->getAttribute('data-entity-uuid')) {
$entity = $this->entityTypeManager->getStorage($entity_type)
->loadByProperties(['uuid' => $id]);
$entity = current($entity);
}
else {
$id = $node->getAttribute('data-entity-id');
$entity = $this->entityTypeManager->getStorage($entity_type)->load($id);
}
if ($entity) {
// Protect ourselves from recursive rendering.
......@@ -114,7 +133,7 @@ class EntityEmbedFilter extends FilterBase implements ContainerFactoryPluginInte
$context = $this->getNodeAttributesAsArray($node);
$context += array('data-langcode' => $langcode);
$build = $this->buildEntityEmbed($entity, $context);
$build = $this->builder->buildEntityEmbed($entity, $context);
// We need to render the embedded entity:
// - without replacing placeholders, so that the placeholders are
// only replaced at the last possible moment. Hence we cannot use
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay\ImageFieldFormatter.
*/
namespace Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\TypedDataManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -33,16 +29,16 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class ImageFieldFormatter extends FileFieldFormatter {
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* {@inheritdoc}
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity manager service.
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
* The field formatter plugin manager.
......@@ -50,9 +46,11 @@ class ImageFieldFormatter extends FileFieldFormatter {
* The typed data manager.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, ImageFactory $image_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager, $formatter_plugin_manager, $typed_data_manager);
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, ImageFactory $image_factory, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $formatter_plugin_manager, $typed_data_manager, $language_manager);
$this->imageFactory = $image_factory;
}
......@@ -64,10 +62,11 @@ class ImageFieldFormatter extends FileFieldFormatter {
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager'),
$container->get('entity_type.manager'),
$container->get('plugin.manager.field.formatter'),
$container->get('typed_data_manager'),
$container->get('image.factory')
$container->get('image.factory'),
$container->get('language_manager')
);
}
......
......@@ -110,7 +110,7 @@ class EntityEmbedDialogTest extends EntityEmbedTestBase {
$this->container->get('config.factory')->getEditable('entity_embed.settings')
->set('rendered_entity_mode', TRUE)->save();
$this->displayPluginManager()->clearCachedDefinitions();
$this->container->get('plugin.manager.entity_embed.display')->clearCachedDefinitions();
$this->getEmbedDialog('custom_format', 'node');
$title = $this->node->getTitle() . ' (' . $this->node->id() . ')';
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Tests\EntityEmbedHooksTest.
*/
namespace Drupal\entity_embed\Tests;
/**
......@@ -34,7 +29,8 @@ class EntityEmbedHooksTest extends EntityEmbedTestBase {
// hook_entity_embed_display_plugins_alter() implementation and ensure it is
// working as designed.
$this->state->set('entity_embed_test_entity_embed_display_plugins_alter', TRUE);
$plugins = $this->displayPluginManager()->getDefinitionOptionsForEntity($this->node);
$plugins = $this->container->get('plugin.manager.entity_embed.display')
->getDefinitionOptionsForEntity($this->node);
// Ensure that name of each plugin is prefixed with 'testing_hook:'.
foreach ($plugins as $plugin => $plugin_info) {
$this->assertTrue(strpos($plugin, 'testing_hook:') === 0, 'Name of the plugin is prefixed by hook_entity_embed_display_plugins_alter()');
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Tests\EntityEmbedTestBase.
*/
namespace Drupal\entity_embed\Tests;
use Drupal\Core\Entity\EntityInterface;
use Drupal\editor\Entity\Editor;
use Drupal\entity_embed\EntityHelperTrait;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\simpletest\WebTestBase;
......@@ -18,7 +12,6 @@ use Drupal\simpletest\WebTestBase;
* Base class for all entity_embed tests.
*/
abstract class EntityEmbedTestBase extends WebTestBase {
use EntityHelperTrait;
/**
* Modules to enable.
......@@ -111,10 +104,12 @@ abstract class EntityEmbedTestBase extends WebTestBase {
}
public function assertAvailableDisplayPlugins(EntityInterface $entity, array $expected_plugins, $message = '') {
$plugin_options = $this->displayPluginManager()->getDefinitionOptionsForEntity($entity);
$plugin_options = $this->container->get('plugin.manager.entity_embed.display')
->getDefinitionOptionsForEntity($entity);
// @todo Remove the sorting so we can actually test return order.
ksort($plugin_options);
sort($expected_plugins);
$this->assertEqual(array_keys($plugin_options), $expected_plugins, $message);
}
}
......@@ -47,7 +47,7 @@ class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
$this->container->get('config.factory')->getEditable('entity_embed.settings')
->set('rendered_entity_mode', TRUE)->save();
$this->displayPluginManager()->clearCachedDefinitions();
$this->container->get('plugin.manager.entity_embed.display')->clearCachedDefinitions();
$this->assertAvailableDisplayPlugins($this->node, [
'entity_reference:entity_reference_label',
......@@ -59,7 +59,7 @@ class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
// 'entity_reference:entity_reference_entity_id' plugin.
$form = array();
$form_state = new FormState();
$display = $this->displayPluginManager()->createInstance('entity_reference:entity_reference_entity_id', array());
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_entity_id', array());
$display->setContextValue('entity', $this->node);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical(array_keys($conf_form), array());
......@@ -68,7 +68,7 @@ class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
// 'entity_reference:entity_reference_entity_view' plugin.
$form = array();
$form_state = new FormState();
$display = $this->displayPluginManager()->createInstance('entity_reference:entity_reference_entity_view', array());
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_entity_view', array());
$display->setContextValue('entity', $this->node);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical($conf_form['view_mode']['#type'], 'select');
......@@ -78,7 +78,7 @@ class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
// 'entity_reference:entity_reference_label' plugin.
$form = array();
$form_state = new FormState();
$display = $this->displayPluginManager()->createInstance('entity_reference:entity_reference_label', array());
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_label', array());
$display->setContextValue('entity', $this->node);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical(array_keys($conf_form), array('link'));
......@@ -87,7 +87,7 @@ class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
// Ensure that 'Rendered Entity' plugin is not available for an entity not
// having a view controller.
$plugin_options = $this->displayPluginManager()->getDefinitionOptionsForEntity($this->menu);
$plugin_options = $this->container->get('plugin.manager.entity_embed.display')->getDefinitionOptionsForEntity($this->menu);
$this->assertFalse(array_key_exists('entity_reference:entity_reference_entity_view', $plugin_options), "The 'Rendered entity' plugin is not available.");
}
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Tests\FileFieldFormatterTest.
*/
namespace Drupal\entity_embed\Tests;
use Drupal\Component\Serialization\Json;
......@@ -60,7 +55,8 @@ class FileFieldFormatterTest extends EntityEmbedTestBase {
);
// Ensure that description field is available for all the 'file' plugins.
foreach ($plugins as $plugin) {
$display = $this->displayPluginManager()->createInstance($plugin, array());
$display = $this->container->get('plugin.manager.entity_embed.display')
->createInstance($plugin, []);
$display->setContextValue('entity', $this->file);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical(array_keys($conf_form), array('description'));
......
<?php
/**
* @file
* Contains \Drupal\entity_embed\Tests\ImageFieldFormatterTest.
*/
namespace Drupal\entity_embed\Tests;
use Drupal\Component\Serialization\Json;
......@@ -62,7 +57,8 @@ class ImageFieldFormatterTest extends EntityEmbedTestBase {
// Ensure that correct form attributes are returned for the image plugin.
$form = array();
$form_state = new FormState();
$display = $this->displayPluginManager()->createInstance('image:image', array());
$display = $this->container->get('plugin.manager.entity_embed.display')
->createInstance('image:image', []);
$display->setContextValue('entity', $this->image);
$conf_form = $display->buildConfigurationForm($form, $form_state);
$this->assertIdentical(array_keys($conf_form), array(
......
......@@ -27,7 +27,8 @@ class ViewModeFieldFormatterTest extends EntityEmbedTestBase {
foreach ($this->plugins as $plugin) {