I know that alt text is usually used for images, to help screen readers etc. In this case my task manager wants me to add 'alternative text' to calendar items, to better satisfy 508 requirements. This would be for a custom content type called Event, which is called up in a view. The alt text would be entered by a user, and may or may not link to anything. I'm just trying to create a field that is wrapped in an alt tag, so it doesn't display but screen readers will find it. I've tried different things, such as adding dummy text into the .tpl file for calendar item, but I can't come up with a good variable for $title that works. I've tried using a php print statement in the .tpl but again, haven't found the right code. I'm thinking I will probably have to do surgery on the custom event module itself, which has a form for people to enter in the event data. One solution is to use hook_form_alter to add in some code, something like this:

function events_form_alter(&$form_state, $form_id) { if ($form_id == 'events_form') { <?php print $title . "text"; ?> } }

One thing is that I think the best place to find an event title variable is in an include file, where most of the module code lives. I can see #title being used in the include, but not sure what that is. A local variable? How can I use that variable to print in my alt text? I'm hoping I can use this variable in a hook_form_alter function that prints a statement using this title. From the include file:

    // Add an end and a date repeat form element, if needed.
if ($element ['#bundle'] == 'event') {
    if($element['#field_name'] == 'field_event_times_combined') {
        $element ['#title'] = t ( 'Date & Time' );

But all this is close to exceeding my PHP skills, however. Does anybody have any general insight?

Many thanks

  • Which drupal version are you referring to? Aug 10, 2018 at 19:18
  • @chadpeppers Use of .tpl files plus the concept of bundles and field names is probably good indication it’s D7
    – Clive
    Aug 11, 2018 at 9:13

1 Answer 1


I don't think your approach will work, adding the new "alt" field to the edit/add form is only a small part of the task. Where and how you save the data? Instead, create a new field via the manage field section. Then you can use theme_preprocess or a tlp/twig file to change the output.

  • @garethallnz Thanks for that tip. I'm now thinking I could print the Description field in the View, then somehow inject the alt tags. Views gives me some fancy options. But I should bite the bullet and try a preprocessor function, something I've not used before.
    – P Tocco
    Aug 13, 2018 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.