Trying to implement your idea from #832046-5: Rules API Changes from 6.x to 7.x.

Inside rules, it works. For the event, the entity is there and if there were a "entity is type" condition (another issue), you could do fun stuff with it :) For granting points action, every entity is listed as a possible data selector (users, private messages, you name it..). BUT..

I'm now inside my action callback function, and I'm getting $entity passed. Trick question: How do I get the type and id of that thing? Because what I have is the raw entity object, not a wrapped object or something like that. I don't think it's possible to write a magic entity_get_type($entity) function because the object probably doesn't contain any information about that. (At least private messages don't)

What about this: Add a new option to the parameter definition and call it 'keep_wrapper' or something like that, defaulting to FALSE. If set to TRUE, the EntityMetadataWrapper class is passed to the action. It's easy to get the id and type of that...

Comments

BenK’s picture

Component: Rules Core » Rules Engine

Since I'm working with Berdir on this, tracking this thread...

fago’s picture

You can access the wrapper by using $state->currentArguments[$parameter_name] - as example have a look at rules_action_data_set(). An option to not unwrap the entity would make sense to me too and shouldn't be hard to do as rules_unwrap_data() already gets the parameter info passed. Maybe better use 'no_unwrap' => TRUE .

Berdir’s picture

Category: support » feature

I can confirm that it works by using currentArguments :)

Adding a no_unwrap option would be nice, but isn't urgent then...

fago’s picture

Title: Identify type of $entity » Add a 'no_unwrap' option to parameters
fago’s picture

Status: Active » Fixed

I've recently added this, it's called 'wrapped' though.

Berdir’s picture

Does that also mean that it is consistent now? :) And what is the default value?

Reason that I'm asking is that I had to use this code a lot "is_object($params['user']->uid) ? $params['user']->getIdentifier() : $params['user']->uid".

EntityValueWrapper has a __toString() method, but that doesn't work when you cast it to an (int).

fago’s picture

>Does that also mean that it is consistent now? :) And what is the default value?
It is FALSE, while for parameters of type 'entity' it's on by default.

I don't see though, when it is not consistent. Please clarify.

Berdir’s picture

Maybe that was changed (that was basically my question ;)) but when I wrote the rules2 integration for privatemsg and userpoints, it depended on where the object came from.

If I remember correctly, if it's a wrapped entity then it is not unwrapped and when it was passed in as a normal entity object, then it was unwrapped.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.