diff --git a/modules/entity.eval.inc b/modules/entity.eval.inc index 0bea469..5069828 100644 --- a/modules/entity.eval.inc +++ b/modules/entity.eval.inc @@ -172,3 +172,24 @@ function rules_condition_entity_field_access(EntityDrupalWrapper $wrapper, $fiel $field = field_info_field($field_name); return !empty($field) && field_access($op, $field, $wrapper->type(), $wrapper->value(), $account = NULL); } + +/** + * Condition: All required entity fields are present + */ +function rules_condition_entity_required_fields(EntityDrupalWrapper $wrapper) { + $property_info = $wrapper->getPropertyInfo(); + foreach ($property_info as $field_name => $field_info) { + $field_value = $wrapper->{$field_name}->value(); + + // A field is empty if the value is not set or if it is an empty array. + if (!empty($field_info['required']) && + (!isset($field_value) || (is_array($field_value) && empty($field_value))) + ) { + // The field is required but there is no value, so we can return FALSE + // immediately here. + return FALSE; + } + } + // All required fields are present. + return TRUE; +} diff --git a/modules/entity.rules.inc b/modules/entity.rules.inc index 52f1270..e61d751 100644 --- a/modules/entity.rules.inc +++ b/modules/entity.rules.inc @@ -441,6 +441,18 @@ function rules_entity_condition_info() { 'group' => t('Entities'), 'base' => 'rules_condition_entity_field_access', ), + 'entity_required_fields' => array( + 'label' => t('Required fields are present'), + 'parameter' => array( + 'entity' => array( + 'type' => 'entity', + 'label' => t('Entity'), + 'description' => t('Specifies the entity for which to evaluate the condition.'), + ), + ), + 'group' => t('Entities'), + 'base' => 'rules_condition_entity_required_fields', + ), ); }