Skip to main content
Facet Title. Change the Views title depending on the selected Facets filter

Initially, Facet Filters are not at all SEO-friendly by default because the URLs there are without a Pretty path (the Facet Api Pretty path module solves this problem), and it is not possible to output the H1 and Title headers generated by the selected filters. In order to make the created Views with Facets generate SEO headers, you need to modify the module a bit.

In the root directory of the theme, the following function must be added to the template.php file:

/**
 * Implements hook_views_pre_render
 */
function THEME_NAME_views_pre_render(&$view) {
  if($view->name == 'PUT YOUR VIEW NAME HERE') {
    if ($searchers = facetapi_get_active_searchers()) {
      $terms_title = array();
      $searcher = reset($searchers);
      $adapter = facetapi_adapter_load($searcher);
      foreach ($adapter->getAllActiveItems() as $item) {
        $term = taxonomy_term_load($item['value']);
        $terms_title[] = $term->name;
      }
      $view->build_info['title'] = implode($terms_title, ' ');
    }
  }
}

If you want Exposet filters to be taken into account as well add this:

**
 * Implements hook_views_pre_render
 */
function THEMENAME_views_pre_render(&$view) {
  if($view->name == 'yourview' && $view->current_display = 'page') {
    $search_values = array();

    // Check the facets
    if ($searchers = facetapi_get_active_searchers()) {
      $searcher = reset($searchers);
      $adapter = facetapi_adapter_load($searcher);
      foreach ($adapter->getAllActiveItems() as $item) {
        $term = taxonomy_term_load($item['value']);
        $search_values[] = $term->name;
      }
    }
    // Check the views exposed filters
    if ($filters = array_filter($view->exposed_input)) {
      $search_values = array_merge($search_values, $filters);
    }
    if (!empty($search_values)) {
     $view->build_info['title'] = '<h3 class="search-result">Results for</h3><h2 class="search-values">' .
       implode($search_values, ', ') . '</h2>';
    } else $view->build_info['title'] = '';
  }
}

I struggled with this problem for a long time, until I found the topic here: https://www.drupal.org/node/2039701. Now Durpal sites in conjunction with facets are SEO friendly

Subscribe

About The Author

Author of this blog. In SEO for over 10 years. In addition to SEO, I am interested in everything related to technology and earnings on the Internet, which I try to share with readers.