diff --git a/modules/entity.eval.inc b/modules/entity.eval.inc index 6ae5331..66418e0 100644 --- a/modules/entity.eval.inc +++ b/modules/entity.eval.inc @@ -157,3 +157,10 @@ function rules_condition_entity_has_field($wrapper, $field_name, $settings, $sta function rules_condition_entity_is_of_type($wrapper, $type) { return $wrapper->type() == $type; } + +/** + * Condition: Entity is of type and bundle. + */ +function rules_condition_entity_is_of_type_bundle($wrapper, $type, $bundles) { + return $wrapper->type() == $type && in_array($wrapper->getBundle(), $bundles); +} diff --git a/modules/entity.rules.inc b/modules/entity.rules.inc index 4d16907..b5ce02f 100644 --- a/modules/entity.rules.inc +++ b/modules/entity.rules.inc @@ -251,6 +251,23 @@ function rules_entity_action_type_options($element, $name = NULL) { } /** + * Options list callback for data actions. + * + * @param $element + * The element to return options for. + * @param $param + * The name of the parameter to return options for. + */ +function rules_entity_action_bundle_options($element) { + $entity_info = entity_get_info($element->settings['type']); + $bundles = array(); + foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { + $bundles[$bundle_name] = $bundle_info['label']; + } + return $bundles; +} + +/** * Returns options containing entity types having the given key set in the info. * * Additionally, we exclude all entity types that are marked as configuration. @@ -348,6 +365,32 @@ function rules_entity_condition_info() { 'group' => t('Entities'), 'base' => 'rules_condition_entity_is_of_type', ), + 'entity_is_of_type_bundle' => array( + 'label' => t('Entity is of type and bundle'), + 'parameter' => array( + 'entity' => array( + 'type' => 'entity', + 'label' => t('Entity'), + 'description' => t('Specifies the entity for which to evaluate the condition.'), + ), + 'type' => array( + 'type' => 'token', + 'label' => t('Entity type'), + 'description' => t('The entity type to check for.'), + 'options list' => 'rules_entity_action_type_options', + 'restriction' => 'input', + ), + 'bundle' => array( + 'type' => 'list', + 'label' => t('Entity bundle'), + 'description' => t('The entity bundle to check for.'), + 'options list' => 'rules_entity_action_bundle_options', + 'restriction' => 'input', + ), + ), + 'group' => t('Entities'), + 'base' => 'rules_condition_entity_is_of_type_bundle', + ), ); } @@ -397,5 +440,70 @@ function rules_condition_entity_is_of_type_assertions($element) { } /** + * Assert the selected entity type and bundle. + */ +function rules_condition_entity_is_of_type_bundle_assertions($element) { + $assertions = array('entity' => array()); + if ($type = $element->settings['type']) { + $assertions['entity']['type'] = $type; + } + if ($bundle = $element->settings['bundle']) { + $assertions['entity']['bundle'] = $bundle; + } + return $assertions; +} + +/** + * Form alter callback for the condition entity_is_of_type_bundle. + * + * Use multiple steps to configure the condition as the needed bundle field list + * depends on the selected entity type. + */ +function rules_condition_entity_is_of_type_bundle_form_alter(&$form, &$form_state, $options, RulesAbstractPlugin $element) { + if (empty($element->settings['entity:select'])) { + $step = 1; + } + elseif (empty($element->settings['type'])) { + $step = 2; + } + else { + $step = 3; + } + + $form['reload'] = array( + '#weight' => $form['submit']['#weight'] + 1, + '#type' => 'submit', + '#name' => 'reload', + '#value' => $step != 3 ? t('Continue') : t('Reload form'), + '#limit_validation_errors' => array(array('parameter', 'entity'), array('parameter', 'type')), + '#submit' => array('rules_form_submit_rebuild'), + '#ajax' => rules_ui_form_default_ajax('fade'), + '#attributes' => array('class' => array('rules-hide-js')), + ); + // Use ajax and trigger as the reload button. + $form['parameter']['type']['settings']['type']['#ajax'] = $form['reload']['#ajax'] + array( + 'event' => 'change', + 'trigger_as' => array('name' => 'reload'), + ); + + switch ($step) { + case 1: + $form['reload']['#limit_validation_errors'] = array(array('parameter', 'entity')); + unset($form['parameter']['type']); + unset($form['reload']['#attributes']['class']); + // NO break; + case 2: + $form['negate']['#access'] = FALSE; + unset($form['parameter']['bundle']); + unset($form['submit']); + // NO break; + case 3: + // Complete form. + break; + default: break; + } +} + +/** * @} */