7

In my site users can upload content and also images.

My goal is that when a user is uploading content to the site, and also an image - the title of the content will be also the image title and the alternate text of the image. Currently I am using the core image field.

Is there a way or a module that can achieve all that?

10

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."

  • is it possible to set the default alt text to be the name of the image file without extension? This may help if a website that missing millions of alt text. – CocoSkin Sep 26 '14 at 17:46
  • 1
    @CocoSkin, sure. If you wanted to do that, I would investigate theme_image (api.drupal.org/api/drupal/includes%21theme.inc/function/…) and override it in your theme, eg, if alt isn't set, grab the name from the path, massage it to your liking and then use it for the alt text – Jimajamma Sep 27 '14 at 14:00
  • hope i'm not bothering you that much. Does it mean It will be the same code as above but using "function theme_image($node)" instead of "function YOURMODULE_node_presave($node)"? if so, what about $path? and $alt? – CocoSkin Oct 1 '14 at 21:00
  • not a problem at all, you would create a function in your theme named THEME_image(....) as described in the api and use that instead of MODULE_node_presave(....) – Jimajamma Oct 2 '14 at 14:37
6

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

  • Looks like a module is unmaintained now. But the dev version works fine. – milkovsky Oct 25 '17 at 19:12
1

Here's a D7 rule I created to do that very thing. Just go to Rules > Import and Save. Will automatically add the [node:title] and [site:name] tokens to the alt text field when content is saved or updated:

{ "rules_alt_text_default" : {
    "LABEL" : "Alt Text Default",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "TAGS" : [ "Custom" ],
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_insert" : [], "node_update" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_image" } }
    ],
    "DO" : [
      { "data_set" : {
          "data" : [ "node:field-image:alt" ],
          "value" : "[node:title] - Available at [site:name]"
        }
      }
    ]
  }
} 
0

There are 2 easy Methods to achieve this:

Method 1 is more straightforward; Configure your File Entity Module and in the alt and title attribute fields, replace the token with [current-page:title] which is also one of the available tokens

Please note: for this method above, the title and attribute fields are replaced if the fields are empty, if any text is input in them at any time, that text will be used instead.

Method 2 uses Rules to achieve this and this is how:

  1. Create Rules with the following events:

    a. after saving new content

    b. after updating existing content

  2. Add condition: "content is of type: (add your content type(s) here.....)" Note: without this condition, rules does not seem to pull out the alt and title attributes from your image field. You may also add other conditions to check if the fields are empty or not (" using data value is empty....") so that you do not alter existing text in the fields. This gives better control than Method 1 above.

  3. Add 2 actions to set your data values for both alt and title fields here using the node title.

For even finer control, you may install the "Conditional Rules" module to check if both or one of alt and title attributes were filled up (example scenario is "if alt is NOT empty and title is empty, update title attribute" or "if alt is empty and title is NOT empty, update alt attribute... etc).

If you do not want to install the conditional rules module but still want better control in checking empty fields, then just create 2 different rules to independently check both fields.

  1. Save Your rule. that's it.

You may also simply import this rule i created which does the simple steps above. Note that my field is named "field_image" and my content type is "article":

{ "rules_update_image_alt_and_title_automatically" : {
    "LABEL" : "Update image alt and title automatically",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_update" : [], "node_insert" : [] },
    "IF" : [
      { "node_is_of_type" : { "node" : [ "node" ], "type" : { "value" : { "article" : "article" } } } }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-image:alt" ], "value" : "[node:title]" } },
      { "data_set" : { "data" : [ "node:field-image:title" ], "value" : "[node:title]" } }
    ]
  }
}

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.