4

I've to create a custom function to display images because I've to add values from custom fields.

I've added a function to template php, like this:

function mytheme_image($variables) {  
}

The problem is that in $variables I haven't the fid of the file, but only the path. I have to load the file to get values from field, how should I do?

I'm looking for functions to get fid from path but I haven't found something..

5 Answers 5

5

@pvorb, you can also use EntityFieldQuery. See the first comment on https://api.drupal.org/api/drupal/includes%21file.inc/function/file_load_multiple/7 for an example. Something along the lines of:

$filename = 'myfirstpony.png';
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'file')
  ->propertyCondition('filename', $filename)
  ->execute();
// if you are guaranteed there is exactly one result, then:
$file_object = reset($result['file']);
$fid = $file_object->fid;
// but beware you might get 0 or many results

If you have more information -- such as the path (from which you can generate a URI) -- then change the query condition to something like ->propertyCondition('uri', 'public://path/to/myfirstpony.png').

5
  • Hello and welcome. Please remember: this is not a forum. Answer the question, not other answers or comments.
    – Mołot
    Commented Dec 15, 2014 at 15:49
  • @molot, the original question was: "The problem is that in $variables I haven't the fid of the file, but only the path. I have to load the file to get values from field, how should I do?" Commented Dec 15, 2014 at 15:52
  • If you are addressing arrubiu's question, don't start your answer with "@pvorb, you can also" - that's misleading. If you are addressing pvorb's post, you shouldn't post it under arrubiu's question at all. Simple as that. You may comment @ someone else to tell him your answer solves their doubts, that's OK, but only answer to the question.
    – Mołot
    Commented Dec 15, 2014 at 15:55
  • @molot, why are you being so pedantic? pvorb asked on dman's answer how to answer arrubiu's original question but using EntityFieldQuery. I did that. Commented Dec 15, 2014 at 16:12
  • @ElephantJim - I believe Molot is just trying to keep the q&a format clean and readable for future visitors. Sure no offence meant Commented Dec 15, 2014 at 17:31
2

Assuming you want to change the image field output - you might want to look at the theme_image_formatter function that you can override and add your variables to the $image var, that will be later sent to theme_image or theme_image_style.

Be aware, that theme_image is working with the paths, so it is possible that the function is not called from within the theme_image_formatter.

3
  • I've tried it, but using netbeans the system doesn't enter in the function while shows images.
    – arrubiu
    Commented Nov 12, 2012 at 13:16
  • so are the images coming through the image field and displayed as such? Commented Nov 12, 2012 at 13:45
  • Images are "file" field, with "media selector" as widget, and displayed as "rendered entity"
    – arrubiu
    Commented Nov 12, 2012 at 13:50
1

Suggestions about formatting are helpful, but to answer the actual question, the answer at https://drupal.org/comment/6493610#comment-6493610 looks good.

// Take a look at: file.inc::file_load_multiple
$files = file_load_multiple(array(), array('uri' => $uri));
$file = reset($files); 
// If empty, $file will be false, otherwise will contain the required file

A two-liner instead of one, but should get the result we need.

1
  • $conditions is marked deprecated in file_load_multiple() so what to do instead?
    – pvorb
    Commented Oct 8, 2014 at 10:29
1

If using the file_entity module, you can use file_uri_to_object as it can load an existing file object (if exists) or create a new one.

http://www.drupalcontrib.org/api/drupal/contributions!file_entity!file_entity.file_api.inc/function/file_uri_to_object/7

If not using this module, just copy this function from "Source" at the link above.

This is a core issue and should be fixed in D8 sometime: https://www.drupal.org/node/685818

0
-1

This seems to be the way to do it In Drupal 8

$files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);

Where $files will be an array of file objects, keyed by the file id.

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.