Skip to content
Snippets Groups Projects

add html to custom elements formatter

3 unresolved threads

Closes #3494426

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
116 * Converts a DOM node into a custom element with all attributes.
117 *
118 * @param \DOMNode $node
119 * The DOM node to convert.
120 *
121 * @return \Drupal\custom_elements\CustomElement|null
122 * Returns the converted CustomElement or NULL.
123 */
124 protected function convertNodeToCustomElement(\DOMNode $node): ?CustomElement {
125 if ($node->nodeType === XML_TEXT_NODE) {
126 $trimmedText = trim(preg_replace('/\s+/', ' ', $node->nodeValue));
127 if (!empty($trimmedText)) {
128 $customElement = CustomElement::create('text');
129 $customElement->addSlot('text', $trimmedText);
130 return $customElement;
131 }
  • This looks like it converts <p>text</p> to <p><text>text</text></p>.

    Looks like it needs to get $parentcustomelement passed into this recursive function, and you can just do $parentcustomelement->addSlot('text', $trimmedText);

  • Please register or sign in to reply
  • 142 }
    143
    144 $hasContent = FALSE;
    145 $content = [];
    146 foreach ($node->childNodes as $childNode) {
    147 $childElement = $this->convertNodeToCustomElement($childNode);
    148 if (!empty($childElement)) {
    149 $slot = $childNode->nodeName === 'img' ? 'image' : 'content';
    150 $content[] = ['slot' => $slot, 'element' => $childElement];
    151 $hasContent = TRUE;
    152 }
    153 }
    154
    155 // Skip if paragraphs and divs have no content and no attributes.
    156 if (($tagName === 'p' || $tagName === 'div') && !$hasContent && empty($customElement->getAttributes())) {
    157 return NULL;
    • This is not supposed to be here; if you convert the CE tree back to HTML, it is supposed to come out exactly as the input.

      Things like the above should be in e.g. the text filter, not in the thing that parses/unserializes the HTML into CE.

    • Please register or sign in to reply
  • 147 $childElement = $this->convertNodeToCustomElement($childNode);
    148 if (!empty($childElement)) {
    149 $slot = $childNode->nodeName === 'img' ? 'image' : 'content';
    150 $content[] = ['slot' => $slot, 'element' => $childElement];
    151 $hasContent = TRUE;
    152 }
    153 }
    154
    155 // Skip if paragraphs and divs have no content and no attributes.
    156 if (($tagName === 'p' || $tagName === 'div') && !$hasContent && empty($customElement->getAttributes())) {
    157 return NULL;
    158 }
    159
    160 // Skip if element has no content, no attributes,
    161 // and is not self-closing (like <br> or <hr>).
    162 if (!$hasContent && empty($customElement->getAttributes()) && !in_array($tagName, ['br', 'hr', 'img', 'input'])) {
    Please register or sign in to reply
    Loading