Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Drupal 7 Views output changes by QueryPath
// Moving lots of stuff (filters, counters, headers...) around in a View
// using QueryPath.
// Not quite the most Drupal-like, but actually much more reliable when it
// comes to inserting items into filters that are not covered by the Views UI
// and moving counters out of the header.
* Implements hook_views_post_render().
function MY_MODULE_views_post_render(&$view, &$output, &$cache) {
if ($view->name === 'MY_VIEW_NAME' && ($view->current_display === 'page')) {
// Load QueryPath to juggle items around the page.
require_once DRUPAL_ROOT . '/sites/all/vendor/querypath/querypath/src/qp.php';
$view_query_path = htmlqp($output);
// Add help link to filters.
$help_markup = '<div class="filter-help"><a href="/some/path/help">Help</a></div>';
$filters = $view_query_path->find('.views-exposed-form')->first()->html();
$view_query_path->find('.views-exposed-form')->first()->html("{$help_markup} \n {$filters}");
// Add definitions link to category filters.
$definitions_link = '<div class="filter-help definitions"><a href="/some/path/secondary-filter-help">View category help</a></div>';
$filters = $view_query_path->find('.bef-secondary-options')->first()->html();
$view_query_path->find('.bef-secondary-options')->first()->html("{$definitions_link} \n {$filters}");
// Move counter from header to top of results.
$counter = $view_query_path->find('.view-count')->first()->remove();
// Create a results header between the filter and the results.
$results_header = '<div class="views-row results-header"></div>';
// Create a title within the results header.
$results_title = t('Search Results');
$results_header_title = "<h2 class=\"title\">{$results_title}</h2>";
// Move Search and reset buttons to in-between the two sets of filters.
$buttons = $view_query_path->find('.views-action-buttons')->first()->remove();
$output = $view_query_path->html();
// Care has to be taken to avoid using fragile selectors that may change.

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment