Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created July 30, 2013 21:43
Show Gist options
  • Save bangpound/6117282 to your computer and use it in GitHub Desktop.
Save bangpound/6117282 to your computer and use it in GitHub Desktop.
Render Drupal fields inside the rendered markup of other Drupal fields.
<?php
/**
* Implement hook_file_view_alter().
*
* @param type $build
* @param type $entity_type
*/
function MODULE_file_view_alter(&$build, $entity_type) {
$qp = QueryPath::withHTML($build['field_caption']['0']['#markup']);
// Captures the contents of the last element of the caption. If there are multiple
// paragraphs, this is the last one.
$html = $qp->find(':root body')->children(':last-child')->innerHTML();
// Append rendered author and publisher field.
$html .= render($build['field_author']);
$html .= render($build['field_publisher']);
// Replace the contents of the last element.
$qp->find(':root body')->children(':last-child')->html($html);
// Set the render markup to the new value.
$build['field_caption']['0']['#markup'] = $qp->find(':root body')->innerHTML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment