1

Drupal user profile image alt text is showing special characters instead of a quote in the user name. My user name is Derek O'Rourke and the alt text for user profile image is showing Derek O'Rourke's picture"which is not correct.

I couldn't find a way to update it. Please help me.

enter image description here

0

Assuming Drupal 7, in the template.php of your theme, add this function:

function themename_preprocess_user_picture(&$variables) {
  $variables['user_picture'] = htmlspecialchars_decode($variables['user_picture'], ENT_QUOTES);
}

The problem arises from the template_preprocess_user_picture() function in user.module. It passes the alt text through the t function, which in turn calls htmlspecialchars() and it is here the apostrophe is encoded as '

In your theme function, the HTML code of the user picture tag will be available in $variables['user_picture']. Passing the HTML code through htmlspecialchars_decode() will convert ' back to an apostrophe.

  • This is not working for user image. But it is coming correctly when try to echo the image within the above function. – user4026 Feb 23 '18 at 9:49
  • Can you expand on what you mean by 'not working'?. Perhaps some other function is re-encoding the alt text after themename_preprocess_user_picture() has decoded it. Try inspecting the $variables array in the user picture template, user-picture.tpl.php and work back from there. – Frank H. Feb 23 '18 at 11:23
  • I have added themename_preprocess_user_picture() to my template and tried to print within the function and the alt text is coming correctly in most of the places now. But i don't know the user profile page is still gives wrong alt text. – user4026 Feb 26 '18 at 9:04
  • Ah, you didn't mention Display Suite, but the screenshot you added to your question indicates you are using it. There are so many possible explanations it is difficult to pick just one, but I would suggest disabling DS first and see if it works. Then enable DS again and find out what template is being used (drush vset theme_debug might help), then find the corresponding preprocess function and call htmlspecialchars_decode() there. – Frank H. Mar 3 '18 at 17:54
  • I disabled the DS and its working fine. And the issue appears when I enable the DS. While going through the "ds"module I found a function function ds_render_user_picture($field) from where alt and titlle text is updated for Display suite. $alt = t("@user's picture", array('@user' => $name)); $vars = array('path' => $filepath, 'alt' => $alt, 'title' => $alt); I have added htmlspecialchars_decode here but no luck. – user4026 Mar 8 '18 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy