3 namespace Drupal\entity_embed\EntityEmbedDisplay;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\Core\Field\FormatterPluginManager;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Plugin\PluginDependencyTrait;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\Core\TypedData\TypedDataManager;
14 use Drupal\node\Entity\Node;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
20 abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBase {
21 use PluginDependencyTrait;
24 * The field formatter plugin manager.
26 * @var \Drupal\Core\Field\FormatterPluginManager
28 protected $formatterPluginManager;
31 * The typed data manager.
33 * @var \Drupal\Core\TypedData\TypedDataManager
35 protected $typedDataManager;
38 * The field definition.
40 * @var \Drupal\Core\Field\BaseFieldDefinition
42 protected $fieldDefinition;
45 * The field formatter.
47 * @var \Drupal\Core\Field\FormatterInterface
49 protected $fieldFormatter;
52 * Constructs a FieldFormatterEntityEmbedDisplayBase object.
54 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
55 * The entity type manager service.
56 * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
57 * The field formatter plugin manager.
58 * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager
59 * The typed data manager.
60 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
61 * The language manager.
63 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, LanguageManagerInterface $language_manager) {
64 $this->formatterPluginManager = $formatter_plugin_manager;
65 $this->setConfiguration($configuration);
66 $this->typedDataManager = $typed_data_manager;
67 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $language_manager);
73 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 $container->get('entity_type.manager'),
79 $container->get('plugin.manager.field.formatter'),
80 $container->get('typed_data_manager'),
81 $container->get('language_manager')
86 * Get the FieldDefinition object required to render this field's formatter.
88 * @return \Drupal\Core\Field\BaseFieldDefinition
89 * The field definition.
91 * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase::build()
93 public function getFieldDefinition() {
94 if (!isset($this->fieldDefinition)) {
95 $field_type = $this->getPluginDefinition()['field_type'];
96 $this->fieldDefinition = BaseFieldDefinition::create($field_type);
97 // Ensure the field name is unique for each Entity Embed Display plugin
100 $this->fieldDefinition->setName('_entity_embed_' . $index++);
102 return $this->fieldDefinition;
106 * Get the field value required to pass into the field formatter.
111 abstract public function getFieldValue();
116 public function access(AccountInterface $account = NULL) {
117 return parent::access($account)->andIf($this->isApplicableFieldFormatter());
121 * Checks if the field formatter is applicable.
123 * @return \Drupal\Core\Access\AccessResult
124 * Returns the access result.
126 protected function isApplicableFieldFormatter() {
127 $definition = $this->formatterPluginManager->getDefinition($this->getFieldFormatterId());
128 return AccessResult::allowedIf($definition['class']::isApplicable($this->getFieldDefinition()));
132 * Returns the field formatter id.
134 * @return string|null
135 * Returns field formatter id or null.
137 public function getFieldFormatterId() {
138 return $this->getDerivativeId();
144 public function build() {
145 // Create a temporary node object to which our fake field value can be
147 $node = Node::create(array('type' => '_entity_embed'));
149 $definition = $this->getFieldDefinition();
151 /* @var \Drupal\Core\Field\FieldItemListInterface $items $items */
152 // Create a field item list object, 1 is the value, array('target_id' => 1)
153 // would work too, or multiple values. 1 is passed down from the list to the
154 // field item, which knows that an integer is the ID.
155 $items = $this->typedDataManager->create(
157 $this->getFieldValue($definition),
158 $definition->getName(),
159 $node->getTypedData()
162 // Prepare, expects an array of items, keyed by parent entity ID.
163 $formatter = $this->getFieldFormatter();
164 $formatter->prepareView(array($node->id() => $items));
165 $build = $formatter->viewElements($items, $this->getLangcode());
166 // For some reason $build[0]['#printed'] is TRUE, which means it will fail
167 // to render later. So for now we manually fix that.
168 // @todo Investigate why this is needed.
176 public function defaultConfiguration() {
177 return $this->formatterPluginManager->getDefaultSettings($this->getFieldFormatterId());
183 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
184 return $this->getFieldFormatter()->settingsForm($form, $form_state);
188 * Constructs a field formatter.
190 * @return \Drupal\Core\Field\FormatterInterface
191 * The formatter object.
193 public function getFieldFormatter() {
194 if (!isset($this->fieldFormatter)) {
196 'type' => $this->getFieldFormatterId(),
197 'settings' => $this->getConfiguration(),
201 // Create the formatter plugin. Will use the default formatter for that
202 // field type if none is passed.
203 $this->fieldFormatter = $this->formatterPluginManager->getInstance(
205 'field_definition' => $this->getFieldDefinition(),
206 'view_mode' => '_entity_embed',
207 'configuration' => $display,
212 return $this->fieldFormatter;
216 * Creates a new faux-field definition.
218 * @param string $type
219 * The type of the field.
221 * @return \Drupal\Core\Field\BaseFieldDefinition
222 * A new field definition.
224 protected function createFieldDefinition($type) {
225 $definition = BaseFieldDefinition::create($type);
227 $definition->setName('_entity_embed_' . $index++);
234 public function calculateDependencies() {
235 $this->addDependencies(parent::calculateDependencies());
237 $definition = $this->formatterPluginManager->getDefinition($this->getFieldFormatterId());
238 $this->addDependency('module', $definition['provider']);
239 // @todo Investigate why this does not work currently.
240 // $this->calculatePluginDependencies($this->getFieldFormatter());
241 return $this->dependencies;