I have an image field in article content type and have created hundreds of nodes for article content type, but now I need to add a value for image alt fields for these existing nodes. Even the default value field for image alt is not showing when I try to edit the field.

How can I add values for the alt attribute of the image field in existing nodes?

enter image description here


Add a node_preprocess function to your theme file. The logic would be to check if the current node has a value in the field for "image alt text", if not then set a default value, else return.

These are not the exact field names but this should get you started on what to look for in your code.

 function MYTHEME_preprocess_article(&$variables) {
      if (!field_image_alt_text->getValue().strlen() = 0 || null ) { set value to something } else {return;}
  • Hi @ssa4b, where I need to add this code? I'm new to Drupal and don't know the Drupal development. Sep 10 '18 at 15:17
  • In your template.php file. In your '/sites/all/themes/<custom theme folder>' if you're using a custom theme.
    – ssa4b
    Sep 10 '18 at 15:27
  • it says syntax error: unexpected T_OBJECT_OPERATOR Sep 10 '18 at 16:23

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.