Paragraphs Editor
ParagraphsEditorElementTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
5 /**
6  * Helper trait for working with Paragraphs Editor DOM Elements.
7  */
9 
10  /**
11  * The field value manager service for looking up element information.
12  *
13  * @var \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface
14  */
15  protected $fieldValueManager;
16 
17  /**
18  * Injects field value manager service.
19  *
20  * @param \Drupal\paragraphs_editor\EditorFieldValue\FieldValueManagerInterface $field_value_manager
21  * The field value manager service for looking up element information.
22  */
23  protected function initializeParagraphsEditorElementTrait(FieldValueManagerInterface $field_value_manager) {
24  $this->fieldValueManager = $field_value_manager;
25  }
26 
27  /**
28  * Gets a selector for an element type.
29  *
30  * @param string $element_name
31  * The name of the element to get the selector for.
32  *
33  * @return string
34  * The selector for the element or NULL if no such element definition
35  * exists.
36  */
37  protected function getSelector($element_name) {
38  return $this->fieldValueManager->getSelector($element_name);
39  }
40 
41  /**
42  * Gets an attribute value from a DOM object.
43  *
44  * @param \DOMNode $node
45  * The DOM node object to extract the attribute from.
46  * @param string $element_name
47  * The name of the element to lookup the attribute for.
48  * @param string $attribute_name
49  * The attribute name to extract.
50  *
51  * @return string
52  * The attribute value on the node, or NULL if no such attribute could be
53  * extracted.
54  */
55  protected function getAttribute(\DOMNode $node, $element_name, $attribute_name) {
56  $key = $this->fieldValueManager->getAttributeName($element_name, $attribute_name);
57  return $key ? $node->getAttribute($key) : NULL;
58  }
59 
60  /**
61  * Sets an attribute value on a DOM object.
62  *
63  * @param \DOMNode $node
64  * The DOM node object to set the attribute for.
65  * @param string $element_name
66  * The name of the element to lookup the attribute for.
67  * @param string $attribute_name
68  * The attribute name to set.
69  * @param string $value
70  * The value to be set on the node.
71  */
72  protected function setAttribute(\DOMNode $node, $element_name, $attribute_name, $value) {
73  $key = $this->fieldValueManager->getAttributeName($element_name, $attribute_name);
74  $node->setAttribute($key, $value);
75  }
76 
77  /**
78  * Sets an attribute value on a DOM object.
79  *
80  * @param \DOMNode $node
81  * The DOM node object to remove the attribute for.
82  * @param string $element_name
83  * The name of the element to lookup the attribute for.
84  * @param string $attribute_name
85  * The name of the attribute to be removed.
86  */
87  protected function removeAttribute(\DOMNode $node, $element_name, $attribute_name) {
88  $key = $this->fieldValueManager->getAttributeName($element_name, $attribute_name);
89  $node->removeAttribute($key);
90  }
91 
92  /**
93  * Creates a DOM element from an element definition.
94  *
95  * @param \DOMDocument $document
96  * The document that will be used as a factory for creating the element.
97  * @param string $element_name
98  * The name of the element type to create.
99  * @param array $attributes
100  * A key value pair of attribute names and values to set on the newly
101  * created attribute.
102  *
103  * @return \DOMElement
104  * The newly created DOM Element.
105  */
106  protected function createElement(\DOMDocument $document, $element_name, array $attributes = []) {
107  $node = $document->createElement($this->fieldValueManager->getElement($element_name)['tag']);
108  foreach ($attributes as $key => $value) {
109  $this->setAttribute($node, $element_name, $key, $value);
110  }
111  return $node;
112  }
113 
114 }
setAttribute(\DOMNode $node, $element_name, $attribute_name, $value)
removeAttribute(\DOMNode $node, $element_name, $attribute_name)
initializeParagraphsEditorElementTrait(FieldValueManagerInterface $field_value_manager)
getAttribute(\DOMNode $node, $element_name, $attribute_name)
createElement(\DOMDocument $document, $element_name, array $attributes=[])