Set alt and title attributes for already uploaded images

Public

This short script sets node title as image alt and title attributes. Alt and title fields should be enabled for image field.

</> CopyGet raw version
php
  1. $nodes = entity_load('node', FALSE, array('type' => 'my_content_type'));
  2. foreach ($nodes as $node) {
  3. if (!empty($node->field_image)){
  4. $node->field_image['und'][0]['alt'] = $node->title;
  5. $node->field_image['und'][0]['title'] = $node->title;
  6. field_attach_update('node', $node);
  7. }
  8. }

Comments

arunas's picture

what about very long loop? example if there are 10 000 nodes.

ryan's picture

is there any other way to accomplish this

Adam Thomason's picture

If there is a long loop with, like you say, over 10,000 nodes, then you should utilise the Batch API.