1

I have a Commerce Product view that pulls in products based on a taxonomy term in the url (category/taxonomy-term). This is done using relationships. Ideally, I would be able to edit metatags for each individual term (which I can) and have those metatags populated on the view page (which I can't figure out).

If I create a metatag default for taxonomy term, it works correctly on /taxonomy/term/%tid, but not category/term-name. If I create a metatag default for that view, I don't have access to the metatags as token replacement values for the term being used. There is a [metatags:description] token, but it is empty when using it on the view.

Thanks for any direction/insight/help!

1 Answer 1

1

The short answer is that the metatag module will provide metatags entered for terms if your view path matches the core taxonomy path (taxonomy/term/%).

In metatag_views_post_render(), metatags checks if your view display path matches the path from the entity's info before it will proceed.

This gives you a couple of options.

1) You can make sure your view uses the taxonomy/term/% path and use Pathauto to make the aliases for your terms for that vocabulary (or all terms) match the desired 'category/term-name' pattern.

2) You can mimic what the metatag module does for your specific view using a custom module and these hooks:

/**
 * Implements hook_views_post_render().
 */
function MYMODULE_views_post_render(&$view, &$output, &$cache) {
  // Build a shortcut to the current display object.
  $display = $view->display[$view->current_display];

  // Only proceed if this view is a full page, don't process block or other
  // Views display objects.
  if ($view->name == 'MY_VIEW_NAME' && $display->display_plugin == 'page') {
    // Check if this is an entity display page, if so trigger
    // hook_entity_view().
    foreach (entity_get_info() as $entity_type => $entity_info) {
      // Entity paths will include an auto-loader that matches the entity's
      // name, thus the path will be 'some/path/%entity_name'.
      if (isset($entity_info['path']) && $entity_info['path'] == 'taxonomy/term/%taxonomy_term') {
        // Only proceed if this entity type supports meta tags.
        if (metatag_entity_supports_metatags($entity_type)) {
          // There must be at least one argument and the first argument must be
          // numerical.
          if (!empty($view->args) && is_numeric($view->args[0])) {
            // Only the first argument is used.
            $entities = entity_load($entity_type, array($view->args[0]));
            // Only if the entity actually exists.
            if (!empty($entities)) {
              $entity = array_pop($entities);
              MYMODULE_entity_view($entity, $entity_type, 'full', NULL, TRUE);
            }
          }
        }
      }
    }
  }
}



/**
 * Implements hook_entity_view().
 *
 * Provides additional argument to allow the display to be forced, to work
 * around problems elsewhere in the APIs.
 */
function MYMODULE_entity_view($entity, $entity_type, $view_mode, $langcode, $force = FALSE) {
  // Only run this function once per page load.
  static $i_will_say_this_only_once = FALSE;

  // Only proceed if this entity object is the page being viewed.
  if ($entity_type == 'taxonomy_term' && $view_mode == 'full') {
    // Some API calls need to force the data loading.
    if (!$force) {
      // Only run this function once per page load.
      if ($i_will_say_this_only_once) {
        return;
      }
      $i_will_say_this_only_once = TRUE;
    }

    // CTools uses 'page_manager' view mode to indicate the full entity display
    // page rather than 'full', so streamline the internal processes.
    if ($view_mode == 'page_manager') {
      $view_mode = 'full';
    }

    // Generate metatags output.
    if ($output = metatag_generate_entity_metatags($entity, $entity_type, $langcode, $view_mode)) {
      list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
      $instance = "{$entity_type}:{$bundle}";
      // We need to register the term's metatags, so we can later fetch them.
      // @see metatag_page_build().
      metatag_page_set_metatags($instance, $output);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.