Hi,

I need to get the URI of the first file attached to a node for Open Graph image. I was looking for some Token for Metatag with no result, so I found this code for template.php which works perfect with an Image field, but doesn't with Media field.

How can I replace get the URI?

Thanks in advance.

<?php
// the function can be located in either in a module or the theme template.php file
function MYTHEME_html_head_alter(&$head_elements) {
// Set the first media field as an og:image for article nodes
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    if ($node && $node->type == 'project') {
      $values = $node->field_media['und'];
      if ($values != FALSE) {
        $val = $values[0]['uri'];
        $head_elements['metatag_og:image']['#value'] = $val;
      }
    }
  }
}

Comments

devin carlson’s picture

Issue summary: View changes
Status: Active » Fixed

I'd suggest using the Entity tokens submodule included with the Entity API project, which should provide tokens for all of the fields attached to a node.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.