Paragraphs Editor
ParagraphsEditorExtractor.php
Go to the documentation of this file.
1 <?php
2 
4 
12 
13 /**
14  * A DOM processor plugin for extracting paragraph edits from a DOM tree.
15  *
16  * @DomProcessorDataProcessor(
17  * id = "paragraphs_editor_extractor",
18  * label = "Paragraphs Editor Extractor"
19  * )
20  */
21 class ParagraphsEditorExtractor implements DataProcessorInterface, ContainerFactoryPluginInterface {
23 
24  /**
25  * Creates a paragraph extractor plugin.
26  *
27  * @param \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface $field_value_manager
28  * The field value manager service to initialize the element trait.
29  */
30  public function __construct(FieldValueManagerInterface $field_value_manager) {
31  $this->initializeParagraphsEditorElementTrait($field_value_manager);
32  }
33 
34  /**
35  * {@inheritdoc}
36  */
37  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
38  return new static(
39  $container->get('paragraphs_editor.field_value.manager')
40  );
41  }
42 
43  /**
44  * {@inheritdoc}
45  */
46  public function process(SemanticDataInterface $data, DomProcessorResultInterface $result) {
47  if ($data->is($this->getSelector('widget'))) {
48  return $this->processWidget($data, $result);
49  }
50  elseif ($data->is($this->getSelector('field')) || $data->isRoot()) {
51  return $this->processField($data, $result);
52  }
53  else {
54  return $result;
55  }
56  }
57 
58  /**
59  * Extracts entities from a widget.
60  *
61  * @param \Drupal\dom_processor\DomProcessor\SemanticDataInterface $data
62  * The current widget data to process.
63  * @param \Drupal\dom_processor\DomProcessor\DomProcessorResultInterface $result
64  * The current result.
65  *
66  * @return \Drupal\dom_processor\DomProcessor\DomProcessorResultInterface
67  * The processed result.
68  */
69  protected function processWidget(SemanticDataInterface $data, DomProcessorResultInterface $result) {
70  $entity = $data->get('paragraph.entity');
71  $wrapper = $data->get('field.wrapper');
72 
73  if ($wrapper) {
74  $wrapper->addReferencedEntity($entity);
75  }
76  else {
77  $result = $result->merge([
78  'entities' => [
79  $entity->uuid() => $entity,
80  ],
81  ]);
82  }
83 
84  foreach ($data->node()->childNodes as $child_node) {
85  $data->node()->removeChild($child_node);
86  }
87 
88  $this->removeAttribute($data->node(), 'widget', '<context>');
89 
90  return $result;
91  }
92 
93  /**
94  * Updates field items based on the current processor state.
95  *
96  * @param \Drupal\dom_processor\DomProcessor\SemanticDataInterface $data
97  * The current field data to process.
98  * @param \Drupal\dom_processor\DomProcessor\DomProcessorResultInterface $result
99  * The current result.
100  *
101  * @return \Drupal\dom_processor\DomProcessor\DomProcessorResultInterface
102  * The processed result.
103  */
104  protected function processField(SemanticDataInterface $data, DomProcessorResultInterface $result) {
105  $wrapper = $data->get('field.wrapper');
106 
107  if ($wrapper) {
108  $wrapper->setMarkup($data->getInnerHTML());
109  $wrapper->setFormat($data->get('filter_format'));
110  $entities = $wrapper->getEntities();
111  }
112  else {
113  $entities = $result->get('entities');
114  if (!$entities) {
115  $entities = [];
116  }
117  }
118 
119  $items = $data->get('field.items');
120  $new_revision = $data->get('owner.new_revision');
121  $langcode = $data->get('langcode');
122  $this->fieldValueManager->setItems($items, $entities, $new_revision, $langcode);
123 
124  return $result->clear('entities');
125  }
126 
127 }
processWidget(SemanticDataInterface $data, DomProcessorResultInterface $result)
process(SemanticDataInterface $data, DomProcessorResultInterface $result)
removeAttribute(\DOMNode $node, $element_name, $attribute_name)
static create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
initializeParagraphsEditorElementTrait(FieldValueManagerInterface $field_value_manager)
processField(SemanticDataInterface $data, DomProcessorResultInterface $result)