commit e69073a6a4cb37db57e92356c60c81705714d1c7 Author: Pieter Frenssen Date: Wed May 28 22:39:51 2014 +0200 1837650-39 diff --git a/entityreference.module b/entityreference.module index a19fc51..6d8c95d 100644 --- a/entityreference.module +++ b/entityreference.module @@ -273,22 +273,23 @@ function entityreference_field_presave($entity_type, $entity, $field, $instance, if (!$referenced_entity_revision) { $referenced_entity_revision = entity_load_single($target_type, $item['target_id']); } - $ref_ids = entity_extract_ids($target_type, $referenced_entity_revision); - // find the original item, which does not have the same delta per se. - $original_revision = FALSE; + list(, $referenced_entity_revision_id) = entity_extract_ids($target_type, $referenced_entity_revision); + // Find the original item, which does not have the same delta per se. + $original_revision_id = FALSE; foreach ($original_items as $original_item) { if ($original_item['target_id'] == $item['target_id']) { - $original_revision = isset($original_item['revision_id']) ? $original_item['revision_id'] : FALSE; + $original_revision_id = isset($original_item['revision_id']) ? $original_item['revision_id'] : FALSE; } } - if (isset($ref_ids[1]) && is_numeric($ref_ids[1]) && !$original_revision) { - $items[$key]['revision_id'] = $ref_ids[1]; + if (isset($referenced_entity_revision_id) && is_numeric($referenced_entity_revision_id) && !$original_revision_id) { + $items[$key]['revision_id'] = $referenced_entity_revision_id; } - else if ($original_revision) { - $items[$key]['revision_id'] = $original_revision; + elseif ($original_revision_id) { + $items[$key]['revision_id'] = $original_revision_id; } } } + // Invoke the behaviors. foreach (entityreference_get_behavior_handlers($field, $instance) as $handler) { $handler->presave($entity_type, $entity, $field, $instance, $langcode, $items); @@ -733,6 +734,9 @@ function entityreference_field_property_callback(&$info, $entity_type, $field, $ } } +/** + * Entity metadata getter callback. Returns the referenced revision. + */ function entityreference_metadata_field_get_revision_data($entity, array $options, $name, $entity_type, $info){ $field = field_info_field($name); $langcode = isset($options['language']) ? $options['language']->language : LANGUAGE_NONE; @@ -752,6 +756,9 @@ function entityreference_metadata_field_get_revision_data($entity, array $option return $field['cardinality'] == 1 ? ($values ? reset($values) : NULL) : $values; } +/** + * Entity metadata setter callback. Sets the referenced revision. + */ function entityreference_metadata_field_set_revision_data($entity, $name, $value, $langcode, $entity_type, $info) { $field = field_info_field($name); $langcode = entity_metadata_field_get_language($entity_type, $entity, $field, $langcode); diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 6b4076c..8afbc8a 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -149,14 +149,20 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select ); } - // Provide an option to lock the entity reference to the current revision if - // the entity supports it. + // Provide options to reference revisions if the entity supports it. if (!empty($entity_info['revision table'])) { + $form['reference_revisions'] = array( + '#type' => 'checkbox', + '#title' => t('Reference revisions'), + '#default_value' => !empty($field['settings']['handler_settings']['reference_revisions']), + '#description' => t('When this is enabled, the reference will track the current revision at the time it is referenced. When disabled the reference will always point to the newest revision of the entity.'), + ); $form['lock_revision'] = array( '#type' => 'checkbox', - '#title' => t('Lock the field to the revision of the entity at the time it was referenced.'), - '#default_value' => !empty($field['settings']['handler_settings']['lock_revision']) ? TRUE : FALSE, - '#description' => t('When this is enabled, the reference will track the latest revision to that entity when this field is saved. This, combined with e.g. the Workbench Moderation module, can be used to provide limited workflow functionality around the referenced content.', array('!url' => 'http://drupal.org/project/workbench_moderation')) + '#title' => t('Lock the revision.'), + '#default_value' => !empty($field['settings']['handler_settings']['lock_revision']), + '#description' => t('Locks the field to the revision of the entity at the time it was referenced. If this is disabled the revision will be updated each time the referencing entity is saved.'), + '#states' => array('visible' => array(':input[name="field[settings][handler_settings][reference_revisions]"]' => array('checked' => TRUE))), ); } diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php index 60e569c..607531e 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -69,14 +69,20 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio ); } - // Provide an option to lock the entity reference to the latest revision - // if the entity supports it. + // Provide options to reference revisions if the entity supports it. if (!empty($entity_info['revision table'])) { + $form['reference_revisions'] = array( + '#type' => 'checkbox', + '#title' => t('Reference revisions'), + '#default_value' => !empty($field['settings']['handler_settings']['reference_revisions']), + '#description' => t('When this is enabled, the reference will track the current revision at the time it is referenced. When disabled the reference will always point to the newest revision of the entity.'), + ); $form['lock_revision'] = array( '#type' => 'checkbox', - '#title' => t('Lock the field to the revision of the entity at the time it was referenced.'), - '#default_value' => !empty($field['settings']['handler_settings']['lock_revision']) ? TRUE : FALSE, - '#description' => t('When this is enabled, the reference will track the latest revision to that entity when this field is saved. This, combined with e.g. the Workbench Moderation module, can be used to provide limited workflow functionality around the referenced content.', array('!url' => 'http://drupal.org/project/workbench_moderation')) + '#title' => t('Lock the revision.'), + '#default_value' => !empty($field['settings']['handler_settings']['lock_revision']), + '#description' => t('Locks the field to the revision of the entity at the time it was referenced. If this is disabled the revision will be updated each time the referencing entity is saved.'), + '#states' => array('visible' => array(':input[name="field[settings][handler_settings][reference_revisions]"]' => array('checked' => TRUE))), ); } diff --git a/tests/entityreference.admin.test b/tests/entityreference.admin.test index 9a12119..e8bfe62 100644 --- a/tests/entityreference.admin.test +++ b/tests/entityreference.admin.test @@ -74,13 +74,17 @@ class EntityReferenceAdminTestCase extends DrupalWebTestCase { // The base handler should be selected by default. $this->assertFieldByName('field[settings][handler]', 'base'); - // The base handler settings should be diplayed. + // The base handler settings should be displayed. $entity_type = 'node'; $entity_info = entity_get_info($entity_type); foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) { $this->assertFieldByName('field[settings][handler_settings][target_bundles][' . $bundle_name . ']'); } + // The revision settings should be displayed since nodes support revisions. + $this->assertFieldByName('field[settings][handler_settings][reference_revisions]', 1); + $this->assertFieldByName('field[settings][handler_settings][lock_revision]', 1); + // Test the sort settings. $options = array('none', 'property', 'field'); $this->assertFieldSelectOptions('field[settings][handler_settings][sort][type]', $options); @@ -110,5 +114,17 @@ class EntityReferenceAdminTestCase extends DrupalWebTestCase { // Check that the field appears in the overview form. $this->assertFieldByXPath('//table[@id="field-overview"]//td[1]', 'Test label', t('Field was created and appears in the overview page.')); + + // Test that the revision options are not shown for an entity type that does + // not support revisions. + $this->drupalPost($bundle_path . '/fields', array( + 'fields[_add_new_field][label]' => 'User', + 'fields[_add_new_field][field_name]' => 'test_user', + 'fields[_add_new_field][type]' => 'entityreference', + 'fields[_add_new_field][widget_type]' => 'entityreference_autocomplete', + ), t('Save')); + $this->drupalPostAJAX(NULL, array('field[settings][target_type]' => 'user'), 'field[settings][target_type]'); + $this->assertNoFieldByName('field[settings][handler_settings][reference_revisions]', 1); + $this->assertNoFieldByName('field[settings][handler_settings][lock_revision]', 1); } }