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.

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..

share|improve this question

3 Answers 3

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.

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

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.

share|improve this answer
    
$conditions is marked deprecated in file_load_multiple() so what to do instead? –  pvorb Oct 8 at 10:29

@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').

share|improve this answer
    
Hello and welcome. Please remember: this is not a forum. Answer the question, not other answers or comments. –  Mołot Dec 15 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?" –  Elephant Jim Dec 15 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 Dec 15 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. –  Elephant Jim Dec 15 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 –  autopoietic Dec 15 at 17:31

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.