Skip to content

Instantly share code, notes, and snippets.

@laradevitt
Created April 20, 2016 15:53
Show Gist options
  • Save laradevitt/aa40d696f98c6d34e7d0138eb20aabc6 to your computer and use it in GitHub Desktop.
Save laradevitt/aa40d696f98c6d34e7d0138eb20aabc6 to your computer and use it in GitHub Desktop.
Remove Media module tags from body field teaser
<?php
/**
* Preprocess variables for node.tpl.php (and derivatives).
*
* Strips Media (media module) tags from teasers in article content.
* Workaround until we have a filter: https://www.drupal.org/node/2165457
*/
function MYTHEME_preprocess_node(&$variables) {
if ($node->type == 'article') {
$content = $variables['content'];
if (!empty($content['body'])) {
if ($content['body']['#view_mode'] == 'teaser') {
// Remove media tags.
$doc = new DOMDocument();
$doc->loadHTML($content['body'][0]['#markup'], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$selector = new DOMXPath($doc);
foreach ($selector->query('//div[contains(attribute::class, "media")]') as $e) {
$e->parentNode->removeChild($e);
}
$variables['content']['body'][0]['#markup'] = $doc->saveHTML($doc->documentElement);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment