Saturday, January 25, 2014

Life and Death questions: Where to write the copyright in an image tag? In the Alt or in the Title attribute?

One of the important questions in my life is where to write the copyright statement in an Image tag. It's sometimes keeps me awake at night. It is one of those daunting questions which are not answered by one google query. For lots of use cases adding the copyright statement as a caption under the image is not an option.

The copyright sign mirrored

The alt attribute should contain a description, in 150 characters or fewer,  preferably a description in the context of the embedding page, of the visual content of the image. This is the part that mr. Google indexes. So it is the SEO part of the image as well as the visually impaired part, targeted at screenreaders and the like.

The title contains the text that appears in the tooltip when you move your mouse over the image. It should contain additional info, like if the image is linked, where the link will lead you.

The Answer: The Title attribute
As the copyright is not a visual description of the image, you should therefore add it to the title attribute. This is also suggested by w3c.

But on the other hand, as you probably would like to buzz the photographers name around on the internet, and would like the search engines to find all the images belonging to a certain copyright holder, the alt attribute is also used for the copyright statement by lots of sites.  The alt text should never begin with the copyright statement, but it is used a lot.
I do not know if there are any accessibility rules targeting the credit statement specifically for this matter.
Reading all information I suppose that from the viewpoint of accessibility it is a bad solution, but from the viewpoint of the search engines, it is a good solution.

Friday, January 24, 2014

Drupal 7 - specifying different output per role type in a template

Lot's of time I want content or links hidden for unauthenticated users but shown to the site administrators or other user roles. As I always forget how I do it, and then search in old code or ask my old friend mister Google, here's a little blog about how to:

In the template, for instance a field template for a field in a view, add this code.

// load the global user object
global $user;
// Check if  user has (f.i.) the 'administrator' or 'contributor' role.
if (in_array('administrator', $user->roles)||in_array('contributor', $user->roles)) {   
    // do something
} else {
   // do something else

NB! I use this on small sites. It is fast and easy.

For larger sites solution you should define these things using permissions, otherwise the site will in time become unmaintainable.  Adding a permission to another role might then mean changing twenty template files or so.

  // user_access default takes the current logged in user as user object
  if (user_access('permission x')) { 
    // do something
  } else  {
   // do something else

Because setting up the normal permissions may become very complex, you can also add custom permissions with this module and use them in your template file for this kind of functionality.