4

I'm inside mytheme_image_formatter($variables) function and I got the image's FID (from $fid = $variables['item']['fid']).

Question: Is there a way to know which object is using the image file?

Using file_usage_list(file_load($fid)), I can see which objects may use the file. But I want to know which one is using the image file, inside my function.

Tried to use mytheme_preprocess_image(&$variables), but I also found the same situation.

This is the same problem as discussed here, without a solution.

Thanks

  • What I'm trying to do is to put an ALT text in all images of all site's pages. If there is no pre-defined ALT text, I want to inject the node's title in it. – dxvargas Jul 19 '13 at 16:40
3

You can use an EntityFieldQuery, e.g.

$field_name = 'field_something';
$fid = 123;

$query = new \EntityFieldQuery();
$query->entityCondition('node')
  ->propertyCondition('type', 'the_type')
  ->fieldCondition($field_name, 'fid', $fid);

$results = $query->execute();
if (!empty($results['node'])) {
  $nodes = node_load_multiple(array_keys($results['node']));

  // $nodes contains all nodes that reference the given file via field_something.
}
  • Thanks! But the problem with this approach is that $field_name is yet another information I want to know, because I don't have it. I just know what comes in $variables. – dxvargas Jul 19 '13 at 16:36
  • Those theme functions are re-usable so they can't rely on a node/field context (that's why they're not in the vars). You'll probably have to be a bit more specific, like implementing hook_node_view() and targeting the specific fields in $node->content, based on $node->type. You should be able to add an alt to the image field structure that way – Clive Jul 19 '13 at 17:06
  • You're right, this wasn't the best approach. I ended up taking another completely different way for getting my problem solved. – dxvargas Jul 20 '13 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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