My Drupal 8.8 site (media embed is now in core) uses the Markdown module for the Body text of nodes.

I created a custom view mode, Ebook export, which has some twig templates that output the raw markdown (in the template: <pre>{{ node.body.value }}</pre>). As suggested by the view mode name, this view is to give me something I can cut-and-paste to generate an ebook using pandoc.

However, the images embedded by media module are not in markdown; they use a custom embed tag:

<drupal-media data-align="center" data-entity-type="media" data-entity-uuid="269d4491-9866-403c-90e3-c1298e9857de"</drupal-media>

To generate a markdown link, I need the image file name, not a UUID. But, I need to use this embed tag to get the image to show up normally for visitors to the website.

So I somehow need to convert the <drupal-media> tags to image file names when in the Ebook export view mode.

What I have tried so far

I created a custom view mode for the media image type and set the format to URL to image.

This allows me to add data-view-mode="markdown-link" to the <drupal-media> tag to get the file name.

<drupal-media data-align="center" data-entity-type="media" data-entity-uuid="269d4491-9866-403c-90e3-c1298e9857de" data-view-mode="markdown_link"></drupal-media>

I thought I could use theme_preprocess() to check for the view mode and then use a regex to add the markdown_link view mode. However, this doesn't work because the twig template is set to <pre>{{ node.body.value }}</pre> to output the raw markdown, and so the <drupal-media> tag won't get rendered, and I need to render it to get the file name.

So I'm stuck about how I might accomplish this.

To summarize, I want to get the file name of the image file represented by a <drupal-media> tag and then output that in markdown format inside the body of a node.

Your Answer

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

Browse other questions tagged or ask your own question.