Drupal - Add title and alternate text to image automatically?

The most direct way might be to do something along these lines in hook_node_presave() in a custom module:

function YOURMODULE_node_presave($node) {

  if(isset($node->field_image)) {
    $node->field_image[LANGUAGE_NONE][0]['alt']=$node->title;
    $node->field_image[LANGUAGE_NONE][0]['title']=$node->title;
  }

}

where field_image is the actual name of the image field you have in your node.

If you don't want your users to be see or to attempt to change this, just make sure the alt and title boxes aren't checked in the field setup, eg:

enter image description here

as this is just for the field add/edit widget in the form; the above code will still work even if these attributes aren't "enabled."


You can use ImageField Tokens:

The ImageField Tokens module extends the default functionality of Image fields adding the ability to specify default values and use node tokens in the Alt and Title text.

enter image description here

Tags:

Media

7