1

I am using the Message module to record some activity.

I have a Rule generating a Message. The Message Type has a couple of fields, and I am setting them in the rule. That part all works great. I can verify the contents of each of those message fields.

Now what I'm trying to do is to conditionally display some things in the message text. I have a couple of Message Text rows and they all display what I expect them to. But for one of these, I want only want to display something if one of the tokens matches a condition.

I have selected the PHP Code filter type on that particular Message Text box, but I just don't know to actually evaluate the field values of the current message. I've tried t() and token_replace() and I just am not getting it.

So here is a simple question for this problem I'm having. Say I have a text field on this message type called field_foo. And I want the message text to display the length of that field. I basically want something to the effect of <?php strlen(this-message:field_foo) ?>.

How do I reference my message instance's field?

  • To clarify: are you asking about how to conditionally display a field (or information that depends on the contents of a field) attached to a custom content type "message"? And when you say "token" do you mean an actual Drupal token: drupal.org/project/token? – Dave Bruns Mar 5 '14 at 17:06
  • I go to Structure > Message Types. I click Edit on my message type. In one of the "Message Text" boxes, I want to put the equivalent of <?php strlen('[message:field_foo']) ?>, but that works and actually outputs the length of that message type field set for that particular message. – franksj Mar 5 '14 at 17:21
  • OK, so you're using the Message module, right? And you're trying to figure out how to evaluate the contents of a text field attached to a message and display something conditionally based on what you find in the field? – Dave Bruns Mar 5 '14 at 17:31
  • Quite so! I'll get the conditional logic part if I can actually figure out how to reference my field vlaues. That's the part I can't seem to crack. – franksj Mar 5 '14 at 17:50
  • The message module provides access to tokens in two forms: field_foo and field-foo (note the hyphen). I believe you should use the hyphenated form when you want to work with the field value. Did you try that? – Dave Bruns Mar 5 '14 at 18:18
0

I couldn't figure out how to do this what I thought was the "right" way - that being inside the message type itself.

Instead, I added a preprocess field from DisplaySuite and put that into my display.

function mytheme_preprocess_message(&$variables) {
    $message = $variables['elements']['#entity'];
    if (isset($message) && $message->type == 'my_custom_message' && isset($message->field_foo) && array_key_exists(LANGUAGE_NONE, $message->field_attachment_themed)) {
        $html = $message->field_fooLANGUAGE_NONE][0]['value'];
        $variables['parsed_output'] = $html;
    }
}

Then I just named my preprocess field parsed_output, and it now displays exactly how I want, and I can add whatever custom code I want to my preprocess_message().

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.