Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Are you using file_entity? –  MPD Apr 25 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 Apr 25 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. –  MPD Apr 25 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 Apr 25 at 15:09
1  
Did a quick test and uri is there with file_entity. –  MPD Apr 25 at 16:36

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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