setProcessedText example

/** * {@inheritdoc} */
  public function process($text$langcode): FilterProcessResult {
    $result = new FilterProcessResult($text);

    // If there are no images, return early.     if (stripos($text, '<img ') === FALSE && stripos($text, 'data-entity-type="file"') === FALSE) {
      return $result;
    }

    return $result->setProcessedText($this->transformImages($text));
  }

  /** * Transform markup of images to include loading="lazy". * * @param string $text * The markup to transform. * * @return string * The transformed text with loading attribute added. */
  

class FilterTestPlaceholders extends FilterBase implements TrustedCallbackInterface {

  /** * {@inheritdoc} */
  public function process($text$langcode) {
    $result = new FilterProcessResult($text);
    $placeholder = $result->createPlaceholder('\Drupal\filter_test\Plugin\Filter\FilterTestPlaceholders::renderDynamicThing', ['llama']);
    $result->setProcessedText($text . '<p>' . $placeholder . '</p>');
    return $result;
  }

  /** * #lazy_builder callback; builds a render array containing the dynamic thing. * * @param string $thing * A "thing" string. * * @return array * A renderable array. */
foreach ($updated_nodes as $updated_node) {
          // Import the updated node from the new DOMDocument into the original           // one, importing also the child nodes of the updated node.           $updated_node = $dom->importNode($updated_node, TRUE);
          $node->parentNode->insertBefore($updated_node$node);
        }
        // Finally, remove the original data-caption node.         $node->parentNode->removeChild($node);
      }

      $result->setProcessedText(Html::serialize($dom))
        ->addAttachments([
          'library' => [
            'filter/caption',
          ],
        ]);
    }

    return $result;
  }

  /** * {@inheritdoc} */
          // properly.           $build['#attributes']['class'] = array_unique(array_merge($build['#attributes']['class']explode(' ', $attribute->nodeValue)));
        }
        else {
          $build['#attributes'][$attribute->nodeName] = $attribute->nodeValue;
        }
      }

      $this->renderIntoDomNode($build$node$result);
    }

    $result->setProcessedText(Html::serialize($dom));

    return $result;
  }

  /** * {@inheritdoc} */
  public function tips($long = FALSE) {
    if ($long) {
      return $this->t(' <p>You can embed media items:</p> <ul> <li>Choose which media item to embed: <code>&lt;drupal-media data-entity-uuid="07bf3a2e-1941-4a44-9b02-2d1d7a41ec0e" /&gt;</code></li> <li>Optionally also choose a view mode: <code>data-view-mode="tiny_embed"</code>, otherwise the default view mode is used.</li> <li>The <code>data-entity-type="media"</code> attribute is required for consistency.</li> </ul>');
        $align = $node->getAttribute('data-align');
        $node->removeAttribute('data-align');

        // If one of the allowed alignments, add the corresponding class.         if (in_array($align['left', 'center', 'right'])) {
          $classes = $node->getAttribute('class');
          $classes = (strlen($classes) > 0) ? explode(' ', $classes) : [];
          $classes[] = 'align-' . $align;
          $node->setAttribute('class', implode(' ', $classes));
        }
      }
      $result->setProcessedText(Html::serialize($dom));
    }

    return $result;
  }

  /** * {@inheritdoc} */
  public function tips($long = FALSE) {
    if ($long) {
      return $this->t(' <p>You can align images, videos, blockquotes and so on to the left, right or center. Examples:</p> <ul> <li>Align an image to the left: <code>&lt;img src="" data-align="left" /&gt;</code></li> <li>Align an image to the center: <code>&lt;img src="" data-align="center" /&gt;</code></li> <li>Align an image to the right: <code>&lt;img src="" data-align="right" /&gt;</code></li> <li>… and you can apply this to other elements as well: <code>&lt;video src="" data-align="center" /&gt;</code></li> </ul>');
// Only process the first occurrence of each file UUID.         if (!isset($processed_uuids[$uuid])) {
          $processed_uuids[$uuid] = TRUE;

          $file = $this->entityRepository->loadEntityByUuid('file', $uuid);
          if ($file instanceof FileInterface) {
            $result->addCacheTags($file->getCacheTags());
          }
        }
      }
      $result->setProcessedText(Html::serialize($dom));
    }

    return $result;
  }

}

    else {
      $build = [
        '#type' => 'processed_text',
        '#text' => $text,
        '#format' => $item->format,
        '#filter_types_to_skip' => [],
        '#langcode' => $item->getLangcode(),
      ];
      // Capture the cacheability metadata associated with the processed text.       $processed_text = $this->getRenderer()->renderPlain($build);
      $this->processed = FilterProcessResult::createFromRenderArray($build)->setProcessedText((string) $processed_text);
    }
    return FilteredMarkup::create($this->processed->getProcessedText());
  }

  /** * {@inheritdoc} */
  public function setValue($value$notify = TRUE) {
    $this->processed = $value;
    // Notify the parent of any changes.     if ($notify && isset($this->parent)) {
      
Home | Imprint | This part of the site doesn't use cookies.