Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created July 23, 2010 20:56
Show Gist options
  • Save bangpound/488020 to your computer and use it in GitHub Desktop.
Save bangpound/488020 to your computer and use it in GitHub Desktop.
alter Drupal 6 theme output after rendering with QueryPath
/**
* Microformat function.
*/
function microformat() {
$args = func_get_args();
$hooks = array_shift($args);
if (!is_array($hooks)) {
$hooks = array($hooks);
}
$output = call_user_func_array('theme', array_merge(array($hooks), $args));
if ($output) {
include_once libraries_get_path('QueryPath') .'/QueryPath.php';
$qp = hqp($output);
foreach ($hooks as $hook) {
drupal_alter('microformat_'. $hook, $qp, $args);
}
$output = $qp->xml();
}
return $output;
}
/**
* A special-purpose version of {@link qp()} for Microformats.
*
* hqp means Microformat (μF) QueryPath.
*
* Output of Drupal themes (if valid XHTML) can be processed as XML snippets if
* libxml is told to handle the document as HTML but the document is preceded
* with an XML declaration, the document has a single root element and HTML
* entities are replaced with UTF-8 encoded entities.
*
* The following options are automatically set unless overridden:
* - ignore_parser_warnings: TRUE
* - omit_xml_declaration: TRUE (on output, remove XML declaration)
* - use_parser: html
* - replace_entities: TRUE
*
* Parser warning messages are also suppressed, so if the parser emits a warning,
* the application will not be notified. This is equivalent to
* calling <code>@qp()</code>.
*
* Warning: Character set conversions will only work if the Multi-Byte (mb) library
* is installed and enabled. This is usually enabled, but not always.
*
* @see qp()
*/
function hqp($document = NULL, $selector = NULL, $options = array(), $declaration = '<?xml version="1.0" encoding="UTF-8" ?>') {
// Need a way to force an HTML parse instead of an XML parse when the
// doctype is XHTML, since many XHTML documents are not valid XML
// (because of coding errors, not by design).
$options += array(
'omit_xml_declaration' => TRUE,
'ignore_parser_warnings' => TRUE,
'use_parser' => 'html',
'replace_entities' => TRUE,
);
return @qp($declaration . $document, $selector, $options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment