I've been looking a lot at entities and the way Rules uses entities and the Entity API, and I think we should move towards making use of that API with Rules Forms. Fago had mentioned that that was their reason for wanting to separate this module's development - that is, it doesn't use entities. Rules itself is heavily built on entities and Entity API, so passing information to Rules in the form of entities or at least form, form state, and form element Rules data types that resemble entities with appropriate properties will greatly improve integration with Rules. Whether this should be developed and released as a separate version of the module will depend on whether backwards compatibility can be maintained. A lot of the changes may deal more with how Rules perceives the data we are passing than how the user will interact with that data within the Rules interface.

Comments

jordojuice’s picture

Status: Active » Needs work

This commit:
http://drupalcode.org/project/rules_forms.git/commit/1aabbcd
Is a start towards entity integration. It's in the 2.x branch because this will have to go in a second version of the module. This defines the form and form_state entity types, and each active form is a bundle of that type. When form elements are evaluated on the form, they are stored in the form of entity information and used in hook_entity_property_info() to define the structure of the form. This allows the user to traverse the form to set data values. Each form element property can be set with the Set a data value action. It has been tested and it works, but this means we need to figure out what conditions or actions Rules Forms does need to provide. Some to consider:
- Set a form property: The data selector can be used to traverse the form to whatever property the user wants as I mentioned, but this makes it more clear than the set a data value action.
- Form property has value
- Set the redirect target of the form
- Form button was clicked
The very flexible set a data value action seems to take care of a lot of what the user might want to do, so good documentation will make this module very powerful.

Still needs a lot of work and cleaning up though.

freddura’s picture

For those who would like to access the entity being edited within their rules workflow, here is a temporary workaround:

First download and enabled Rules Data Transforms, then within the configuration for your rules form support rule do the following

Add the action: "Explode text."
Set "Input Text" = "site:current-page:path" using data selector.
Set "delimiter" = "/"
"Provided variables" should = "output_list"
Click save

Add the action: "Convert data type."
Set "target type" = "integer"
Click continue
Set "value to convert" = "output-list:1" using the data selector (This is assuming your path = node/*/edit where * = node-id. If you were editing a term, for example, you would select output-list:2 because the path for editing terms = vocabulary/taxonomy/*/edit where * = term-id)
"Provided variables" should = "conversion_results"
Click save

Now add an action "Fetch entity by id"
Select your entity type
Click continue
Set "Identifier" = "conversion-results" using the data selector
"Provided variables" should = "entity_fetched"
Click save

You have now added Entity support to you Rules Form Support rule! If you want to do anything special with the entity (such as with its fields), you will need to add conditions. In order to add conditions, you must create a "rules component" action and pass the "entity_fetched" variable into this component as a parameter variable. This component must already have been created and the "component plugin" must = "rule". Within this component you may add any conditions and actions as needed (such as the "entity has field" condition...) You can also pass back results of this rules component to the rule as "provided" variables....

Not the ideal integration that version2 may be working towards, but a workaround none the less, and is quiet flexible!

PatchRanger’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Needs work » Active
darrick’s picture

FWIW: I wanted to be able to use the conditions such as "Entity is of Type". I'm also writing some conditions which use Entity and Field Names as parameters as it's a bit more intuitive in the Rules UI to select those rather then form, form_state and form_element.

Found it easiest to use hook_rules_event_info_alter(&$events) to add the Entity object to the variables array for the rules_forms events. I'm unsure on how to make it apply only to forms which only edit entities.

function merci_rules_rules_event_info_alter(&$events) {
  $form_events = variable_get('rules_forms_event_info', array());
  foreach ($form_events as $form_id => $info) {
    $events['rules_forms_' . $form_id . '_form_validate']['variables']['entity'] = array(
      'type' => 'entity',
      'label' => 'Entity',
      'handler' => 'merci_rules_events_variable_entity_load',
    ); 
  }
}

function merci_rules_events_variable_entity_load($arguments, $variable_name, $variable_info) {
  $entity = $arguments['form_state']['values'];
  $entity_type = array_key_exists('#entity_type', $arguments['form']) ? $arguments['form']['#entity_type'] : NULL;
  if (empty($entity_type)) {
    return NULL;
  } 
  return entity_metadata_wrapper($entity_type, (object) $entity);
}
kenorb’s picture