11

Getting hold of file objects related to nodes through a field is easy using an EntityMetadataWrapper:

$wrapper = entity_metadata_wrapper('node', $node);
$images = $wrapper->field_images;

So that's great. However, I want to use some of the properties of the standard file object, and they're not available. When I look at the properties available on $images[0]->file using the above code, I see these:

  • fid
  • name
  • mime
  • size
  • url
  • timestamp
  • owner

Most notably, uri is missing, and in its place there's a url property, which is the full external URL to the image file. I want to manipulate these images via their URI (create thumbs, etc), so I'm left doing this:

$fid = $images[0]->file->fid->value();
$uri = file_load($fid)->uri;

Which seems to negate the whole point of using an EntityMetadataWrapper in the first place.

What am I missing? Where is the URI on the wrapped file object? If it's not available, for the love of all things good and pure, why not?!

I'm aware I could deconstruct the URL back to a URI, but that also feels very unnecessary.

5
  • 1
    Are you using file_entity?
    – mpdonadio
    Commented Apr 25, 2014 at 14:59
  • @MPD Ahhhh, great call, no I'm not...maybe I shouldn't be expecting to see what I'd consider 'properties' if the object isn't acting like an entity. URL seems a strange choice instead of URI for the Entity module guys to have chosen, though. If the answer is "you're going to need to implement hook_entity_property_info_alter()" then that's fine, just wanted to know
    – Clive
    Commented Apr 25, 2014 at 15:02
  • I'm actually not sure if that will make a difference, but having that module enabled does end up in some subtle differences here and there. I have found File integration with EMW a bit lacking in other places, but haven't been bothered enough by it to submit a patch.
    – mpdonadio
    Commented Apr 25, 2014 at 15:07
  • @MPD I think it will - just found out the 'offender' is entity_metadata_system_entity_property_info(), where a bunch of new properties get tacked onto $info['file']['properties']. I'm willing to bet if file_entity was in there that array would already have the entity properties themselves in there. I'll try to write an answer up later or tomorrow if no-one's already done it
    – Clive
    Commented Apr 25, 2014 at 15:09
  • 1
    Did a quick test and uri is there with file_entity.
    – mpdonadio
    Commented Apr 25, 2014 at 16:36

3 Answers 3

9

Big thanks to MPD for pointing me in the right direction. In short, without the File Entity module, files aren't entities, so they don't naturally have the 'properties' I was expecting.

The Entity module provides some on their behalf, but URI isn't one of them. Fortunately it's very simple to make it available using hook_entity_property_info_alter() and a custom property callback:

function MYMODULE_entity_property_info_alter(&$info) {
  $info['file']['properties']['uri'] = array(
    'label' => t("URI"),
    'description' => t("The URI of the file."),
    'getter callback' => 'MYMODULE_entity_get_file_properties',
    'schema field' => 'uri',
  );
}

function MYMODULE_entity_get_file_properties($file, array $options, $name) {
  switch ($name) {
    case 'uri':
      return $file->uri;
      break;
  }
}

After that, $images[0]->file->uri->value() is available as expected.

0
8

Below works without the hack:

 $node_wrapper->field_media[0]->value()['uri'];
1
  • 2
    It's good to point out that this is supported from PHP 5.4 on.
    – alesr
    Commented May 17, 2016 at 15:09
3

This is what works for me to get the full path of a single image...

// wrap it
$wrapper = entity_metadata_wrapper('node', node_load(16));
// get the value
$image = $wrapper->field_image->value();
// check it
if ($image) {
   // get the full path
   $path = file_create_url($image['uri']);
}
print_r($path);

Alternatively...

  $image = $wrapper->field_image->value();
  $image_path = $image ? file_create_url($image['uri']) : '';

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.