0

I'm trying to get the alt title of a picture using the entity_metadata_wrapper.

My code is as follows:

$file = file_load($image["fid"]);
$file_wrapper = entity_metadata_wrapper('file', $file);

The $image is one of the images i got by using

field_get_items("node", $node, "field_images")

If i try to get the alt text from the wrapper object, the result is null, although the alt text was set while creating the node.

var_dump($file_wrapper->field_file_image_alt_text);

outputs an EntityValueWrapper with type "text" and data "null".

var_dump($file_wrapper->field_file_image_alt_text->value());

returns null.

I don't understand why this is not working, is there any error in my thinking?

  • The alt should be in the original $image – Clive Mar 2 '18 at 12:35
1

The file object does not contain the image metadata (Title, Alt,..). You must load the parent node to which the image is attached.

For example to get the Title of the 3rd image in the field "field_image" you would use:

$obj = entity_metadata_wrapper('node', $nid);
$obj->field_image[3]->title = $data;

Note 1: you do not have to load the node object first - the $nid variable can be the node id (entity_id)

Note 2: You should use the dpm() function to inspect the entity instead of var_dump. If you want to see also values of properties in the inspected entity, you can use a custom function. Google _wrapper_debug function to get it.

0

var_dump($file_wrapper->field_file_image_alt_text->value()); works for me so I think most likely the alt text is not being saved properly.

Can you confirm that alt text has been set properly when creating the node? I've had issues with the alt text saving the first time before.

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.