reverted: --- date_validation/plugins/validator/field_validation_date_field_values_validator.inc 2012-07-06 11:42:40.000000000 +0100 +++ /dev/null 2012-07-06 11:47:03.000000000 +0100 @@ -1,106 +0,0 @@ - t('Date validation against another date field'), - 'description' => t("Validates user-entered date against another separated date field. For example field_date1 is greater than field_date2."), - 'handler' => array( - 'class' => 'field_validation_date_field_values_validator', - ), -); - -class field_validation_date_field_values_validator extends field_validation_validator { - - /** - * Validate field. - */ - public function validate() { - $settings = $this->rule->settings; - - if ($this->value != '') { - $flag = TRUE; - - $query = new EntityFieldQuery(); - !empty($settings['entity_type']) ? $query->entityCondition('entity_type', $settings['entity_type']) : NULL; - !empty($settings['bundle']) ? $query->entityCondition('bundle', $settings['bundle']) : NULL; - !empty($settings['field']) ? $query->fieldCondition($settings['field'], 'value', $this->value, $settings['operator']) : NULL; - $flag = (bool)$query->range(0, 1)->count()->execute(); - - $flag = !empty($settings['reverse']) ? !$flag : $flag; // reverse flag value if option is set - !$flag ? $this->set_error() : NULL; - } - } - - /** - * Provide settings option - */ - function settings_form(&$form, &$form_state) { - $default_settings = $this->get_default_settings($form, $form_state); - - $entity_info = entity_get_info(); // get all entities - $entities = array_filter($entity_info, function($a) { return $a['fieldable']; }); // filter only fieldable - $entities = drupal_map_assoc(array_keys($entities)); // return only the key names - $form['settings']['entity_type'] = array( - '#title' => t('Entity type'), - '#description' => t("Machine name. Entity type of the property that to be matched against."), - '#type' => 'select', - '#default_value' => isset($default_settings['entity_type']) ? $default_settings['entity_type'] : '', - '#options' => array('' => t('Choose the entity type')) + $entities, - // '#attributes' => array('onchange' => 'this.form.submit();'), - '#attributes' => array('onchange' => 'this.form.bundle.options = 1;'), - ); - - $bundles = $entity_info['node']['bundles']; - $bundles = drupal_map_assoc(array_keys($bundles)); // return only the key names - $form['settings']['bundle'] = array( - '#title' => t('Bundle'), - '#description' => t("Machine name. Bundle of the property that to be matched against."), - '#type' => 'select', - '#default_value' => isset($default_settings['bundle']) ? $default_settings['bundle'] : '', - '#options' => array('' => t('Choose the bundle')) + $bundles, - ); - - $fields = field_info_fields(); - $fields = drupal_map_assoc(array_keys($fields)); // return only the key names - $form['settings']['field'] = array( - '#title' => t('Field'), - '#description' => t("Name of the Field that to be matched against."), - '#type' => 'select', - '#default_value' => isset($default_settings['field']) ? $default_settings['field'] : '', - '#options' => array('' => t('Choose the field')) + $fields, - ); - - $operators = array( - '<' => t('Is less than'), - '<=' => t('Is less than or equal to'), - '=' => t('Is equal to'), - '!=' => t('Is not equal to'), - '>=' => t('Is greater than or equal to'), - '>' => t('Is greater than'), - // 'between' => t('Is between'), - // 'not between' => t('Is not between'), - ); - - $form['settings']['operator'] = array( - '#title' => t('Operator'), - '#description' => t("Choose the operator for the selected field."), - '#type' => 'select', - '#default_value' => isset($default_settings['operator']) ? $default_settings['operator'] : '', - '#options' => array('' => t('Choose the operator')) + $operators, - ); - - $form['settings']['reverse'] = array( - '#title' => t('Reverse'), - '#description' => t("If it is checked, it means must not match the field."), - '#type' => 'checkbox', - '#default_value' => isset($default_settings['reverse']) ? $default_settings['reverse'] : FALSE, - ); - - parent::settings_form($form, $form_state); - } - -} - only in patch2: unchanged: --- /dev/null +++ b/plugins/validator/field_validation_date_field_values_validator.inc @@ -0,0 +1,130 @@ + t('Date validation against another date field'), + 'description' => t("Validates user-entered date against another separated date field. For example field_date1 is greater than field_date2."), + 'handler' => array( + 'class' => 'field_validation_date_field_values_validator', + ), +); + +class field_validation_date_field_values_validator extends field_validation_validator { + + /** + * Validate field. + */ + public function validate() { + $settings = $this->rule->settings; + + if ($this->value != '') { + $flag = TRUE; + $wrapper = entity_metadata_wrapper($settings['entity_type'], $this->entity); + $matched_value = $wrapper->{$settings['field']}->value(); + $current_value = strtotime($this->value); + + switch ($settings['operator']) { + case '<': + $flag = $matched_value < $current_value; + break; + + case '<=': + $flag = $matched_value <= $current_value; + break; + + case '=': + $flag = $matched_value == $current_value; + break; + + case '!=': + $flag = $matched_value != $current_value; + break; + + case '>=': + $flag = $matched_value >= $current_value; + break; + + case '>': + $flag = $matched_value > $current_value; + break; + + } + + $flag = !empty($settings['reverse']) ? !$flag : $flag; // reverse flag value if option is set + !$flag ? $this->set_error() : NULL; + } + } + + /** + * Provide settings option + */ + function settings_form(&$form, &$form_state) { + $default_settings = $this->get_default_settings($form, $form_state); + + $entity_info = entity_get_info(); // get all entities + $entities = array_filter($entity_info, function($a) { return $a['fieldable']; }); // filter only fieldable + $entities = drupal_map_assoc(array_keys($entities)); // return only the key names + $form['settings']['entity_type'] = array( + '#title' => t('Entity type'), + '#description' => t("Machine name. Entity type of the property that to be matched against."), + '#type' => 'select', + '#default_value' => isset($default_settings['entity_type']) ? $default_settings['entity_type'] : '', + '#options' => array('' => t('Choose the entity type')) + $entities, + // '#attributes' => array('onchange' => 'this.form.submit();'), + '#attributes' => array('onchange' => 'this.form.bundle.options = 1;'), + ); + + $bundles = $entity_info['node']['bundles']; + $bundles = drupal_map_assoc(array_keys($bundles)); // return only the key names + $form['settings']['bundle'] = array( + '#title' => t('Bundle'), + '#description' => t("Machine name. Bundle of the property that to be matched against."), + '#type' => 'select', + '#default_value' => isset($default_settings['bundle']) ? $default_settings['bundle'] : '', + '#options' => array('' => t('Choose the bundle')) + $bundles, + ); + + $fields = field_info_fields(); + $fields = drupal_map_assoc(array_keys($fields)); // return only the key names + $form['settings']['field'] = array( + '#title' => t('Field'), + '#description' => t("Name of the Field that to be matched against."), + '#type' => 'select', + '#default_value' => isset($default_settings['field']) ? $default_settings['field'] : '', + '#options' => array('' => t('Choose the field')) + $fields, + ); + + $operators = array( + '<' => t('Is less than'), + '<=' => t('Is less than or equal to'), + '=' => t('Is equal to'), + '!=' => t('Is not equal to'), + '>=' => t('Is greater than or equal to'), + '>' => t('Is greater than'), + // 'between' => t('Is between'), + // 'not between' => t('Is not between'), + ); + + $form['settings']['operator'] = array( + '#title' => t('Operator'), + '#description' => t("Choose the operator for the selected field."), + '#type' => 'select', + '#default_value' => isset($default_settings['operator']) ? $default_settings['operator'] : '', + '#options' => array('' => t('Choose the operator')) + $operators, + ); + + $form['settings']['reverse'] = array( + '#title' => t('Reverse'), + '#description' => t("If it is checked, it means must not match the field."), + '#type' => 'checkbox', + '#default_value' => isset($default_settings['reverse']) ? $default_settings['reverse'] : FALSE, + ); + + parent::settings_form($form, $form_state); + } + +} +