In message module I've defined for the metadata:

    $properties['arguments'] = array(
      'label' => t("Arguments"),
      'type' => 'text',
      'description' => t("The arguments to associate with the message."),
      'getter callback' => 'entity_metadata_verbatim_get',
      'setter callback' => 'entity_metadata_verbatim_set',
    );

However, I'd like the data that is inputed to be transformed into an array. In D6, using rules I had this function to execute the user's input via Rules form:


function _message_rules_get_php_value($settings, &$state) {
  $value = '';
  if ($settings['code']) {
    if (function_exists('rules_input_evaluator_php_apply')) {
      // Support adding variables to the php code, if php module is present.
      $value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE);
    }
    else {
      ob_start();
      $value = eval($settings['code']);
      ob_end_clean();
    }
  }
  return $value;
}

The question - is there a similar way for me to process the user's input as possible PHP code? To clarify - in D6 the user could insert "simple" text in one text area, and PHP code in another.

Comments

fago’s picture

Title: How to get value as an array » Arguments rules integration
Project: Entity API » Message
Version: 7.x-1.x-dev » 6.x-1.x-dev
Component: Entity Metadata - main » Code
Category: support » task

I'd not suggest going with PHP for message - having to use PHP is always bad. You could do a 'text' input and enable the PHP module, then it's automatically there though.

Anyway, I'd suggest allowing user to add X-arguments (similar rules allows adding variables to a rule component), however we know the supposed data-type - 'text'). Then use the _info_alter() callback to these additional arguments to the parameter of the action and rebuild the form, then Rules will render additional parameters people can configure using the data selector.

btw, could we get a 7.x branch pls? :)

fago’s picture

Status: Active » Needs review

I took a stab on that -> https://github.com/fago/message/commits/DRUPAL-7--1
In my tests it worked fine. For testing I've used the following message and rule:

{
  "name" : "node_has_been_created",
  "description" : "Node has been created",
  "argument_keys" : [ "%title", "!url" ],
  "message_text" : { "und" : [
      {
        "value" : "\u003ca href=\"!url\"\u003e%title\u003c\/a\u003e has been created.",
        "format" : null,
        "safe_value" : "\u0026lt;a href=\u0026quot;!url\u0026quot;\u0026gt;%title\u0026lt;\/a\u0026gt; has been created."
      }
    ]
  },
  "rdf_mapping" : []
}

and the rule:

{ "rules_log_node_creation" : {
    "LABEL" : "Log node creation",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "node_insert" ],
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "message",
            "param_name" : "node_has_been_created",
            "param_user" : [ "site:current-user" ]
          },
          "PROVIDE" : { "entity_created" : { "message" : "Message" } }
        }
      },
      { "data_set" : { "data" : [ "message:arguments:title" ], "value" : "[node:title]" } },
      { "data_set" : { "data" : [ "message:arguments:url" ], "value" : "[node:url]" } }
    ]
  }
}

If that's ok, I'd have a look at improve the Rules related tests to cover argument creation too.

fago’s picture

oh, I forgot my changes require the latest entity API and Rules code as I discovered some small bugs while working on it.

amitaibu’s picture

Status: Needs review » Reviewed & tested by the community

Thanks fago!
While I'm not "crazy" on the idea to add arguments keys (from UX view point it feels as duplication), the integration with entity-metadata is important enough to let it go. I've committed to github, and will sync back to Drupal after the git migration.

btw, for ones trying to import the rule from #2, make sure to install token module.

fago’s picture

Thanks!

>btw, for ones trying to import the rule from #2, make sure to install token module.
It should not required that, as the token API is on core.

>from UX view point it feels as duplication
hm, how do you mean that? Why is it duplicated? Because the user has to enter the replacement tokens in the text message to? The problem is that we cannot really know something needs to be replaced just because there is a @foo in the text, while it will be the case usually. Maybe we could provide some JS that automatically populates the argument keys textfields with that as suggestions though.

amitaibu’s picture

> It should not required that, as the token API is on core.

weird, It didn't allow me to import before.

> Why is it duplicated? Because the user has to enter the replacement tokens in the text message to?

Yes.

> Maybe we could provide some JS that automatically populates the argument keys textfields with that as suggestions though.

I've been thinking about it, but it might be hard to accomplish :)

  • 18e700a committed on 8.x-1.x
    #989838 by fago: Added arguments rules integration.
    
    
bluegeek9’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Closed (won't fix)