|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\paragraphs_editor\Plugin\Field\FieldFormatter; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityDisplayRepositoryInterface; |
|
6
|
|
|
use Drupal\Core\Field\FieldDefinitionInterface; |
|
7
|
|
|
use Drupal\Core\Field\FieldItemListInterface; |
|
8
|
|
|
use Drupal\Core\Field\FormatterBase; |
|
9
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
10
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
|
11
|
|
|
use Drupal\dom_processor\DomProcessor\DomProcessorInterface; |
|
12
|
|
|
use Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface; |
|
13
|
|
|
use Drupal\paragraphs_editor\Utility\TypeUtility; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Implementation of the 'entity_reference_paragraphs_editor' formatter. |
|
18
|
|
|
* |
|
19
|
|
|
* @FieldFormatter( |
|
20
|
|
|
* id = "entity_reference_paragraphs_editor", |
|
21
|
|
|
* label = @Translation("Rendered Editor Markup"), |
|
22
|
|
|
* field_types = { |
|
23
|
|
|
* "entity_reference_revisions" |
|
24
|
|
|
* } |
|
25
|
|
|
* ) |
|
26
|
|
|
*/ |
|
27
|
|
|
class ParagraphsEditorFormatter extends FormatterBase implements ContainerFactoryPluginInterface { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The field value manager for wrapping editor field items. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $fieldValueManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The entity display repository service for getting view modes. |
|
38
|
|
|
* |
|
39
|
|
|
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $entityDisplayRepository; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The dom processor for processing embed codes. |
|
45
|
|
|
* |
|
46
|
|
|
* @var \Drupal\dom_processor\DomProcessor\DomProcessorInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $domProcessor; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, FieldValueManagerInterface $field_value_manager, EntityDisplayRepositoryInterface $entity_display_repository, DomProcessorInterface $dom_processor) { |
|
54
|
|
|
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); |
|
55
|
|
|
$this->fieldValueManager = $field_value_manager; |
|
56
|
|
|
$this->entityDisplayRepository = $entity_display_repository; |
|
57
|
|
|
$this->domProcessor = $dom_processor; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
|
64
|
|
|
return new static( |
|
65
|
|
|
$plugin_id, |
|
66
|
|
|
$plugin_definition, |
|
67
|
|
|
$configuration['field_definition'], |
|
68
|
|
|
$configuration['settings'], |
|
69
|
|
|
$configuration['label'], |
|
70
|
|
|
$configuration['view_mode'], |
|
71
|
|
|
$configuration['third_party_settings'], |
|
72
|
|
|
$container->get('paragraphs_editor.field_value.manager'), |
|
73
|
|
|
$container->get('entity_display.repository'), |
|
74
|
|
|
$container->get('dom_processor.dom_processor') |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function defaultSettings() { |
|
82
|
|
|
return [ |
|
83
|
|
|
'view_mode' => 'default', |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function viewElements(FieldItemListInterface $items, $langcode) { |
|
91
|
|
|
$elements = []; |
|
92
|
|
|
|
|
93
|
|
|
// Prepare the processor with data from the top level item being rendered |
|
94
|
|
|
// only. This is pushed onto the processing stack and consumed at the |
|
95
|
|
|
// composition root of the paragraph tree. Recursive rendering will all use |
|
96
|
|
|
// the settings from the composition root. Note that we cannot set the |
|
97
|
|
|
// 'field' key here since it will be used for all recursive rendering, which |
|
98
|
|
|
// would result in an infinite rendering loop. |
|
99
|
|
|
$field_value_wrapper = $this->fieldValueManager->wrapItems($items); |
|
100
|
|
|
if (!$this->domProcessor->prepared()) { |
|
101
|
|
|
$this->domProcessor->prepare([ |
|
102
|
|
|
'field' => [ |
|
103
|
|
|
'items' => $items, |
|
104
|
|
|
'wrapper' => $field_value_wrapper, |
|
105
|
|
|
'is_mutable' => TRUE, |
|
106
|
|
|
], |
|
107
|
|
|
'settings' => $this->getSettings(), |
|
108
|
|
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$elements[0] = [ |
|
112
|
|
|
'#type' => 'processed_text', |
|
113
|
|
|
'#text' => $field_value_wrapper->getMarkup(), |
|
114
|
|
|
'#format' => $field_value_wrapper->getFormat(), |
|
115
|
|
|
'#langcode' => $langcode, |
|
116
|
|
|
]; |
|
117
|
|
|
|
|
118
|
|
|
return $elements; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function settingsForm(array $form, FormStateInterface $form_state) { |
|
125
|
|
|
$elements = []; |
|
126
|
|
|
$elements['view_mode'] = [ |
|
127
|
|
|
'#type' => 'select', |
|
128
|
|
|
'#options' => $this->entityDisplayRepository->getViewModeOptions('paragraph'), |
|
129
|
|
|
'#title' => $this->t('View Mode'), |
|
130
|
|
|
'#description' => $this->t('The view mode that embedded entities will be rendered with.'), |
|
131
|
|
|
'#default_value' => $this->getSetting('view_mode'), |
|
132
|
|
|
'#required' => TRUE, |
|
133
|
|
|
]; |
|
134
|
|
|
|
|
135
|
|
|
return $elements; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function settingsSummary() { |
|
142
|
|
|
$summary = []; |
|
143
|
|
|
$summary[] = $this->t('Rendered as @mode', ['@mode' => $this->getSetting('view_mode')]); |
|
144
|
|
|
return $summary; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function isApplicable(FieldDefinitionInterface $field_definition) { |
|
151
|
|
|
return \Drupal::service('paragraphs_editor.field_value.manager')->isParagraphsEditorField($field_definition); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* {@inheritdoc} |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function mergeDefaults() { |
|
158
|
|
|
$field_config = TypeUtility::ensureFieldConfig($this->fieldDefinition); |
|
159
|
|
|
$this->settings += $field_config->getThirdPartySettings('paragraphs_editor'); |
|
160
|
|
|
$this->settings += static::defaultSettings(); |
|
161
|
|
|
$this->defaultSettingsMerged = TRUE; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|