Skip to content

Instantly share code, notes, and snippets.

@swirtSJW
Last active March 1, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swirtSJW/902b870bb50a207711b8 to your computer and use it in GitHub Desktop.
Save swirtSJW/902b870bb50a207711b8 to your computer and use it in GitHub Desktop.
Drupal 7 Views output changes by QueryPath
<?php
// 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>';
$view_query_path->find('.views-row-1')->first()->before($results_header);
$view_query_path->find('.results-header')->first()->prepend($counter);
// Create a title within the results header.
$results_title = t('Search Results');
$results_header_title = "<h2 class=\"title\">{$results_title}</h2>";
$view_query_path->find('.results-header')->first()->prepend($results_header_title);
// Move Search and reset buttons to in-between the two sets of filters.
$buttons = $view_query_path->find('.views-action-buttons')->first()->remove();
$view_query_path->find('#edit-secondary-wrapper')->first()->before($buttons);
$output = $view_query_path->html();
}
}
// Care has to be taken to avoid using fragile selectors that may change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment