Sometimes we want to use some configuration data and it should not be stored as a literal in the rule but in a central place.
think of "email address for shop administrator" that is used in 17 rules actions.

it would be nice to have this stored in a drupal system variable and administer it using variable module.

seems there is currently no way to do this or am i missing something?

Comments

geek-merlin’s picture

thought a bit about it and maybe the easiest way ist to add an entity definition for the variable table.

edit: of course we would need a numeric id for that...

edit: no, that's no good - a variable can have a default value although nothing is in the db.

geek-merlin’s picture

Title: Make Drupal variables accessible » Make Drupal variables accessible with data browser
Priority: Normal » Minor

update: variable.module provides tokens so we can do it that way.

so this is only about using data browser to access variable values which surely has minor prio or is wontfix.

fago’s picture

Status: Active » Fixed

yep, also it would be great if the variable module would provide variables as "entity properties" via hook_entity_property_info(). There is a special 'site' key for such global data.

geek-merlin’s picture

Title: Make Drupal variables accessible with data browser » Make Drupal variables accessible with data browser in rules
Project: Rules » Variable
Version: 7.x-2.x-dev » 7.x-1.x-dev
Component: Miscellaneous » Code
Status: Fixed » Active

right. so moving this to variable module.

jose reyero’s picture

Status: Active » Postponed (maintainer needs more info)

Sounds interesting, where can we find that 'site' entity?

jose reyero’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Closing, no follow up. Feel free to reopen.

tedbow’s picture

I am attaching a patch that starts off this. It implenents hook_entity_property_info_alter

http://drupalcontrib.org/api/drupal/contributions!entity!entity.api.php/...

It would need a lot more work but at least works form showing the variable under "site" in the data browser

Basically it exposes the variables as metadata properties of the "site" entity.

The getter callback will need to be more dynamic and the will need 'type' of the properties based on the variable type key.

I have tested this for showing a message via a rule that displays a simple string variable('site_frontpage').

tedbow’s picture

Status: Closed (won't fix) » Needs work

Meant to reopen

tedbow’s picture

Ok, I added the setter method to the metadata properties. This allows setting the variables through Rules.

I tested it by setting a string variable.

mitchell’s picture

Priority: Minor » Normal
Issue tags: +rules integration
StatusFileSize
new111.01 KB

Wow. This is cool! tedbow++

I have a few questions:
* Should variable_entity_property_info_alter check if Entity is installed?
* Would it be possible/desirable for site variables to be in a subsection as [site:variables], similar to [site:current-page]?
* Would it be possible to declare data types for these text and integer values, or would that be better to do separate from this wrapper?

I noticed a few of errors/unexpected behaviors:
* Notice: Undefined index: description in variable_entity_property_info_alter() (line 810 of .../variable/variable.module). when adding a 'drupal_message' action, but it seemed to go away. It then reappeared, and I think 12x was the common pattern. (shown in screenshot)
* Another error, that I was only able produce once, while setting [site:preprocess-css] was like 'Data value is required. \n Data parameter is not set.'
* When [site:preprocess-css] was set to 0, 'drupal_message' did not display anything.

Attached is a screenshot of this patch in action. Note: to make a screenshot that included the data selector dropdown, I had to do Alt+H (which opened another menu in the background) and then do print screen.

tedbow’s picture

Wow. This is cool! tedbow++

Thanks

* Should variable_entity_property_info_alter check if Entity is installed?

I don't think so becasue the hook is defined in Entity so it would also be enable if called

* Would it be possible/desirable for site variables to be in a subsection as [site:variables], similar to [site:current-page]?

Yes I think that is good idea. The list gets very long.

* Would it be possible to declare data types for these text and integer values, or would that be better to do separate from this wrapper?

This will need to be done. Basically we to have some translation from what "variable_build_info" returns and what "hook_entity_property_info_alter" expects with properties.

Right now i don't have time to get back to this. If someone else wants to tackle it I can help.

jose reyero’s picture

Looks great, I'll be committing it as soon as all these issues are fixed.

Some issues:
- The list of variables should be retrieved with: variable_get_info() instead of variable_build_info()
- Some of Variable module variable names are group names (like 'node_options_[node_type]'), these ones should be built with variable_build() and then check for $variable['children'].
- Typo in function name: variable_meTATdata_set_property(
- This function should be using variable_set_value(), to go through full variable API, instead of variable_set(), I guess

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new1.41 KB

Ok another try at this

* Would it be possible/desirable for site variables to be in a subsection as [site:variables], similar to [site:current-page]?

I didn't do this. This is actually must harder than I thought. It works fine for view values but not for setting them. You get a error "Unable to set the data property xxxx as the parent data structure is not set. Rules expects the direct parent to be a entity of some sort I think. I similar problem is described here #1696594: Setting data to double field through rules.

[site:current-page] works because the properties underneath it are read-only.

If anyone wants to look into it the problem comes from EntityStructureWrapper->setproperty() in the entity.wrapper.inc.

The current method works it just causes a long list under "site:" in the data browser. There is going to be long list somewhere b/c of the number of variables.

I fixed the other issues in #12 above.

Marking as "needs review" to get feedback. But really it needs more work probably.

mitchell’s picture

Status: Needs review » Needs work
StatusFileSize
new584 bytes

The error messages are fixed.

I'm still getting this unexpected behavior:
* When 'Aggregate and compress CSS files' is disabled, 'Show a system message' of [site:preprocess-css] does not display anything.
I believe the attached interdiff from #1540180: Add a property for user's default theme may help a bit.

> error "Unable to set the data property xxxx as the parent data structure is not set'.
AFAIK, the source of this error is a configuration element" which receives a data type different from what the plugin supports.

Rules' configurations are oriented in a nest / tree structure, and parent elements expose values through property chains. For example, the 'Entity has field' condition exposes a field's values to child actions in the configuration.

tedbow’s picture

@mitchell

I'm still getting this unexpected behavior:
* When 'Aggregate and compress CSS files' is disabled, 'Show a system message' of [site:preprocess-css] does not display anything.

I think is expected. The value you are trying to display == 0. Because of the way drupal_set_message is coded "0" will not be displayed.
You can test this my making a rule with an action to display a message and just put 0 (zero) in. It will not be displayed. also the code:
drupal_set_message(0);
Will also not display.

tedbow’s picture

@mitchell do have an idea how to solve the issue from #13/#14?

mitchell’s picture

Hmm, I think a good workaround would be to declare a new site-wide ‘variable’ faux-entity.

geek-merlin’s picture

I think the current "spirit" of rules data would be a "variable" property of "site, making it [site:variable:foobar]

eileenmcnaughton’s picture

i just wanted to link this across

http://drupal.org/node/812058#comment-6536478

From what I can see it doesn't allow you to use tokens in the comparison operator (which is how I hoped to use it - Data (Integer) is one of [variable:myselecteditems]) - however, it does indicate the syntax rules module is following ie.

[variable:value]

From where I am I think I am hitting 2 short-comings - 1) Rules is now supporting tokens - but not for the list type & 2 it isn't supporting them for data comparison. (I'm planning on backing off my approach for now - I was just trying to find a way to let people select some criteria outside the rule feature I'm setting up)

turion’s picture

Issue summary: View changes

Is this now being worked currently?

geek-merlin’s picture