1

I'm using the Metatag module to include appropriate Open Graph, etc. tags to delineate site content as it is shared on social media sites. The front page of our site is a Views Page, feature only the most recent piece of content. We would like some of the info from this featured node to be included in the meta tags for the home page of the site.

What I'm wondering is -- How can I include meta tags specific to the first result from a View?

Alternately, can anyone suggest an alternative approach to either featuring the most recent piece of content on the home page that would solve this problem?

Thanks!

1 Answer 1

0

Found this thread, which was very helpful:

Metatags and Commerce - pass taxonomy metatags to commerce product view

I adapted that approach to have metatag generate the tags from the first entity returned by the view:

/* 
 * Implement HOOK_views_post_render to run code for each view render
 */ 
function THEME_OR_MODULE_views_post_render(&$view, &$output, &$cache) {
  // check for a particular view and view display page
  if ($view->name=='YOUR_VIEW' && $view->current_display == 'YOUR_VIEW_DISPLAY') {

    // retrieve the first node in the view
    $entity_type = 'node';
    $entity = entity_load($entity_type, $view->result[0]->nid);
    $language = entity_language($entity_type,$entity);

    // Generate metatags output
    if ($tags = metatag_generate_entity_metatags($entity, $entity_type, $language, 'YOUR_ENTITY_DISPLAY_MODE')) {
      list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
      $instance = "{$entity_type}:{$bundle}";

      // register the term's metatags
      metatag_page_set_metatags($instance, $tags);
    }
  }
}

Here is alternate code which doesn't rely on the Metatag module, and allows for more finely tailored meta tags based on view results:

function HOOK_preprocess_html(&$vars) {

    // only operate on the front page
    if ($vars['is_front']) {

        // get the view results
        $view_results = views_get_view_result('YOUR_VIEW_NAME','YOUR_VIEW_DISPLAY_NAME');
        // load up the first node
        $node = entity_metadata_wrapper('node',$view_results[0]->_field_data['nid']['entity']);

        // get some data
        $title = $node->label();

        // some fields may require more processing
        $description = trim(drupal_html_to_text($node->field_description->value()['safe_value']));
        $image = image_style_url('YOUR_IMAGE_STYLE',$film->field_image->value()['uri']);

        // generate and output the meta tags
        $tag = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'property' =>  'og:title',
          'content' => $title,
        )
        );
      drupal_add_html_head($tag, 'og_title');

        $tag = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'property' =>  'og:description',
          'content' => $description,
        )
        );
      drupal_add_html_head($tag, 'og_description');

        $tag = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'property' =>  'og:image',
          'content' => $image,
        )
        );
      drupal_add_html_head($tag, 'og_image');
    }
}

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.