1

I need to, given the mid (= Message ID), generate the Message content for a specific view mode. How can I do this?

It seems to be complicated by the fact that a Message is an entity (created by the Message module), and there are multiple Message types, each of which have their own fields and rely heavily on tokens.

I'm hoping for something simple like:

$message = message_load($mid, $view_mode);
$output = drupal_render(entity_view($message));

... but everything I've found that loads Messages seems to rely on weird wrappers and stuff and I haven't been able to find a working solution no matter how much I tinker with it.

1

there's a better one:

$build = $message->buildContent('my_view_mode');
$output = drupal_render($build);
3

Got it.

$message = message_load($mid);
$build = $message->view();
$build += array(
  '#theme' => 'message',
  '#entity' => $message,
  '#view_mode' => 'full',
);  
$output = drupal_render($build);
  • not sure where you found the view() method, this is probably from Entity itself? Message class has a own method for that. – Andre Baumeier Dec 16 '13 at 17:07

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.