0

I would like to rewrite the original function code for the Private messages block module. The original code displays a text message for no messages or messages in the inbox. I would like to change it to an image but am no where near on the right track. Here is the coded that I am trying to change

 /**
 * Theme a block which displays the number of new messages a user has.
 */
function theme_privatemsg_new_block($count) {
  $count = $count['count'];
  if ($count == 0) {
    $text = t('0 Messages');
  }
  else {
    $text = format_plural($count, 'You have a new message!',
                        'You have @count new messages!');
  }

  return l($text, 'messages', array('attributes' => array('id' => 'privatemsg-new-link')));
}

I was trying the following replacement:

/**
 * Theme a block which displays the number of new messages a user has.
 */
function theme_privatemsg_new_block($count) {
  $count = $count['count'];
  if ($count == 0) {
    /*$text = t('0 Messages');*/
    $text = l('<img src="messages.png" height="40"/>);
  }
  else {
    /*$text = format_plural($count, 'You have a new message!',
                        'You have @count new messages!');*/
    $text = l('<img src="messages.png" height="40"/>');                   
  }

  return l($text, 'messages', array('attributes' => array('id' => 'privatemsg-new-link')));
}

I suspect there are issues with the fact that it is designated as a text string instead of code that should be rendered. What do you think?

Using PHP I am trying:

/**
 * Theme a block which displays the number of new messages a user has.
 */
function theme_privatemsg_new_block($count) {
  $count = $count['count'];
  if ($count == 0) {
    /*$text = t('0 Messages');*/
    $theme_path = drupal_get_path('theme', 'eternal'));
    $text = '<img src="' . $base_path . '/' . $theme_path . '/images/messages.png" width="50"/>';
  }
  else {
    /*$text = format_plural($count, 'You have a new message!',
                        'You have @count new messages!');*/
    $theme_path = drupal_get_path('theme', 'eternal'));                    
    $text = '<img src="' . $base_path . '/' . $theme_path . '/images/messages.png" width="50"/>';                   
  }

  return l($text, 'messages', array('attributes' => array('id' => 'privatemsg-new-link'), 'html' => TRUE));
}
1

$text = l('<img src="messages.png" height="40"/>); looks wrong. "l" is function to create links, i guess thats what is failing here. Try to remove it and leave just $text = '<img src="messages.png" height="40"/>';.

For reference on "l" see drupal api page - please note that there is also 'HTML' parametr in "l" function that you should set to true, since you are passing HTLM code in $text to that returning "l".

'html' (default FALSE): Whether $text is HTML or just plain-text. For example, to make an image tag into a link, this must be set to TRUE, or you will see the escaped HTML image tag. $text is not sanitized if 'html' is TRUE. The calling function must ensure that $text is already safe.

|improve this answer|||||
  • I had tried that before but it just literally prints the html code instead of rendering the image – Cybergei Jul 20 '15 at 6:36
  • Did you modified return? return l($text, 'messages', array('attributes' => array('id' => 'privatemsg-new-link'), 'html' => TRUE)); ? – tdd Jul 20 '15 at 7:11
  • nope, looks like the html true was the final key. Thank you very much – Cybergei Jul 20 '15 at 7:57
  • One more question, can I make it php also? – Cybergei Jul 20 '15 at 8:45
  • You can put any PHP code inside your theme_privatemsg_new_block function that will output text/HTML to $text variable (instead of static string '<img src="messages.png" height="40"/>' you have there now). – tdd Jul 20 '15 at 8:58

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.