I have a content type with an image field. How do I update all my images to have the node title as the alt and title of the image? I don't want to edit hundreds of nodes...
Thank you. Daniel
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have a content type with an image field. How do I update all my images to have the node title as the alt and title of the image? I don't want to edit hundreds of nodes...
Thank you. Daniel
The basic logic goes like this:
$nodes = entity_load('node', FALSE, array('type' => 'the_type'));
foreach ($nodes as $node) {
$node->field_image[$node->language][0]['alt'] = $node->title;
$node->field_image[$node->language][0]['title'] = $node->title;
node_save($node);
}
You should probably split that up into a batch job if you have a lot of nodes.
I'm also pretty sure you could make a quicker solution using VBO and some custom PHP but I don't have time to flesh that out right now.
field_image
to whatever the name of you image field is. If you don't have many nodes I'd just download the Devel module, go to /devel/php and run the code there. If you've got a lot of nodes that might time out though, so you might need to look at implementing a batch (there's an example in the Examples Module) or for a very quick fix running set_time_limit(0);
before the code, if your server allows it
– Clive♦
Jan 24 '13 at 22:06
With VBO it is very easy to do. All you need is "Execute arbitrary PHP script" on nodes your want to add alt and title. Change field name "field_image".
foreach ($entity->field_image[$entity->language] as $key => $value) {
if(empty($value['alt'])) $value['alt'] = $entity->title;
if(empty($value['title'])) $value['title'] = $entity->title;
$entity->field_image[$entity->language][$key] = $value;
}
node_save($entity);
This may have been closed, but saw this while searching for the same concern. To date, Drupal 7 has two modules that might help. Hope this helps anyone searching for same requirement.