6

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

5

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.

  • Thanks Clive. But how do I run this? The idea is to put this code in a php file inside my site direcotry and access it in a browser? Or should I use something inside Drupal? (And the only thing I need to change is the "the_type" to my node type machine name, right? ) Sorry for this rookie questions... and thanks again – Daniel Jan 24 '13 at 21:18
  • 1
    No worries, yeah you're right about the node type. You might also need to change 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
  • Yes, I also changed the "field_image" for my image field name. I just did something that does not seem right to me but I was not finding the right way to do it. I put the code in a block, assign the block to the page footer and fired it :) I put a log message in the begining of the script and in the end. I guess it is running yet from some minutes ago... there are updated nodes, but others are not... I'll wait a little bit more... if the "script finished" message will appear... maybe i should have used the set_time_limit(0). – Daniel Jan 24 '13 at 22:26
  • You could just try running it again with set_time_limit it shouldn't hurt... – Clive Jan 24 '13 at 22:34
  • Did it. I just put an "if" to test if the field was already updated... and put some counters to give me feedback about how many were updated, how many skiped and total... checked: 0 left ;) Tested and it is ok. Once again, thank you very much @Clive. – Daniel Jan 24 '13 at 22:51
1

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 worked great! – Collins Oct 23 '17 at 12:47
1

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.

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.