2

I'm using Drupal 8.1.1 with Acquia's Lightning distribution. I need to be able to embed a media entity (a tweet, instagram post, youtube video, image, etc.) and then captured the resultant HTML output in a hook. However, $node->body->value just gives me a weird markup like this:

<drupal-entity data-align=\"none\" data-caption=\"\" data-embed-button=\"media_library\" data-entity-embed-display=\"entity_reference:entity_reference_entity_view\" data-entity-embed-settings=\"{&quot;view_mode&quot;:&quot;media_library&quot;}\" data-entity-id=\"21\" data-entity-type=\"media\" data-entity-uuid=\"ea90ea95-6b7e-4e44-a1a6-4e7baabe3cb7\"></drupal-entity>\r\n"

And rendering the entity through the renderer service gives me the entire output of the node, rather than just the html equivalent of the aforementioned markup.

Is there any way to parse this output and get the actual HTML of the embedded entity?

  • This could be a placeholder. See this: drupal.stackexchange.com/questions/181594/… – Eyal Jun 20 '16 at 5:15
  • We're you able to get this working? I need to do something similar. I'm getting the value of a WYSIWYG field in a node preprocessor. I'm then passing the value of the field to a Twig variable in a markup array that adds drupal-entity to the list of allows XSS tags. HTML is rendered as expected on the front-end. However, even though the drupal-entity markup prints, it is not rendered. I believe this is because it needs to be filtered prior to being passed to Twig. – JP1971 Oct 13 '16 at 13:14
2

The body contains tokens set when you press an embed button in ckeditor.

This raw body content is processed by this filter:

http://cgit.drupalcode.org/entity_embed/tree/src/Plugin/Filter/EntityEmbedFilter.php

 public function process($text, $langcode) {
    $result = new FilterProcessResult($text);

    if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) {
      $dom = Html::load($text);
      $xpath = new \DOMXPath($dom);

      foreach ($xpath->query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id) and (@data-entity-embed-display or @data-view-mode)]') as $node) {
        /** @var \DOMElement $node */
....

You would have to build a view mode in ui with only the body field or apply the filter in code.

  • Thank you!. I can't see any service registered for this class, do you have any example of how I can use this? – Daniel Peraza Jun 20 '16 at 17:24
  • This is not a service, it is a filter plugin and normally you use this in text formats. The complete workflow of the module is to configure the embed entity type with a button in ckeditor, use this button to place the inline content in the body and then use the filter plugin for output. – 4k4 Jun 20 '16 at 17:55
  • Per my comment above, I'm trying to do something similar. However, I'm having a tough time instantiating an instance of EntityEmbedFilter using the new key word. This is because the constructor expects a configuration array, and I haven't yet discovered what that array should look like. – JP1971 Oct 13 '16 at 13:17
0

Please referParse text markup

I was able to get the entity embed markup converted to the desired HTML markup with the help of the last and correct answer mentioned above.

Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.