1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains \Drupal\entity_embed\EntityEmbedDisplay\FieldFormatterEntityEmbedDisplayBase. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\entity_embed\EntityEmbedDisplay; |
9
|
|
|
|
10
|
|
|
use Drupal\Core\Access\AccessResult; |
11
|
|
|
use Drupal\Core\Entity\EntityManagerInterface; |
12
|
|
|
use Drupal\Core\Field\BaseFieldDefinition; |
13
|
|
|
use Drupal\Core\Field\FormatterPluginManager; |
14
|
|
|
use Drupal\Core\Form\FormStateInterface; |
15
|
|
|
use Drupal\Core\Plugin\PluginDependencyTrait; |
16
|
|
|
use Drupal\Core\Session\AccountInterface; |
17
|
|
|
use Drupal\Core\TypedData\TypedDataManager; |
18
|
|
|
use Drupal\node\Entity\Node; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
20
|
|
|
|
21
|
|
|
abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBase { |
22
|
|
|
use PluginDependencyTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The field formatter plugin manager. |
26
|
|
|
* |
27
|
|
|
* @var \Drupal\Core\Field\FormatterPluginManager |
28
|
|
|
*/ |
29
|
|
|
protected $formatterPluginManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The typed data manager. |
33
|
|
|
* |
34
|
|
|
* @var \Drupal\Core\TypedData\TypedDataManager |
35
|
|
|
*/ |
36
|
|
|
protected $typedDataManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The field definition. |
40
|
|
|
* |
41
|
|
|
* @var \Drupal\Core\Field\BaseFieldDefinition |
42
|
|
|
*/ |
43
|
|
|
protected $fieldDefinition; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The field formatter. |
47
|
|
|
* |
48
|
|
|
* @var \Drupal\Core\Field\FormatterInterface |
49
|
|
|
*/ |
50
|
|
|
protected $fieldFormatter; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructs a FieldFormatterEntityEmbedDisplayBase object. |
54
|
|
|
* |
55
|
|
|
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager |
56
|
|
|
* The entity manager service. |
57
|
|
|
* @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager |
58
|
|
|
* The field formatter plugin manager. |
59
|
|
|
* @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager |
60
|
|
|
* The typed data manager. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager) { |
63
|
|
|
$this->formatterPluginManager = $formatter_plugin_manager; |
64
|
|
|
$this->setConfiguration($configuration); |
65
|
|
|
$this->setEntityManager($entity_manager); |
66
|
|
|
$this->typedDataManager = $typed_data_manager; |
67
|
|
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
|
|
|
74
|
|
|
return new static( |
75
|
|
|
$configuration, |
76
|
|
|
$plugin_id, |
77
|
|
|
$plugin_definition, |
78
|
|
|
$container->get('entity.manager'), |
79
|
|
|
$container->get('plugin.manager.field.formatter'), |
80
|
|
|
$container->get('typed_data_manager') |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the FieldDefinition object required to render this field's formatter. |
86
|
|
|
* |
87
|
|
|
* @return \Drupal\Core\Field\BaseFieldDefinition |
88
|
|
|
* The field definition. |
89
|
|
|
* |
90
|
|
|
* @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase::build() |
91
|
|
|
*/ |
92
|
|
|
public function getFieldDefinition() { |
93
|
|
|
if (!isset($this->fieldDefinition)) { |
94
|
|
|
$field_type = $this->getPluginDefinition()['field_type']; |
95
|
|
|
$this->fieldDefinition = BaseFieldDefinition::create($field_type); |
96
|
|
|
// Ensure the field name is unique for each Entity Embed Display plugin |
97
|
|
|
// instance. |
98
|
|
|
static $index = 0; |
99
|
|
|
$this->fieldDefinition->setName('_entity_embed_' . $index++); |
100
|
|
|
} |
101
|
|
|
return $this->fieldDefinition; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the field value required to pass into the field formatter. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
* The field value. |
109
|
|
|
*/ |
110
|
|
|
abstract public function getFieldValue(); |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function access(AccountInterface $account = NULL) { |
116
|
|
|
return parent::access($account)->andIf($this->isApplicableFieldFormatter()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checks if the field formatter is applicable. |
121
|
|
|
* |
122
|
|
|
* @return \Drupal\Core\Access\AccessResult |
123
|
|
|
* Returns the access result. |
124
|
|
|
*/ |
125
|
|
|
protected function isApplicableFieldFormatter() { |
126
|
|
|
$definition = $this->formatterPluginManager->getDefinition($this->getDerivativeId()); |
127
|
|
|
return AccessResult::allowedIf($definition['class']::isApplicable($this->getFieldDefinition())); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function build() { |
134
|
|
|
// Create a temporary node object to which our fake field value can be |
135
|
|
|
// added. |
136
|
|
|
$node = Node::create(array('type' => '_entity_embed')); |
137
|
|
|
|
138
|
|
|
$definition = $this->getFieldDefinition(); |
139
|
|
|
|
140
|
|
|
/* @var \Drupal\Core\Field\FieldItemListInterface $items $items */ |
141
|
|
|
// Create a field item list object, 1 is the value, array('target_id' => 1) |
142
|
|
|
// would work too, or multiple values. 1 is passed down from the list to the |
143
|
|
|
// field item, which knows that an integer is the ID. |
144
|
|
|
$items = $this->typedDataManager->create( |
145
|
|
|
$definition, |
146
|
|
|
$this->getFieldValue($definition), |
147
|
|
|
$definition->getName(), |
148
|
|
|
$node->getTypedData() |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
// Prepare, expects an array of items, keyed by parent entity ID. |
152
|
|
|
$formatter = $this->getFieldFormatter(); |
153
|
|
|
$formatter->prepareView(array($node->id() => $items)); |
154
|
|
|
$build = $formatter->viewElements($items, $this->getLangcode()); |
155
|
|
|
// For some reason $build[0]['#printed'] is TRUE, which means it will fail |
156
|
|
|
// to render later. So for now we manually fix that. |
157
|
|
|
// @todo Investigate why this is needed. |
158
|
|
|
show($build[0]); |
159
|
|
|
return $build[0]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function defaultConfiguration() { |
166
|
|
|
return $this->formatterPluginManager->getDefaultSettings($this->getDerivativeId()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
173
|
|
|
return $this->getFieldFormatter()->settingsForm($form, $form_state); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Constructs a field formatter. |
178
|
|
|
* |
179
|
|
|
* @return \Drupal\Core\Field\FormatterInterface |
180
|
|
|
* The formatter object. |
181
|
|
|
*/ |
182
|
|
|
public function getFieldFormatter() { |
183
|
|
|
if (!isset($this->fieldFormatter)) { |
184
|
|
|
$display = array( |
185
|
|
|
'type' => $this->getDerivativeId(), |
186
|
|
|
'settings' => $this->getConfiguration(), |
187
|
|
|
'label' => 'hidden', |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
// Create the formatter plugin. Will use the default formatter for that |
191
|
|
|
// field type if none is passed. |
192
|
|
|
$this->fieldFormatter = $this->formatterPluginManager->getInstance( |
193
|
|
|
array( |
194
|
|
|
'field_definition' => $this->getFieldDefinition(), |
195
|
|
|
'view_mode' => '_entity_embed', |
196
|
|
|
'configuration' => $display, |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->fieldFormatter; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Creates a new faux-field definition. |
206
|
|
|
* |
207
|
|
|
* @param string $type |
208
|
|
|
* The type of the field. |
209
|
|
|
* |
210
|
|
|
* @return \Drupal\Core\Field\BaseFieldDefinition |
211
|
|
|
* A new field definition. |
212
|
|
|
*/ |
213
|
|
|
protected function createFieldDefinition($type) { |
214
|
|
|
$definition = BaseFieldDefinition::create($type); |
215
|
|
|
static $index = 0; |
216
|
|
|
$definition->setName('_entity_embed_' . $index++); |
217
|
|
|
return $definition; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* {@inheritdoc} |
222
|
|
|
*/ |
223
|
|
|
public function calculateDependencies() { |
224
|
|
|
$this->addDependencies(parent::calculateDependencies()); |
225
|
|
|
|
226
|
|
|
$definition = $this->formatterPluginManager->getDefinition($this->getDerivativeId()); |
227
|
|
|
$this->addDependency('module', $definition['provider']); |
228
|
|
|
// @todo Investigate why this does not work currently. |
229
|
|
|
//$this->calculatePluginDependencies($this->getFieldFormatter()); |
230
|
|
|
|
231
|
|
|
return $this->dependencies; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |
235
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.