diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index 3566121..459f12d 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -29,12 +29,27 @@ function entity_reference_field_info() { ), 'default_widget' => 'entity_reference_autocomplete', 'default_formatter' => 'entity_reference_label', - 'field item class' => '\Drupal\Core\Entity\Field\Type\EntityReferenceItem', + 'data_type' => 'entity_reference_configurable_field', + 'field item class' => '\Drupal\entity_reference\Type\ConfigurableEntityReferenceItem', ); return $field_info; } /** + * Implements hook_data_type_info(). + */ +function entity_reference_data_type_info() { + return array( + 'entity_reference_configurable_field' => array( + 'label' => t('Configurable entity reference field item'), + 'description' => t('An entity field containing a configurable entity reference.'), + 'class' => '\Drupal\entity_reference\Type\ConfigurableEntityReferenceItem', + 'list class' => '\Drupal\Core\Entity\Field\Type\Field', + ), + ); +} + +/** * Implements hook_entity_field_info_alter(). * * Set the "target_type" property definition for entity reference fields. @@ -487,6 +502,76 @@ function entity_reference_create_instance($entity_type, $bundle, $field_name, $f } /** + * Implements hook_field_attach_rename_bundle(). + */ +function entity_reference_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) { + // Reflect bundle name changes in the definitions of existing entity reference + // field instance. + $fields = field_read_fields(); + foreach (field_read_instances() as $instance) { + $field_name = $instance['field_name']; + if ($fields[$field_name]['type'] != 'entity_reference' || $fields[$field_name]['settings']['target_type'] != $entity_type) { + // Not an entity reference field, referencing the given entity type. + continue; + } + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + // Instance is referencing all bundles. + continue; + } + + $update_instance = FALSE; + foreach ($instance['settings']['handler_settings']['target_bundles'] as $delta => $bundle) { + if ($bundle == $bundle_old) { + $instance['settings']['handler_settings']['target_bundles'][$delta] = $bundle_new; + $update_instance = TRUE; + } + } + if ($update_instance) { + field_update_instance($instance); + } + } +} + +/** + * Implements hook_field_attach_delete_bundle(). + */ +function entity_reference_field_attach_delete_bundle($entity_type, $bundle, $instances) { + // Iterate over all entity reference fields referencing the given entity type + // and delete those which target only the bundle that was deleted. + $fields = field_read_fields(); + foreach (field_read_instances() as $instance) { + $field_name = $instance['field_name']; + if ($fields[$field_name]['type'] != 'entity_reference' || $fields[$field_name]['settings']['target_type'] != $entity_type) { + // Not an entity reference field, referencing the given entity type. + continue; + } + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + // Instance is referencing all bundles. + continue; + } + + $modified_instance = FALSE; + foreach ($instance['settings']['handler_settings']['target_bundles'] as $delta => $target_bundle) { + if ($target_bundle == $bundle) { + unset($instance['settings']['handler_settings']['target_bundles'][$delta]); + $modified_instance = TRUE; + } + } + if ($modified_instance) { + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + field_delete_instance($instance); + } + else { + // Re-key the 'target_bundles' array. + $instance['settings']['handler_settings']['target_bundles'] = array_values($instance['settings']['handler_settings']['target_bundles']); + // Update the instance definition with the new target bundles. + field_update_instance($instance); + } + } + } +} + +/** * Menu Access callback for the autocomplete widget. * * @param string $type diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php index 069a2cb..dcc0c42 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/Type/Selection/SelectionInterface.php @@ -23,8 +23,9 @@ * Returns a list of referencable entities. * * @return array - * An array of referencable entities. Keys are entity IDs and - * values are (safe HTML) labels to be displayed to the user. + * A multidimensional array of referencable entities. The first level of + * keys are entity bundles. The second level are entity IDs and values are + * (safe HTML) labels to be displayed to the user. */ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php index fc5106e..4706824 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php @@ -218,7 +218,10 @@ public function validateReferencableEntities(array $ids) { * Implements SelectionInterface::validateAutocompleteInput(). */ public function validateAutocompleteInput($input, &$element, &$form_state, $form, $strict = TRUE) { - $entities = $this->getReferencableEntities($input, '=', 6); + $entities = array(); + foreach ($this->getReferencableEntities($input, '=', 6) as $bundle => $bundle_entities) { + $entities += $bundle_entities; + } $params = array( '%value' => $input, '@value' => $input, diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php index a8b4fee..994aea2 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceFormatterBase.php @@ -18,6 +18,18 @@ abstract class EntityReferenceFormatterBase extends FormatterBase { /** + * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::view(). + */ + public function view(EntityInterface $entity, $langcode, array $items) { + $addition = parent::view($entity, $langcode, $items); + + // Add a target type variable. + $addition[$this->field['field_name']]['#entity_reference_target_type'] = $this->field['settings']['target_type']; + + return $addition; + } + + /** * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::prepareView(). * * Mark the accessible IDs a user can see. We do not unset unaccessible diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index 6a8df6f..ae4fa03 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -7,8 +7,6 @@ namespace Drupal\entity_reference\Plugin\field\widget; -use Drupal\Component\Annotation\Plugin; -use Drupal\Core\Annotation\Translation; use Drupal\field\Plugin\Type\Widget\WidgetBase; /** @@ -45,7 +43,6 @@ public function settingsForm(array $form, array &$form_state) { '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), ); - return $element; } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php index d7b2d84..1138994 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutoCreateTest.php @@ -17,7 +17,7 @@ class EntityReferenceAutoCreateTest extends WebTestBase { public static function getInfo() { return array( 'name' => 'Entity Reference auto-create', - 'description' => 'Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.', + 'description' => 'Tests creating new entity (e.g. node) from an autocomplete widget.', 'group' => 'Entity Reference', ); } @@ -55,7 +55,7 @@ function setUp() { 'settings' => array( 'handler' => 'default', 'handler_settings' => array( - // Reference a single vocabulary. + // Reference a single bundle. 'target_bundles' => array( $referenced->type, ), @@ -89,7 +89,7 @@ public function testAutoCreate() { $edit = array( 'title' => $this->randomName(), - 'test_field[und][0][target_id]' => $new_title, + 'test_field[' . LANGUAGE_NOT_SPECIFIED . '][0][target_id]' => $new_title, ); $this->drupalPost("node/add/$this->referencing_type", $edit, 'Save'); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php index 70961b0..6eea17f 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAutocompleteTest.php @@ -116,5 +116,13 @@ function testTermAutocompletion() { } $target = array($n => '
' . check_plain($third_term->name) . '
'); $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.'); + + // Try to autocomplete using a nonexistent field. + $field_name = $this->randomName(); + $tag = $this->randomName(); + $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name)); + $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name))); + $this->drupalGet('entity_reference/autocomplete/single/' . $field_name . '/node/article/NULL', array('query' => array('q' => $tag))); + $this->assertResponse('403', 'Autocomplete returns correct HTTP response code when the taxonomy field does not exist.'); } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php new file mode 100644 index 0000000..f23e343 --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceFieldTest.php @@ -0,0 +1,143 @@ + 'Entity Reference field', + 'description' => 'Test the creation of entity reference fields.', + 'group' => 'Entity Reference', + ); + } + + public function setUp() { + parent::setUp(); + + $this->installSchema('system', 'variable'); + $this->installSchema('field', array('field_config', 'field_config_instance')); + $this->installSchema('field_test', array('test_entity', 'test_entity_revision')); + + // Setup a field and instance. + $this->field_name = drupal_strtolower($this->randomName()); + $this->field = array( + 'field_name' => $this->field_name, + 'type' => 'entity_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + 'settings' => array( + 'target_type' => 'test_entity', + ), + ); + field_create_field($this->field); + + $this->instance = array( + 'field_name' => $this->field_name, + 'bundle' => 'test_bundle', + 'entity_type' => 'test_entity', + 'widget' => array( + 'type' => 'options_select', + ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + 'test_bundle', + ), + ), + ), + ); + field_create_instance($this->instance); + } + + /** + * Tests reference field validation. + */ + function testEntityReferenceFieldValidation() { + // Test valid and invalid values with field_attach_validate(). + $langcode = LANGUAGE_NOT_SPECIFIED; + + $referenced_entity = field_test_create_entity(); + $referenced_entity->save(); + + $entity = field_test_create_entity(); + $entity->{$this->field_name}[$langcode][0]['target_id'] = $referenced_entity->id(); + try { + field_attach_validate($entity); + $this->pass('Correct reference does not cause validation error.'); + } + catch (FieldValidationException $e) { + $this->fail('Correct reference does not cause validation error.'); + } + + $bad_referenced_entity = field_test_create_entity(2, 2, 'test_bundle_2'); + $bad_referenced_entity->save(); + + $entity = field_test_create_entity(); + $entity->{$this->field_name}[$langcode][0]['target_id'] = $bad_referenced_entity->id(); + try { + field_attach_validate($entity); + $this->fail('Wrong reference causes validation error.'); + } + catch (FieldValidationException $e) { + $this->pass('Wrong reference causes validation error.'); + } + } + + /** + * Tests that bundle changes are mirrored in field definitions. + */ + function testEntityReferenceFieldChangeMachineName() { + // Add several entries in the 'target_bundles' setting, to make sure that + // they all get updated. + $test_bundle_2 = drupal_strtolower($this->randomName()); + field_test_create_bundle($test_bundle_2); + $this->instance['settings']['handler_settings']['target_bundles'] = array( + $test_bundle_2, + $test_bundle_2, + 'test_bundle', + ); + field_update_instance($this->instance); + + // Change the machine name. + $test_bundle_2_new = drupal_strtolower($this->randomName()); + field_test_rename_bundle($test_bundle_2, $test_bundle_2_new); + + // Check that the field instance settings have changed. + $instance = field_info_instance('test_entity', $this->field_name, 'test_bundle'); + $target_bundles = $instance['settings']['handler_settings']['target_bundles']; + $this->assertEqual($target_bundles[0], $test_bundle_2_new, 'Index 0: Machine name was updated correctly.'); + $this->assertEqual($target_bundles[1], $test_bundle_2_new, 'Index 1: Machine name was updated correctly.'); + $this->assertEqual($target_bundles[2], 'test_bundle', 'Index 2: Machine name was left untouched.'); + + field_test_delete_bundle($test_bundle_2_new); + // Check that the field instance settings have changed. + $instance = field_info_instance('test_entity', $this->field_name, 'test_bundle'); + $target_bundles = $instance['settings']['handler_settings']['target_bundles']; + $this->assertEqual($target_bundles[0], 'test_bundle', 'Index 0: The bundle that was not removed is still referenced.'); + $this->assertEqual(count($target_bundles[0]), 1, "The 'target_bundles' setting contains only one element."); + } +} diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php index 75caa6b..cc089fb 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php @@ -7,26 +7,25 @@ namespace Drupal\entity_reference\Tests; -use Drupal\simpletest\WebTestBase; -use Drupal\entity_reference\Type\EntityReferenceItem; -use Drupal\Core\Entity\Field\FieldItemInterface; use Drupal\Core\Entity\Field\FieldInterface; +use Drupal\Core\Entity\Field\FieldItemInterface; +use Drupal\field\Tests\FieldUnitTestBase; /** * Tests the new entity API for the entity reference field type. */ -class EntityReferenceItemTest extends WebTestBase { +class EntityReferenceItemTest extends FieldUnitTestBase { /** * Modules to enable. * * @var array */ - public static $modules = array('field', 'field_sql_storage', 'entity_test', 'options', 'entity_reference'); + public static $modules = array('entity_reference', 'taxonomy', 'options'); public static function getInfo() { return array( - 'name' => 'Entity Reference field API', + 'name' => 'Entity Reference field item', 'description' => 'Tests using entity fields of the entity reference field type.', 'group' => 'Entity Reference', ); @@ -38,44 +37,65 @@ public static function getInfo() { public function setUp() { parent::setUp(); + $this->installSchema('taxonomy', 'taxonomy_term_data'); + $this->installSchema('taxonomy', 'taxonomy_term_hierarchy'); + + $vocabulary = entity_create('taxonomy_vocabulary', array( + 'name' => $this->randomName(), + 'vid' => drupal_strtolower($this->randomName()), + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $vocabulary->save(); + + $this->term = entity_create('taxonomy_term', array( + 'name' => $this->randomName(), + 'vid' => $vocabulary->id(), + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $this->term->save(); + // Use the util to create an instance. - entity_reference_create_instance('entity_test', 'entity_test', 'field_test', 'Test entity reference', 'node'); + entity_reference_create_instance('entity_test', 'entity_test', 'field_test_taxonomy', 'Test entity reference', 'taxonomy_term'); } /** - * Tests using entity fields of the taxonomy term reference field type. + * Tests using entity fields of the entity reference field type. */ public function testEntityReferenceItem() { - // Create a node. - $node1 = $this->drupalCreateNode(); - $nid = $node1->id(); + $tid = $this->term->id(); // Just being able to create the entity like this verifies a lot of code. - $entity = entity_create('entity_test', array('name' => 'foo')); - $entity->field_test->target_id = $nid; + $entity = entity_create('entity_test', array()); + $entity->field_test_taxonomy->target_id = $tid; + $entity->name->value = $this->randomName(); $entity->save(); - $this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.'); - $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.'); - $this->assertEqual($entity->field_test->target_id, $nid); - $this->assertEqual($entity->field_test->entity->title, $node1->label()); - $this->assertEqual($entity->field_test->entity->id(), $nid); - $this->assertEqual($entity->field_test->entity->uuid(), $node1->uuid()); + $entity = entity_load('entity_test', $entity->id()); + $this->assertTrue($entity->field_test_taxonomy instanceof FieldInterface, 'Field implements interface.'); + $this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEqual($entity->field_test_taxonomy->target_id, $tid); + $this->assertEqual($entity->field_test_taxonomy->entity->name, $this->term->name); + $this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid); + $this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid()); // Change the name of the term via the reference. $new_name = $this->randomName(); - $entity->field_test->entity->title = $new_name; - $entity->field_test->entity->save(); - + $entity->field_test_taxonomy->entity->name = $new_name; + $entity->field_test_taxonomy->entity->save(); // Verify it is the correct name. - $node = node_load($nid); - $this->assertEqual($node->label(), $new_name); + $term = entity_load('taxonomy_term', $tid); + $this->assertEqual($term->name, $new_name); - // Make sure the computed node reflects updates to the node id. - $node2 = $this->drupalCreateNode(); + // Make sure the computed term reflects updates to the term id. + $term2 = entity_create('taxonomy_term', array( + 'name' => $this->randomName(), + 'vid' => $this->term->vid, + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $term2->save(); - $entity->field_test->target_id = $node2->nid; - $this->assertEqual($entity->field_test->entity->id(), $node2->id()); - $this->assertEqual($entity->field_test->entity->title, $node2->label()); + $entity->field_test_taxonomy->target_id = $term2->id(); + $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id()); + $this->assertEqual($entity->field_test_taxonomy->entity->name, $term2->name); } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Upgrade/EntityReferenceUpgradePathTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Upgrade/EntityReferenceUpgradePathTest.php new file mode 100644 index 0000000..367260a --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/Upgrade/EntityReferenceUpgradePathTest.php @@ -0,0 +1,66 @@ + 'Entity reference upgrade test', + 'description' => 'Upgrade tests for taxonomy term fields.', + 'group' => 'Upgrade path', + ); + } + + public function setUp() { + // Path to the database dump files. + $this->databaseDumpFiles = array( + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.filled.standard_all.database.php.gz', + ); + parent::setUp(); + } + + /** + * Tests a successful point release update. + */ + public function testEntityReferenceUpgrade() { + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + // Check that the default node page is displayed successfully. + $this->drupalGet('node'); + $this->assertResponse(200, 'Node page displayed successfully.'); + + // Check that a node that previously had a taxonomy term reference field can + // still be edited (Node 8 is a page node with taxonomy fields filled). + $this->drupalGet('node/8/edit'); + $this->assertResponse(200, 'Node edit page displayed successfully.'); + + // Check that a node that previously had a taxonomy term reference field can + // still be added. + $this->drupalGet('node/add/page'); + $this->assertResponse(200, 'Node add page displayed successfully.'); + + // Check that a Term reference field has been converted to Entity reference. + $taxonomy_term_field = field_info_field('taxonomy_vocabulary_1_0'); + $this->assertTrue($taxonomy_term_field['type'] == 'entity_reference', 'Term reference field is now a entity reference field.'); + + // Check that a Term reference instance has been converted to Entity + // reference. + $taxonomy_term_field_instance = field_info_instance('node', 'taxonomy_vocabulary_1_0', 'page'); + $this->assertTrue(isset($taxonomy_term_field_instance['settings']['handler_settings']), 'Term reference field instance is now a entity reference field instance.'); + } +} diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php new file mode 100644 index 0000000..eea88c7 --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/ConfigurableEntityReferenceItem.php @@ -0,0 +1,86 @@ +definition['settings']['target_type']; + + if (!isset(self::$propertyDefinitions[$target_type])) { + // Call the parent to define the target_id and entity properties. + parent::getPropertyDefinitions(); + + static::$propertyDefinitions[$target_type]['revision_id'] = array( + // @todo: Lookup the entity type's ID data type and use it here. + 'type' => 'integer', + 'label' => t('Revision ID'), + 'constraints' => array( + 'Range' => array('min' => 0), + ), + ); + static::$propertyDefinitions[$target_type]['label'] = array( + 'type' => 'string', + 'label' => t('Label (auto-create)'), + 'computed' => TRUE, + ); + static::$propertyDefinitions[$target_type]['access'] = array( + 'type' => 'boolean', + 'label' => t('Access'), + 'computed' => TRUE, + ); + } + return static::$propertyDefinitions[$target_type]; + } + + /** + * Overrides \Drupal\Core\Entity\Field\Type\EntityReferenceItem::setValue(). + */ + public function setValue($values) { + // Treat the values as property value of the entity field, if no array + // is given. That way we support setting the field by entity ID or object. + if (!is_array($values)) { + $values = array('entity' => $values); + } + + foreach (array('revision_id', 'access', 'label') as $property) { + if (array_key_exists($property, $values)) { + $this->properties[$property]->setValue($values[$property]); + unset($values[$property]); + } + } + + // Pass the remaining values through to the parent class. + parent::setValue($values); + } + +} diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 695fd89..04accf7 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -170,9 +170,15 @@ function field_schema() { /** * Creates a field by writing directly to the database. * + * @param $field + * The field definition array, passed by reference. + * @param $update + * (optional) Indicate if we need to create a new field or update an existing + * one. Defaults to FALSE. + * * @ingroup update_api */ -function _update_7000_field_create_field(&$field) { +function _update_7000_field_create_field(&$field, $update = FALSE) { // Merge in default values.` $field += array( 'entity_types' => array(), @@ -228,13 +234,34 @@ function _update_7000_field_create_field(&$field) { 'translatable' => (int) $field['translatable'], 'deleted' => (int) $field['deleted'], ); + // We don't use drupal_write_record() here because it depends on the schema. - $field['id'] = db_insert('field_config') - ->fields($record) - ->execute(); + if ($update) { + db_update('field_config') + ->condition('id', $field['id']) + ->fields($record) + ->execute(); + } + else { + $field['id'] = db_insert('field_config') + ->fields($record) + ->execute(); + + // Create storage for the field. + field_sql_storage_field_storage_create_field($field); + } +} - // Create storage for the field. - field_sql_storage_field_storage_create_field($field); +/** + * Updates a field by writing directly to the database. + * + * @param $field + * The field definition array. + * + * @ingroup update_api + */ +function _update_7000_field_update_field($field) { + _update_7000_field_create_field($field, TRUE); } /** @@ -347,9 +374,17 @@ function _update_7000_field_read_fields(array $conditions = array(), $key = 'id' /** * Writes a field instance directly to the database. * + * @param $field + * The field definition array. + * @param $instance + * The instance definition array, passed by reference. + * @param $update + * (optional) Indicate if we need to create a new instance or update an + * existing one. Defaults to FALSE. + * * @ingroup update_api */ -function _update_7000_field_create_instance($field, &$instance) { +function _update_7000_field_create_instance($field, &$instance, $update = FALSE) { // Merge in defaults. $instance += array( 'field_id' => $field['id'], @@ -371,9 +406,33 @@ function _update_7000_field_create_instance($field, &$instance) { 'data' => serialize($data), 'deleted' => (int) $instance['deleted'], ); - $instance['id'] = db_insert('field_config_instance') - ->fields($record) - ->execute(); + + if ($update) { + db_update('field_config_instance') + ->condition('id', $instance['id']) + ->fields($record) + ->execute(); + } + else { + $instance['id'] = db_insert('field_config_instance') + ->fields($record) + ->execute(); + + } +} + +/** + * Updates a field instance directly to the database. + * + * @param $field + * The field definition array. + * @param $instance + * The instance definition array, passed by reference. + * + * @ingroup update_api + */ +function _update_7000_field_update_instance($field, &$instance) { + _update_7000_field_create_instance($field, $instance, TRUE); } /** @@ -487,4 +546,3 @@ function field_update_8002() { * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ - diff --git a/core/modules/field/field.module b/core/modules/field/field.module index 11cd9bb..928addc 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -441,7 +441,7 @@ function field_entity_field_info($entity_type) { // @todo: Allow for adding field type settings. $definition = array( 'label' => t('Field !name', array('!name' => $field_name)), - 'type' => $field['type'] . '_field', + 'type' => isset($field_types[$field['type']]['data_type']) ? $field_types[$field['type']]['data_type'] : $field['type'] . '_field', 'configurable' => TRUE, 'translatable' => !empty($field['translatable']) ); diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/FieldUiTestBase.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/FieldUiTestBase.php index 2d3fb98..ca9c056 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/FieldUiTestBase.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/FieldUiTestBase.php @@ -19,7 +19,7 @@ * * @var array */ - public static $modules = array('node', 'field_ui', 'field_test', 'taxonomy'); + public static $modules = array('node', 'field_ui', 'field_test', 'taxonomy', 'entity_reference'); function setUp() { parent::setUp(); diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php index cbc132f..a4d69fa 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php @@ -41,7 +41,10 @@ function setUp() { $field = array( 'field_name' => 'field_' . $vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', + 'settings' => array( + 'target_type' => 'taxonomy_term', + ), ); field_create_field($field); @@ -397,7 +400,7 @@ function testDuplicateFieldName() { $edit = array( 'fields[_add_new_field][field_name]' => 'tags', 'fields[_add_new_field][label]' => $this->randomName(), - 'fields[_add_new_field][type]' => 'taxonomy_term_reference', + 'fields[_add_new_field][type]' => 'entity_reference', 'fields[_add_new_field][widget_type]' => 'options_select', ); $url = 'admin/structure/types/manage/' . $this->type . '/fields'; @@ -414,15 +417,15 @@ function testWidgetChange() { $url_fields = 'admin/structure/types/manage/article/fields'; $url_tags_widget = $url_fields . '/field_tags/widget-type'; - // Check that the field_tags field currently uses the 'options_select' + // Check that the field_tags field currently uses the 'entity_reference_autocomplete' // widget. $instance = field_info_instance('node', 'field_tags', 'article'); - $this->assertEqual($instance['widget']['type'], 'options_select'); + $this->assertEqual($instance['widget']['type'], 'entity_reference_autocomplete'); // Check that the "Manage fields" page shows the correct widget type. $this->drupalGet($url_fields); $link = current($this->xpath('//a[contains(@href, :href)]', array(':href' => $url_tags_widget))); - $this->assertEqual((string) $link, 'Select list'); + $this->assertEqual((string) $link, 'Autocomplete'); // Go to the 'Widget type' form and check that the correct widget is // selected. diff --git a/core/modules/forum/forum.info.yml b/core/modules/forum/forum.info.yml index 5d55d08..9e220c3 100644 --- a/core/modules/forum/forum.info.yml +++ b/core/modules/forum/forum.info.yml @@ -2,6 +2,7 @@ name: Forum description: 'Provides discussion forums.' dependencies: - node + - entity_reference - history - taxonomy - comment diff --git a/core/modules/forum/forum.install b/core/modules/forum/forum.install index b1e63de..02836ff 100644 --- a/core/modules/forum/forum.install +++ b/core/modules/forum/forum.install @@ -58,14 +58,9 @@ function forum_enable() { if (!field_info_field('taxonomy_forums')) { $field = array( 'field_name' => 'taxonomy_forums', - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -90,19 +85,28 @@ function forum_enable() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($instance); // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'forum', 'default') ->setComponent('taxonomy_forums', array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); entity_get_display('node', 'forum', 'teaser') ->setComponent('taxonomy_forums', array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 574d536..5edff67 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -291,11 +291,11 @@ function forum_node_validate(EntityInterface $node, $form) { foreach ($node->taxonomy_forums[$langcode] as $delta => $item) { // If no term was selected (e.g. when no terms exist yet), remove the // item. - if (empty($item['tid'])) { + if (empty($item['target_id'])) { unset($node->taxonomy_forums[$langcode][$delta]); continue; } - $term = taxonomy_term_load($item['tid']); + $term = taxonomy_term_load($item['target_id']); if (!$term) { form_set_error('taxonomy_forums', t('Select a forum.')); continue; @@ -324,11 +324,11 @@ function forum_node_presave(EntityInterface $node) { reset($node->taxonomy_forums); $langcode = key($node->taxonomy_forums); if (!empty($node->taxonomy_forums[$langcode])) { - $node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid']; + $node->forum_tid = $node->taxonomy_forums[$langcode][0]['target_id']; $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField(); if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) { // A shadow copy needs to be created. Retain new term and add old term. - $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid); + $node->taxonomy_forums[$langcode][] = array('target_id' => $old_tid); } } } @@ -526,7 +526,7 @@ function forum_field_storage_pre_insert(EntityInterface $entity, &$skip_fields) $query->values(array( 'nid' => $entity->id(), 'title' => $translation->title->value, - 'tid' => $translation->taxonomy_forums->tid, + 'tid' => $translation->taxonomy_forums->target_id, 'sticky' => $entity->sticky, 'created' => $entity->created, 'comment_count' => 0, @@ -561,7 +561,7 @@ function forum_field_storage_pre_update(EntityInterface $entity, &$skip_fields) $query->values(array( 'nid' => $entity->nid, 'title' => $entity->title, - 'tid' => $item['tid'], + 'tid' => $item['target_id'], 'sticky' => $entity->sticky, 'created' => $entity->created, 'comment_count' => 0, diff --git a/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php b/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php index c08ce70..c711879 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php @@ -525,7 +525,7 @@ function createForumTopic($forum, $container = FALSE) { // Retrieve node object, ensure that the topic was created and in the proper forum. $node = $this->drupalGetNodeByTitle($title); $this->assertTrue($node != NULL, format_string('Node @title was loaded', array('@title' => $title))); - $this->assertEqual($node->taxonomy_forums[LANGUAGE_NOT_SPECIFIED][0]['tid'], $tid, 'Saved forum topic was in the expected forum'); + $this->assertEqual($node->taxonomy_forums[LANGUAGE_NOT_SPECIFIED][0]['target_id'], $tid, 'Saved forum topic was in the expected forum'); // View forum topic. $this->drupalGet('node/' . $node->nid); diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldTestSetupHelper.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldTestSetupHelper.php index d0eb0c3..1b5c080 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldTestSetupHelper.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldTestSetupHelper.php @@ -56,7 +56,7 @@ public function __construct() { $this->rdfMappingManager = new RdfMappingManager($dispatcher, $this->siteSchemaManager); // Construct normalizers. $this->normalizers = array( - 'entityreference' => new JsonldEntityReferenceNormalizer($this->siteSchemaManager, $this->rdfMappingManager), + 'entity_reference' => new JsonldEntityReferenceNormalizer($this->siteSchemaManager, $this->rdfMappingManager), 'field_item' => new JsonldFieldItemNormalizer($this->siteSchemaManager, $this->rdfMappingManager), 'entity' => new JsonldEntityNormalizer($this->siteSchemaManager, $this->rdfMappingManager), ); diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php index 38cee8a..b4f498e 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/Tests/SupportsSerializationTest.php @@ -58,7 +58,7 @@ public function testSupportsNormalization() { $supportedEntity = entity_create('entity_test', array()); $unsupportedEntity = new ConfigEntityTest(); $field = $supportedEntity->get('uuid'); - $entityreferenceField = $supportedEntity->get('user_id'); + $entityReferenceField = $supportedEntity->get('user_id'); // Supported entity. $this->assertTrue($this->normalizers['entity']->supportsNormalization($supportedEntity, static::$format), "Entity normalization is supported for $format on content entities."); @@ -68,7 +68,7 @@ public function testSupportsNormalization() { // Field item. $this->assertTrue($this->normalizers['field_item']->supportsNormalization($field->offsetGet(0), static::$format), "Field item normalization is supported for $format."); // Entity reference field item. - $this->assertTrue($this->normalizers['entityreference']->supportsNormalization($entityreferenceField->offsetGet(0), static::$format), "Entity reference field item normalization is supported for $format."); + $this->assertTrue($this->normalizers['entity_reference']->supportsNormalization($entityReferenceField->offsetGet(0), static::$format), "Entity reference field item normalization is supported for $format."); } /** @@ -80,7 +80,7 @@ public function testSupportsDenormalization() { $supportedEntityClass = 'Drupal\Core\Entity\EntityNG'; $unsupportedEntityClass = 'Drupal\config\Tests\ConfigEntityTest'; $fieldClass = 'Drupal\Core\Entity\Field\Type\StringItem'; - $entityreferenceFieldClass = 'Drupal\Core\Entity\Field\Type\EntityReferenceItem'; + $entityReferenceFieldClass = 'Drupal\Core\Entity\Field\Type\EntityReferenceItem'; // Supported entity. $this->assertTrue($this->normalizers['entity']->supportsDenormalization($data, $supportedEntityClass, static::$format), "Entity denormalization is supported for $format on content entities."); @@ -90,7 +90,7 @@ public function testSupportsDenormalization() { // Field item. $this->assertTrue($this->normalizers['field_item']->supportsDenormalization($data, $fieldClass, static::$format), "Field item denormalization is supported for $format."); // Entity reference field item. - $this->assertTrue($this->normalizers['entityreference']->supportsDenormalization($data, $entityreferenceFieldClass, static::$format), "Entity reference field item denormalization is supported for $format."); + $this->assertTrue($this->normalizers['entity_reference']->supportsDenormalization($data, $entityReferenceFieldClass, static::$format), "Entity reference field item denormalization is supported for $format."); } } diff --git a/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php b/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php index 4f868f7..65a7861 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php +++ b/core/modules/node/lib/Drupal/node/Plugin/views/wizard/Node.php @@ -280,12 +280,11 @@ protected function build_filters(&$form, &$form_state) { foreach (field_info_instances($this->entity_type, $bundle) as $instance) { // We define "tag-like" taxonomy fields as ones that use the // "Autocomplete term widget (tagging)" widget. - if ($instance['widget']['type'] == 'taxonomy_autocomplete') { - $tag_fields[] = $instance['field_name']; + if ($instance['widget']['type'] == 'entity_reference_autocomplete_tags') { + $tag_fields[$instance['field_name']] = $instance; } } } - $tag_fields = array_unique($tag_fields); if (!empty($tag_fields)) { // If there is more than one "tag-like" taxonomy field available to // the view, we can only make our filter apply to one of them (as @@ -293,20 +292,22 @@ protected function build_filters(&$form, &$form_state) { // that is created by the Standard install profile in core and also // commonly used by contrib modules; thus, it is most likely to be // associated with the "main" free-tagging vocabulary on the site. - if (in_array('field_tags', $tag_fields)) { + if (array_key_exists('field_tags', $tag_fields)) { $tag_field_name = 'field_tags'; } else { - $tag_field_name = reset($tag_fields); + $tag_field_name = key($tag_fields); } + $tag_field_instance = $tag_fields[$tag_field_name]; // Add the autocomplete textfield to the wizard. $form['displays']['show']['tagged_with'] = array( '#type' => 'textfield', '#title' => t('tagged with'), - '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name, + '#autocomplete_path' => 'entity_reference/autocomplete/tags/' . $tag_field_name . '/' . $tag_field_instance['entity_type'] . '/' . $tag_field_instance['bundle'] . '/NULL', '#size' => 30, '#maxlength' => 1024, '#field_name' => $tag_field_name, + '#field_instance' => $tag_field_instance, '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'), ); } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php index 6b7d00d..ebb432d 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php @@ -77,11 +77,11 @@ function testNodeAccessBasic() { if ($is_private) { $edit['private'] = TRUE; $edit['body[und][0][value]'] = 'private node'; - $edit['field_tags[und]'] = 'private'; + $edit['field_tags[und][target_id]'] = 'private'; } else { $edit['body[und][0][value]'] = 'public node'; - $edit['field_tags[und]'] = 'public'; + $edit['field_tags[und][target_id]'] = 'public'; } $this->drupalPost('node/add/article', $edit, t('Save')); diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php index a9ae1c3..4d75105 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php @@ -86,7 +86,7 @@ public function testForumPager() { 'nid' => NULL, 'type' => 'forum', 'taxonomy_forums' => array( - array('tid' => $tid) + array('target_id' => $tid), ), )); } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php index d8fa419..360b897 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php @@ -12,7 +12,7 @@ */ class NodeTypePersistenceTest extends NodeTestBase { // Enable the prerequisite modules for forum - public static $modules = array('history', 'taxonomy', 'options', 'comment'); + public static $modules = array('history', 'entity_reference', 'taxonomy', 'options', 'comment'); public static function getInfo() { return array( 'name' => 'Node type persist', diff --git a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php index 6f99dae..f7dc955 100644 --- a/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php +++ b/core/modules/rdf/lib/Drupal/rdf/Tests/RdfaMarkupTest.php @@ -157,7 +157,7 @@ function testAttributesInMarkupFile() { $tag1 = $this->randomName(8); $tag2 = $this->randomName(8); $edit = array(); - $edit['field_tags[' . LANGUAGE_NOT_SPECIFIED . ']'] = "$tag1, $tag2"; + $edit['field_tags[' . LANGUAGE_NOT_SPECIFIED . '][target_id]'] = "$tag1, $tag2"; $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $term_1_id = key(taxonomy_term_load_multiple_by_name($tag1)); $taxonomy_term_1_uri = url('taxonomy/term/' . $term_1_id, array('absolute' => TRUE)); diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 547075a..a2d55c4 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -783,22 +783,37 @@ function rdf_field_attach_view_alter(&$output, $context) { // Append term mappings on displayed taxonomy links. foreach (element_children($output) as $field_name) { $element = &$output[$field_name]; - if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') { - foreach ($element['#items'] as $delta => $item) { - // This function is invoked during entity preview when taxonomy term - // reference items might contain free-tagging terms that do not exist - // yet and thus have no $item['entity']. - if (isset($item['entity'])) { - $term = $item['entity']; - if (!empty($term->rdf_mapping['rdftype'])) { - $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype']; - } - if (!empty($term->rdf_mapping['name']['predicates'])) { - // A property attribute is used with an empty datatype attribute so - // the term name is parsed as a plain literal in RDFa 1.0 and 1.1. - $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates']; - $element[$delta]['#options']['attributes']['datatype'] = ''; - } + $field_name = $element['#field_name']; + $field = field_info_field($field_name); + if ($field['type'] != 'entity_reference' || $field['settings']['target_type'] != 'taxonomy_term') { + // Not an entity reference field referencing taxonomy terms. + continue; + } + + // @todo: We should pass the view-mode, however we get view-mode as "full" + // which doesn't exist. + $display = entity_get_display($element['#entity_type'], $element['#bundle'], $element['#view_mode'] != 'full' ? $element['#view_mode'] : 'default'); + $options = $display->getComponent($field_name); + + if ($options['type'] != 'entity_reference_label' || empty($options['settings']['link'])) { + // Formatter is not a label with link. + continue; + } + + foreach ($element['#items'] as $delta => $item) { + // This function is invoked during entity preview when taxonomy term + // reference items might contain free-tagging terms that do not exist + // yet and thus have no $item['entity']. + if (isset($item['entity'])) { + $term = $item['entity']; + if (!empty($term->rdf_mapping['rdftype'])) { + $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype']; + } + if (!empty($term->rdf_mapping['name']['predicates'])) { + // A property attribute is used with an empty datatype attribute so + // the term name is parsed as a plain literal in RDFa 1.0 and 1.1. + $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates']; + $element[$delta]['#options']['attributes']['datatype'] = ''; } } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php index 67f190e..02734d5 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php @@ -407,6 +407,7 @@ public function testTaxonomyTermHooks() { */ public function testTaxonomyVocabularyHooks() { $this->installSchema('taxonomy', array('taxonomy_term_data', 'taxonomy_term_hierarchy')); + $this->installSchema('system', 'variable'); $vocabulary = entity_create('taxonomy_vocabulary', array( 'name' => 'Test vocabulary', diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php index 434311c..e39e49e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php @@ -17,7 +17,7 @@ class EntityQueryRelationshipTest extends EntityUnitTestBase { * * @var array */ - public static $modules = array('taxonomy', 'options'); + public static $modules = array('entity_reference', 'taxonomy', 'options'); /** * @var \Drupal\field_sql_storage\Entity\QueryFactory @@ -82,16 +82,29 @@ public function setUp() { $this->fieldName = strtolower($this->randomName()); $field = array( 'field_name' => $this->fieldName, - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', + 'settings' => array( + 'target_type' => 'taxonomy_term', + ), ); - $field['settings']['allowed_values']['vocabulary'] = $vocabulary->id(); + field_create_field($field); // Third, create the instance. $instance = array( 'entity_type' => 'entity_test', 'field_name' => $this->fieldName, 'bundle' => 'entity_test', + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); + field_create_instance($instance); // Create two terms and also two accounts. for ($i = 0; $i <= 1; $i++) { @@ -111,7 +124,7 @@ public function setUp() { $entity->name->value = $this->randomName(); $index = $i ? 1 : 0; $entity->user_id->target_id = $this->accounts[$index]->uid; - $entity->{$this->fieldName}->tid = $this->terms[$index]->tid; + $entity->{$this->fieldName}->target_id = $this->terms[$index]->tid; $entity->save(); $this->entities[] = $entity; } @@ -153,7 +166,7 @@ public function testQuery() { // This returns the 0th entity as that's only one pointing to the 0th // term (test with specifying the column name). $this->queryResults = $this->factory->get('entity_test') - ->condition("$this->fieldName.tid.entity.name", $this->terms[0]->name) + ->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name) ->execute(); $this->assertResults(array(0)); // This returns the 1st and 2nd entity as those point to the 1st term. diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php index c5bfd17..31f85c5 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/BreadcrumbTest.php @@ -307,7 +307,7 @@ function testBreadCrumbs() { 'Breadcrumbs' => array(), ); $edit = array( - "field_tags[$langcode]" => implode(',', array_keys($tags)), + "field_tags[$langcode][target_id]" => implode(',', array_keys($tags)), ); $this->drupalPost("node/{$parent->nid}/edit", $edit, t('Save and keep published')); diff --git a/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php b/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php index e561ab7..aee17bb 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php @@ -139,7 +139,7 @@ function testModuleEnableOrder() { // - options depends on number // - ban depends on php (via module_test) // The correct enable order is: - $expected_order = array('php', 'ban', 'comment', 'history', 'number', 'options', 'taxonomy', 'forum'); + $expected_order = array('php', 'ban', 'comment', 'entity_reference', 'history', 'number', 'options', 'taxonomy', 'forum'); // Enable the modules through the UI, verifying that the dependency chain // is correct. @@ -147,8 +147,9 @@ function testModuleEnableOrder() { $edit['modules[Core][forum][enable]'] = 'forum'; $this->drupalPost('admin/modules', $edit, t('Save configuration')); $this->assertModules(array('forum'), FALSE); - $this->assertText(t('You must enable the History, Taxonomy, Options, Number, Comment, Ban, PHP Filter modules to install Forum.')); + $this->assertText(t('You must enable the Entity Reference, History, Taxonomy, Options, Number, Comment, Ban, PHP Filter modules to install Forum.')); $edit['modules[Core][history][enable]'] = 'history'; + $edit['modules[Core][entity_reference][enable]'] = 'entity_reference'; $edit['modules[Core][options][enable]'] = 'options'; $edit['modules[Core][number][enable]'] = 'number'; $edit['modules[Core][taxonomy][enable]'] = 'taxonomy'; @@ -156,7 +157,7 @@ function testModuleEnableOrder() { $edit['modules[Core][ban][enable]'] = 'ban'; $edit['modules[Core][php][enable]'] = 'php'; $this->drupalPost('admin/modules', $edit, t('Save configuration')); - $this->assertModules(array('forum', 'ban', 'php', 'comment', 'history', 'taxonomy', 'options', 'number'), TRUE); + $this->assertModules(array('forum', 'ban', 'php', 'comment', 'entity_reference', 'history', 'taxonomy', 'options', 'number'), TRUE); // Check the actual order which is saved by module_test_modules_enabled(). $module_order = state()->get('system_test.module_enable_order') ?: array(); diff --git a/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php b/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php index 3859041..1d26480 100644 --- a/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php @@ -13,6 +13,14 @@ * Test token replacement in strings. */ class TokenReplaceTest extends WebTestBase { + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('entity_reference'); + public static function getInfo() { return array( 'name' => 'Token replacement', diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/EntityFilteringThemeTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/EntityFilteringThemeTest.php index 1f0f2ee..f18dd2f 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Theme/EntityFilteringThemeTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/EntityFilteringThemeTest.php @@ -102,7 +102,7 @@ function setUp() { 'title' => $this->xss_label, 'type' => 'article', 'promote' => NODE_PROMOTED, - 'field_tags' => array(array('tid' => $this->term->tid)), + 'field_tags' => array(array('target_id' => $this->term->tid)), )); // Create a test comment on the test node. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php deleted file mode 100644 index 3457126..0000000 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ /dev/null @@ -1,108 +0,0 @@ - 'textfield', - '#title' => t('Placeholder'), - '#default_value' => $this->getSetting('placeholder'), - '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), - ); - return $element; - } - - /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement(). - */ - public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) { - $field = $this->field; - - $tags = array(); - foreach ($items as $item) { - $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']); - } - $element += array( - '#type' => 'textfield', - '#default_value' => taxonomy_implode_tags($tags), - '#autocomplete_path' => $this->getSetting('autocomplete_path') . '/' . $field['field_name'], - '#size' => $this->getSetting('size'), - '#placeholder' => $this->getSetting('placeholder'), - '#maxlength' => 1024, - '#element_validate' => array('taxonomy_autocomplete_validate'), - ); - - return $element; - } - - /** - * Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::massageFormValues() - */ - public function massageFormValues(array $values, array $form, array &$form_state) { - // Autocomplete widgets do not send their tids in the form, so we must detect - // them here and process them independently. - $items = array(); - $field = $this->field; - - // Collect candidate vocabularies. - foreach ($field['settings']['allowed_values'] as $tree) { - if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) { - $vocabularies[$vocabulary->id()] = $vocabulary; - } - } - - // Translate term names into actual terms. - foreach($values as $value) { - // See if the term exists in the chosen vocabulary and return the tid; - // otherwise, create a new 'autocreate' term for insert/update. - if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) { - $term = array_pop($possibilities); - $item = array('tid' => $term->tid); - } - else { - $vocabulary = reset($vocabularies); - $term = entity_create('taxonomy_term', array( - 'vid' => $vocabulary->id(), - 'name' => $value, - )); - $item = array('tid' => FALSE, 'entity' => $term); - } - $items[] = $item; - } - - return $items; - } - -} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php index 61da87b..8fd6e72 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php @@ -138,7 +138,7 @@ function get_argument() { $fields = field_info_instances('node', $node->type); foreach ($fields as $name => $info) { $field_info = field_info_field($name); - if ($field_info['type'] == 'taxonomy_term_reference') { + if ($field_info['type'] == 'entity_reference') { $items = field_get_items($node, $name); if (is_array($items)) { foreach ($items as $item) { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/LegacyTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/LegacyTest.php index 778b1a0..15eeb85 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/LegacyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/LegacyTest.php @@ -42,7 +42,7 @@ function testTaxonomyLegacyNode() { $edit['date[date]'] = $date->format('Y-m-d'); $edit['date[time]'] = $date->format('H:i:s'); $edit["body[$langcode][0][value]"] = $this->randomName(); - $edit["field_tags[$langcode]"] = $this->randomName(); + $edit["field_tags[$langcode][target_id]"] = $this->randomName(); $this->drupalPost('node/add/article', $edit, t('Save and publish')); // Checks that the node has been saved. $node = $this->drupalGetNodeByTitle($edit['title']); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php index 4c3e3d3..d9aed7e 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php @@ -17,7 +17,7 @@ class RssTest extends TaxonomyTestBase { * * @var array */ - public static $modules = array('node', 'field_ui', 'views'); + public static $modules = array('node', 'field_ui', 'views', 'entity_reference'); public static function getInfo() { return array( @@ -36,15 +36,10 @@ function setUp() { $field = array( 'field_name' => 'taxonomy_' . $this->vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -56,11 +51,20 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance); entity_get_display('node', 'article', 'default') ->setComponent('taxonomy_' . $this->vocabulary->id(), array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } @@ -84,7 +88,7 @@ function testTaxonomyRss() { // Change the format to 'RSS category'. $this->drupalGet("admin/structure/types/manage/article/display/rss"); $edit = array( - "fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'taxonomy_term_reference_rss_category', + "fields[taxonomy_" . $this->vocabulary->id() . "][type]" => 'entity_reference_rss_category', ); $this->drupalPost(NULL, $edit, t('Save')); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php deleted file mode 100644 index ea9bc3e..0000000 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php +++ /dev/null @@ -1,116 +0,0 @@ - 'Taxonomy reference field item', - 'description' => 'Tests using entity fields of the taxonomy term reference field type.', - 'group' => 'Taxonomy', - ); - } - - public function setUp() { - parent::setUp(); - $this->installSchema('taxonomy', 'taxonomy_term_data'); - $this->installSchema('taxonomy', 'taxonomy_term_hierarchy'); - - $vocabulary = entity_create('taxonomy_vocabulary', array( - 'name' => $this->randomName(), - 'vid' => drupal_strtolower($this->randomName()), - 'langcode' => LANGUAGE_NOT_SPECIFIED, - )); - $vocabulary->save(); - $field = array( - 'field_name' => 'field_test_taxonomy', - 'type' => 'taxonomy_term_reference', - 'cardinality' => FIELD_CARDINALITY_UNLIMITED, - 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $vocabulary->id(), - 'parent' => 0, - ), - ), - ), - ); - field_create_field($field); - $instance = array( - 'entity_type' => 'entity_test', - 'field_name' => 'field_test_taxonomy', - 'bundle' => 'entity_test', - 'widget' => array( - 'type' => 'options_select', - ), - ); - field_create_instance($instance); - $this->term = entity_create('taxonomy_term', array( - 'name' => $this->randomName(), - 'vid' => $vocabulary->id(), - 'langcode' => LANGUAGE_NOT_SPECIFIED, - )); - $this->term->save(); - } - - /** - * Tests using entity fields of the taxonomy term reference field type. - */ - public function testTaxonomyTermReferenceItem() { - $tid = $this->term->id(); - // Just being able to create the entity like this verifies a lot of code. - $entity = entity_create('entity_test', array()); - $entity->field_test_taxonomy->tid = $this->term->tid; - $entity->name->value = $this->randomName(); - $entity->save(); - - $entity = entity_load('entity_test', $entity->id()); - $this->assertTrue($entity->field_test_taxonomy instanceof FieldInterface, 'Field implements interface.'); - $this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.'); - $this->assertEqual($entity->field_test_taxonomy->tid, $this->term->tid); - $this->assertEqual($entity->field_test_taxonomy->entity->name, $this->term->name); - $this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid); - $this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid()); - - // Change the name of the term via the reference. - $new_name = $this->randomName(); - $entity->field_test_taxonomy->entity->name = $new_name; - $entity->field_test_taxonomy->entity->save(); - // Verify it is the correct name. - $term = entity_load('taxonomy_term', $tid); - $this->assertEqual($term->name, $new_name); - - // Make sure the computed term reflects updates to the term id. - $term2 = entity_create('taxonomy_term', array( - 'name' => $this->randomName(), - 'vid' => $this->term->vid, - 'langcode' => LANGUAGE_NOT_SPECIFIED, - )); - $term2->save(); - - $entity->field_test_taxonomy->tid = $term2->tid; - $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->tid); - $this->assertEqual($entity->field_test_taxonomy->entity->name, $term2->name); - } - -} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTestBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTestBase.php index fab42f0..f633102 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTestBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTestBase.php @@ -19,7 +19,7 @@ * * @var array */ - public static $modules = array('taxonomy'); + public static $modules = array('entity_reference', 'taxonomy'); function setUp() { parent::setUp(); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php index 8c5571c..a8f7d20 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php @@ -43,20 +43,11 @@ function setUp() { $this->field_name = drupal_strtolower($this->randomName()); $this->field = array( 'field_name' => $this->field_name, - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary1->id(), - 'parent' => '0', - ), - array( - 'vocabulary' => $this->vocabulary2->id(), - 'parent' => '0', - ), - ), - ) + 'target_type' => 'taxonomy_term', + ), ); field_create_field($this->field); $this->instance = array( @@ -66,11 +57,21 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary1->id(), + $this->vocabulary2->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance); entity_get_display('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name, array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } @@ -121,10 +122,6 @@ function testTaxonomyTermFieldMultipleVocabularies() { $this->assertText($term1->name, 'Term 1 name is displayed.'); $this->assertNoText($term2->name, 'Term 2 name is not displayed.'); - // Verify that field and instance settings are correct. - $field_info = field_info_field($this->field_name); - $this->assertEqual(count($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.'); - // The widget should still be displayed. $this->drupalGet('test-entity/add/test_bundle'); $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed'); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php deleted file mode 100644 index d5de5fa..0000000 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ /dev/null @@ -1,171 +0,0 @@ - 'Taxonomy term reference field', - 'description' => 'Test the creation of term fields.', - 'group' => 'Taxonomy', - ); - } - - function setUp() { - parent::setUp(); - - $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy')); - $this->drupalLogin($web_user); - $this->vocabulary = $this->createVocabulary(); - - // Setup a field and instance. - $this->field_name = drupal_strtolower($this->randomName()); - $this->field = array( - 'field_name' => $this->field_name, - 'type' => 'taxonomy_term_reference', - 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => '0', - ), - ), - ) - ); - field_create_field($this->field); - $this->instance = array( - 'field_name' => $this->field_name, - 'entity_type' => 'test_entity', - 'bundle' => 'test_bundle', - 'widget' => array( - 'type' => 'options_select', - ), - ); - field_create_instance($this->instance); - entity_get_display('test_entity', 'test_bundle', 'full') - ->setComponent($this->field_name, array( - 'type' => 'taxonomy_term_reference_link', - )) - ->save(); - } - - /** - * Test term field validation. - */ - function testTaxonomyTermFieldValidation() { - // Test valid and invalid values with field_attach_validate(). - $langcode = LANGUAGE_NOT_SPECIFIED; - $entity = field_test_create_entity(); - $term = $this->createTerm($this->vocabulary); - $entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid; - try { - field_attach_validate($entity); - $this->pass('Correct term does not cause validation error.'); - } - catch (FieldValidationException $e) { - $this->fail('Correct term does not cause validation error.'); - } - - $entity = field_test_create_entity(); - $bad_term = $this->createTerm($this->createVocabulary()); - $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid; - try { - field_attach_validate($entity); - $this->fail('Wrong term causes validation error.'); - } - catch (FieldValidationException $e) { - $this->pass('Wrong term causes validation error.'); - } - } - - /** - * Test widgets. - */ - function testTaxonomyTermFieldWidgets() { - // Create a term in the vocabulary. - $term = $this->createTerm($this->vocabulary); - - // Display creation form. - $langcode = LANGUAGE_NOT_SPECIFIED; - $this->drupalGet('test-entity/add/test_bundle'); - $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.'); - - // Submit with some value. - $edit = array( - "{$this->field_name}[$langcode]" => array($term->tid), - ); - $this->drupalPost(NULL, $edit, t('Save')); - preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); - $id = $match[1]; - $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); - - // Display the object. - $entity = field_test_entity_test_load($id); - $entities = array($id => $entity); - $display = entity_get_display($entity->entityType(), $entity->bundle(), 'full'); - field_attach_prepare_view('test_entity', $entities, array($entity->bundle() => $display)); - $entity->content = field_attach_view($entity, $display); - $this->content = drupal_render($entity->content); - $this->assertText($term->label(), 'Term label is displayed.'); - - // Delete the vocabulary and verify that the widget is gone. - taxonomy_vocabulary_delete($this->vocabulary->id()); - $this->drupalGet('test-entity/add/test_bundle'); - $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed'); - } - - /** - * Tests that vocabulary machine name changes are mirrored in field definitions. - */ - function testTaxonomyTermFieldChangeMachineName() { - // Add several entries in the 'allowed_values' setting, to make sure that - // they all get updated. - $this->field['settings']['allowed_values'] = array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => '0', - ), - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => '0', - ), - array( - 'vocabulary' => 'foo', - 'parent' => '0', - ), - ); - field_update_field($this->field); - // Change the machine name. - $new_name = drupal_strtolower($this->randomName()); - $this->vocabulary->vid = $new_name; - taxonomy_vocabulary_save($this->vocabulary); - - // Check that the field instance is still attached to the vocabulary. - $field = field_info_field($this->field_name); - $allowed_values = $field['settings']['allowed_values']; - $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.'); - $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.'); - $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.'); - } -} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php index f9cba0d..82716a9 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermIndexTest.php @@ -33,15 +33,10 @@ function setUp() { $this->field_name_1 = drupal_strtolower($this->randomName()); $this->field_1 = array( 'field_name' => $this->field_name_1, - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($this->field_1); @@ -52,26 +47,30 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance_1); entity_get_display('node', 'article', 'default') ->setComponent($this->field_name_1, array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); $this->field_name_2 = drupal_strtolower($this->randomName()); $this->field_2 = array( 'field_name' => $this->field_name_2, - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($this->field_2); @@ -82,11 +81,20 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance_2); entity_get_display('node', 'article', 'default') ->setComponent($this->field_name_2, array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } @@ -169,7 +177,7 @@ function testTaxonomyIndex() { $this->assertEqual(1, $index_count, 'Term 2 is indexed once.'); // Update the article to change one term. - $node->{$this->field_name_1}[$langcode] = array(array('tid' => $term_1->tid)); + $node->{$this->field_name_1}[$langcode] = array(array('target_id' => $term_1->tid)); $node->save(); // Check that both terms are indexed. @@ -185,7 +193,7 @@ function testTaxonomyIndex() { $this->assertEqual(1, $index_count, 'Term 2 is indexed.'); // Update the article to change another term. - $node->{$this->field_name_2}[$langcode] = array(array('tid' => $term_1->tid)); + $node->{$this->field_name_2}[$langcode] = array(array('target_id' => $term_1->tid)); $node->save(); // Check that only one term is indexed. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php index 93cc8d4..36a0e19 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php @@ -28,15 +28,10 @@ function setUp() { $field = array( 'field_name' => 'taxonomy_' . $this->vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -48,11 +43,20 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance); entity_get_display('node', 'article', 'default') ->setComponent($this->instance['field_name'], array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } @@ -143,7 +147,7 @@ function testNodeTermCreationAndDeletion() { // Enable tags in the vocabulary. $instance = $this->instance; $instance['widget'] = array( - 'type' => 'taxonomy_autocomplete', + 'type' => 'entity_reference_autocomplete_tags', 'settings' => array( 'placeholder' => 'Start typing here.', ), @@ -162,7 +166,7 @@ function testNodeTermCreationAndDeletion() { $edit["body[$langcode][0][value]"] = $this->randomName(); // Insert the terms in a comma separated list. Vocabulary 1 is a // free-tagging field created by the default profile. - $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms); + $edit[$instance['field_name'] . "[$langcode][target_id]"] = drupal_implode_tags($terms); // Verify the placeholder is there. $this->drupalGet('node/add/article'); @@ -210,74 +214,6 @@ function testNodeTermCreationAndDeletion() { } $this->assertNoText($term_objects['term1']->name, format_string('The deleted term %name does not appear on the node page.', array('%name' => $term_objects['term1']->name))); $this->assertNoText($term_objects['term2']->name, format_string('The deleted term %name does not appear on the node page.', array('%name' => $term_objects['term2']->name))); - - // Test autocomplete on term 3, which contains a comma. - // The term will be quoted, and the " will be encoded in unicode (\u0022). - $input = substr($term_objects['term3']->name, 0, 3); - $json = $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->id(), array('query' => array('q' => $input))); - $this->assertEqual($json, '{"\u0022' . $term_objects['term3']->name . '\u0022":"' . $term_objects['term3']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->name))); - - // Test autocomplete on term 4 - it is alphanumeric only, so no extra - // quoting. - $input = substr($term_objects['term4']->name, 0, 3); - $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->id(), array('query' => array('q' => $input))); - $this->assertRaw('{"' . $term_objects['term4']->name . '":"' . $term_objects['term4']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term4']->name))); - - // Test taxonomy autocomplete with a nonexistent field. - $field_name = $this->randomName(); - $tag = $this->randomName(); - $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name)); - $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name))); - $this->drupalGet('taxonomy/autocomplete/' . $field_name, array('query' => array('q' => $tag))); - $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.'); - } - - /** - * Tests term autocompletion edge cases with slashes in the names. - */ - function testTermAutocompletion() { - // Add a term with a slash in the name. - $first_term = $this->createTerm($this->vocabulary); - $first_term->name = '10/16/2011'; - taxonomy_term_save($first_term); - // Add another term that differs after the slash character. - $second_term = $this->createTerm($this->vocabulary); - $second_term->name = '10/17/2011'; - taxonomy_term_save($second_term); - // Add another term that has both a comma and a slash character. - $third_term = $this->createTerm($this->vocabulary); - $third_term->name = 'term with, a comma and / a slash'; - taxonomy_term_save($third_term); - - // Try to autocomplete a term name that matches both terms. - // We should get both term in a json encoded string. - $input = '10/'; - $path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary->id(); - // The result order is not guaranteed, so check each term separately. - $result = $this->drupalGet($path, array('query' => array('q' => $input))); - $data = drupal_json_decode($result); - $this->assertEqual($data[$first_term->name], check_plain($first_term->name), 'Autocomplete returned the first matching term'); - $this->assertEqual($data[$second_term->name], check_plain($second_term->name), 'Autocomplete returned the second matching term'); - - // Try to autocomplete a term name that matches first term. - // We should only get the first term in a json encoded string. - $input = '10/16'; - $path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary->id(); - $this->drupalGet($path, array('query' => array('q' => $input))); - $target = array($first_term->name => check_plain($first_term->name)); - $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns only the expected matching term.'); - - // Try to autocomplete a term name with both a comma and a slash. - $input = '"term with, comma and / a'; - $path = 'taxonomy/autocomplete/taxonomy_' . $this->vocabulary->id(); - $this->drupalGet($path, array('query' => array('q' => $input))); - $n = $third_term->name; - // Term names containing commas or quotes must be wrapped in quotes. - if (strpos($third_term->name, ',') !== FALSE || strpos($third_term->name, '"') !== FALSE) { - $n = '"' . str_replace('"', '""', $third_term->name) . '"'; - } - $target = array($n => check_plain($third_term->name)); - $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.'); } /** @@ -506,7 +442,7 @@ function testTaxonomyGetTermByName() { function testReSavingTags() { // Enable tags in the vocabulary. $instance = $this->instance; - $instance['widget'] = array('type' => 'taxonomy_autocomplete'); + $instance['widget'] = array('type' => 'entity_reference_autocomplete_tags'); field_update_instance($instance); // Create a term and a node using it. @@ -515,7 +451,7 @@ function testReSavingTags() { $edit = array(); $edit["title"] = $this->randomName(8); $edit["body[$langcode][0][value]"] = $this->randomName(16); - $edit[$this->instance['field_name'] . '[' . $langcode . ']'] = $term->label(); + $edit[$this->instance['field_name'] . '[' . $langcode . '][target_id]'] = $term->label() . '(' . $term->id() . ')'; $this->drupalPost('node/add/article', $edit, t('Save')); // Check that the term is displayed when editing and saving the node with no diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php index ea73152..6887c62 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TokenReplaceTest.php @@ -29,15 +29,10 @@ function setUp() { $field = array( 'field_name' => 'taxonomy_' . $this->vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -49,11 +44,20 @@ function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance); entity_get_display('node', 'article', 'default') ->setComponent('taxonomy_' . $this->vocabulary->id(), array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php index 5f98990..4a3777d 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php @@ -20,7 +20,7 @@ * * @var array */ - public static $modules = array('taxonomy', 'taxonomy_test_views'); + public static $modules = array('entity_reference', 'taxonomy', 'taxonomy_test_views'); /** * Stores the nodes used for the different tests. @@ -54,8 +54,8 @@ function setUp() { $node = array(); $node['type'] = 'article'; - $node['field_views_testing_tags'][]['tid'] = $this->term1->tid; - $node['field_views_testing_tags'][]['tid'] = $this->term2->tid; + $node['field_views_testing_tags'][]['target_id'] = $this->term1->tid; + $node['field_views_testing_tags'][]['target_id'] = $this->term2->tid; $this->nodes[] = $this->drupalCreateNode($node); $this->nodes[] = $this->drupalCreateNode($node); } @@ -82,16 +82,11 @@ protected function mockStandardInstall() { $this->vocabulary->save(); $field = array( 'field_name' => 'field_' . $this->vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', // Set cardinality to unlimited for tagging. 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -101,21 +96,30 @@ protected function mockStandardInstall() { 'label' => 'Tags', 'bundle' => 'article', 'widget' => array( - 'type' => 'taxonomy_autocomplete', + 'type' => 'entity_reference_autocomplete_tags', 'weight' => -4, ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($instance); entity_get_display('node', 'article', 'default') ->setComponent($instance['field_name'], array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); entity_get_display('node', 'article', 'teaser') ->setComponent($instance['field_name'], array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php deleted file mode 100644 index 7f7ab02..0000000 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php +++ /dev/null @@ -1,78 +0,0 @@ - 'integer', - 'label' => t('Referenced taxonomy term id.'), - ); - static::$propertyDefinitions['entity'] = array( - 'type' => 'entity', - 'constraints' => array( - 'EntityType' => 'taxonomy_term', - ), - 'label' => t('Term'), - 'description' => t('The referenced taxonomy term'), - // The entity object is computed out of the tid. - 'computed' => TRUE, - 'read-only' => FALSE, - 'settings' => array('id source' => 'tid'), - ); - } - return static::$propertyDefinitions; - } - - /** - * Overrides \Drupal\Core\Entity\Field\FieldItemBase::setValue(). - */ - public function setValue($values) { - // Treat the values as property value of the entity field, if no array - // is given. - if (!is_array($values)) { - $values = array('entity' => $values); - } - - // Entity is computed out of the ID, so we only need to update the ID. Only - // set the entity field if no ID is given. - if (isset($values['tid'])) { - $this->properties['tid']->setValue($values['tid']); - } - elseif (isset($values['entity'])) { - $this->properties['entity']->setValue($values['entity']); - } - else { - $this->properties['entity']->setValue(NULL); - } - unset($values['entity'], $values['tid']); - if ($values) { - throw new \InvalidArgumentException('Property ' . key($values) . ' is unknown.'); - } - } - -} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php index b14fc2f..51afca3 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php @@ -23,24 +23,6 @@ protected function postSave(EntityInterface $entity, $update) { field_attach_create_bundle('taxonomy_term', $entity->id()); } elseif ($entity->getOriginalID() != $entity->id()) { - // Reflect machine name changes in the definitions of existing 'taxonomy' - // fields. - $fields = field_read_fields(); - foreach ($fields as $field_name => $field) { - $update_field = FALSE; - if ($field['type'] == 'taxonomy_term_reference') { - foreach ($field['settings']['allowed_values'] as $key => &$value) { - if ($value['vocabulary'] == $entity->getOriginalID()) { - $value['vocabulary'] = $entity->id(); - $update_field = TRUE; - } - } - if ($update_field) { - field_update_field($field); - } - } - } - // Update bundles. field_attach_rename_bundle('taxonomy_term', $entity->getOriginalID(), $entity->id()); } parent::postSave($entity, $update); @@ -61,37 +43,12 @@ protected function preDelete($entities) { * Overrides Drupal\Core\Config\Entity\ConfigStorageController::postDelete(). */ protected function postDelete($entities) { - parent::postDelete($entities); - - $vocabularies = array(); - foreach ($entities as $vocabulary) { - $vocabularies[$vocabulary->id()] = $vocabulary->id(); - } - // Load all Taxonomy module fields and delete those which use only this - // vocabulary. - $taxonomy_fields = field_read_fields(array('module' => 'taxonomy')); - foreach ($taxonomy_fields as $field_name => $taxonomy_field) { - $modified_field = FALSE; - // Term reference fields may reference terms from more than one - // vocabulary. - foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) { - if (isset($vocabularies[$allowed_value['vocabulary']])) { - unset($taxonomy_field['settings']['allowed_values'][$key]); - $modified_field = TRUE; - } - } - if ($modified_field) { - if (empty($taxonomy_field['settings']['allowed_values'])) { - field_delete_field($field_name); - } - else { - // Update the field definition with the new allowed values. - field_update_field($taxonomy_field); - } - } + foreach ($entities as $entity) { + field_attach_delete_bundle('taxonomy_term', $entity->id()); } - // Reset caches. - $this->resetCache(array_keys($vocabularies)); + + parent::postDelete($entities); + $this->resetCache(array_keys($entities)); } /** diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install index fef48fb..87d742b 100644 --- a/core/modules/taxonomy/taxonomy.install +++ b/core/modules/taxonomy/taxonomy.install @@ -5,6 +5,7 @@ * Install, update and uninstall functions for the taxonomy module. */ +use Drupal\Component\Utility\NestedArray; use Drupal\Component\Uuid\Uuid; /** @@ -170,30 +171,6 @@ function taxonomy_schema() { } /** - * Implements hook_field_schema(). - */ -function taxonomy_field_schema($field) { - return array( - 'columns' => array( - 'tid' => array( - 'type' => 'int', - 'unsigned' => TRUE, - 'not null' => FALSE, - ), - ), - 'indexes' => array( - 'tid' => array('tid'), - ), - 'foreign keys' => array( - 'tid' => array( - 'table' => 'taxonomy_term_data', - 'columns' => array('tid' => 'tid'), - ), - ), - ); -} - -/** * Remove the {taxonomy_vocabulary}.module field. */ function taxonomy_update_8000() { @@ -335,3 +312,116 @@ function taxonomy_update_8006() { ->execute(); } } + +/** + * Convert taxonomy term fields to entity reference. + */ +function taxonomy_update_8007() { + if (!$fields = field_read_fields(array('type' => 'taxonomy_term_reference'), array('include_inactive' => 1))) { + return; + } + update_module_enable(array('entity_reference')); + module_load_install('entity_reference'); + + foreach ($fields as $field_name => $field) { + if ($field['storage']['type'] != 'field_sql_storage') { + // Field doesn't use SQL storage, we cannot modify the schema. + continue; + } + + $instances = field_read_instances(array('field_id' => $field['id']), array('include_inactive' => 1)); + + $tables = array( + _field_sql_storage_tablename($field), + _field_sql_storage_revision_tablename($field), + ); + + foreach ($tables as $table_name) { + db_change_field($table_name, $field_name . '_tid', $field_name . '_target_id', array( + 'description' => 'The ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + )); + + // Add the revision ID column. + $column = array( + 'description' => 'The revision ID of the target entity.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + ); + db_add_field($table_name, $field_name . '_revision_id', $column); + + // Change the index. + db_drop_index($table_name, $field_name . '_tid'); + db_add_index($table_name, $field_name . '_target_id', array($field_name . '_target_id')); + } + + // Update the field settings. + $field['settings']['target_type'] = 'taxonomy_term'; + $schema = entity_reference_field_schema($field); + unset($field['columns'], $field['indexes'], $field['foreign keys']); + $field = NestedArray::mergeDeep($field, $schema); + + // @todo: Bring back $field['settings']['allowed_values'][0]['parent'] in + // http://drupal.org/node/1915056. + $vocabulary_name = $field['settings']['allowed_values'][0]['vocabulary']; + unset($field['settings']['allowed_values']); + + $field['module'] = $field['type'] = 'entity_reference'; + + _update_7000_field_update_field($field); + + // Update the instance settings. + if (!$instances) { + continue; + } + + $field = field_info_field($field_name); + + foreach ($instances as $instance) { + if ($instance['widget']['type'] == 'taxonomy_autocomplete') { + // Update the widget. + $instance['widget']['type'] = 'entity_reference_autocomplete_tags'; + $instance['widget']['module'] = 'entity_reference'; + $instance['widget']['settings']['match_operator'] = "CONTAINS"; + $instance['widget']['settings']['autocomplete_path'] = "entity_reference/autocomplete/tags"; + } + + $instance['settings']['handler'] = 'default'; + $instance['settings']['handler_settings'] = array( + 'target_bundles' => array($vocabulary_name), + // Enable auto-create. + 'auto_create' => TRUE, + ); + + _update_7000_field_update_instance($field, $instance); + + if (empty($field['bundles'])) { + continue; + } + + // Update the formatter. + foreach ($field['bundles'] as $entity_type => $bundles) { + $entity_info = drupal_container()->get('plugin.manager.entity')->getDefinition($entity_type); + foreach ($bundles as $bundle) { + // Add the 'default' view mode. + $entity_info['view_modes'][] = 'default'; + foreach (array_keys($entity_info['view_modes']) as $view_mode) { + $display = _update_8000_entity_get_display($entity_type, $bundle, $view_mode); + $original_display = $display->get('content.' . $field_name); + $display->set('content.' . $field_name, array( + 'label' => $original_display['label'], + 'weight' => $original_display['weight'], + 'type' => 'entity_reference_label', + 'settings' => array('link' => TRUE), + )) + ->save(); + update_config_manifest_add('entity.display', array($display->get('id'))); + } + } + } + } + } +} diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 43d2ac8..5155d45 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -26,14 +26,6 @@ const TAXONOMY_HIERARCHY_MULTIPLE = 2; /** - * Users can create new terms in a free-tagging vocabulary when - * submitting a taxonomy_autocomplete_widget. We store a term object - * whose tid is 'autocreate' as a field data item during widget - * validation and then actually create the term if/when that field - * data item makes it to taxonomy_field_insert/update(). - */ - -/** * Implements hook_help(). */ function taxonomy_help($path, $arg) { @@ -323,14 +315,6 @@ function taxonomy_menu() { 'type' => MENU_CALLBACK, 'file' => 'taxonomy.pages.inc', ); - $items['taxonomy/autocomplete/%'] = array( - 'title' => 'Autocomplete taxonomy', - 'page callback' => 'taxonomy_autocomplete', - 'page arguments' => array(2), - 'access arguments' => array('access content'), - 'type' => MENU_CALLBACK, - 'file' => 'taxonomy.pages.inc', - ); $items['admin/structure/taxonomy/%taxonomy_vocabulary'] = array( 'title callback' => 'entity_page_label', @@ -958,274 +942,6 @@ function taxonomy_implode_tags($tags, $vid = NULL) { } /** - * Implements hook_field_info(). - * - * Field settings: - * - allowed_values: a list array of one or more vocabulary trees: - * - vocabulary: a vocabulary machine name. - * - parent: a term ID of a term whose children are allowed. This should be - * '0' if all terms in a vocabulary are allowed. The allowed values do not - * include the parent term. - * - */ -function taxonomy_field_info() { - return array( - 'taxonomy_term_reference' => array( - 'label' => t('Term reference'), - 'description' => t('This field stores a reference to a taxonomy term.'), - 'default_widget' => 'options_select', - 'default_formatter' => 'taxonomy_term_reference_link', - 'field item class' => 'Drupal\taxonomy\Type\TaxonomyTermReferenceItem', - 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => '', - 'parent' => '0', - ), - ), - ), - ), - ); -} - -/** - * Implements hook_field_widget_info_alter(). - */ -function taxonomy_field_widget_info_alter(&$info) { - $info['options_select']['field_types'][] = 'taxonomy_term_reference'; - $info['options_buttons']['field_types'][] = 'taxonomy_term_reference'; -} - -/** - * Implements hook_options_list(). - */ -function taxonomy_options_list($field, $instance, $entity) { - $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'taxonomy_allowed_values'; - return $function($field, $instance, $entity); -} - -/** - * Implements hook_field_validate(). - * - * Taxonomy field settings allow for either a single vocabulary ID, multiple - * vocabulary IDs, or sub-trees of a vocabulary to be specified as allowed - * values, although only the first of these is supported via the field UI. - * Confirm that terms entered as values meet at least one of these conditions. - * - * Possible error codes: - * - 'taxonomy_term_illegal_value': The value is not part of the list of allowed values. - */ -function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instance, $langcode, $items, &$errors) { - // Build an array of existing term IDs so they can be loaded with - // taxonomy_term_load_multiple(); - foreach ($items as $delta => $item) { - if (!empty($item['tid']) && $item['tid'] != 'autocreate') { - $tids[] = $item['tid']; - } - } - if (!empty($tids)) { - $terms = taxonomy_term_load_multiple($tids); - - // Check each existing item to ensure it can be found in the - // allowed values for this field. - foreach ($items as $delta => $item) { - $validate = TRUE; - if (!empty($item['tid']) && $item['tid'] != 'autocreate') { - $validate = FALSE; - foreach ($field['settings']['allowed_values'] as $settings) { - // If no parent is specified, check if the term is in the vocabulary. - if (isset($settings['vocabulary']) && empty($settings['parent'])) { - if ($settings['vocabulary'] == $terms[$item['tid']]->bundle()) { - $validate = TRUE; - break; - } - } - // If a parent is specified, then to validate it must appear in the - // array returned by taxonomy_term_load_parents_all(). - elseif (!empty($settings['parent'])) { - $ancestors = taxonomy_term_load_parents_all($item['tid']); - foreach ($ancestors as $ancestor) { - if ($ancestor->tid == $settings['parent']) { - $validate = TRUE; - break 2; - } - } - } - } - } - if (!$validate) { - $errors[$field['field_name']][$langcode][$delta][] = array( - 'error' => 'taxonomy_term_reference_illegal_value', - 'message' => t('%name: illegal value.', array('%name' => $instance['label'])), - ); - } - } - } -} - -/** - * Implements hook_field_is_empty(). - */ -function taxonomy_field_is_empty($item, $field) { - return !is_array($item) || (empty($item['tid']) && empty($item['entity'])); -} - -/** - * Implements hook_field_formatter_info(). - */ -function taxonomy_field_formatter_info() { - return array( - 'taxonomy_term_reference_link' => array( - 'label' => t('Link'), - 'field types' => array('taxonomy_term_reference'), - ), - 'taxonomy_term_reference_plain' => array( - 'label' => t('Plain text'), - 'field types' => array('taxonomy_term_reference'), - ), - 'taxonomy_term_reference_rss_category' => array( - 'label' => t('RSS category'), - 'field types' => array('taxonomy_term_reference'), - ), - ); -} - -/** - * Implements hook_field_formatter_view(). - */ -function taxonomy_field_formatter_view(EntityInterface $entity, $field, $instance, $langcode, $items, $display) { - $element = array(); - - // Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is not - // set. Theme such terms as just their name. - - switch ($display['type']) { - case 'taxonomy_term_reference_link': - foreach ($items as $delta => $item) { - if ($item['tid'] == 'autocreate') { - $element[$delta] = array( - '#markup' => check_plain($item['name']), - ); - } - else { - $term = $item['entity']; - $uri = $term->uri(); - $element[$delta] = array( - '#type' => 'link', - '#title' => $term->label(), - '#href' => $uri['path'], - '#options' => $uri['options'], - ); - } - } - break; - - case 'taxonomy_term_reference_plain': - foreach ($items as $delta => $item) { - $name = ($item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name']); - $element[$delta] = array( - '#markup' => check_plain($name), - ); - } - break; - - case 'taxonomy_term_reference_rss_category': - foreach ($items as $delta => $item) { - $entity->rss_elements[] = array( - 'key' => 'category', - 'value' => $item['tid'] != 'autocreate' ? $item['entity']->label() : $item['name'], - 'attributes' => array( - 'domain' => $item['tid'] != 'autocreate' ? url('taxonomy/term/' . $item['tid'], array('absolute' => TRUE)) : '', - ), - ); - } - break; - } - - return $element; -} - -/** - * Returns the set of valid terms for a taxonomy field. - * - * @param $field - * The field definition. - * @param $instance - * The instance definition. It is recommended to only use instance level - * properties to filter out values from a list defined by field level - * properties. - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity object the field is attached to, or NULL if no entity - * exists (e.g. in field settings page). - * - * @return - * The array of valid terms for this field, keyed by term id. - */ -function taxonomy_allowed_values($field, $instance, EntityInterface $entity) { - $options = array(); - foreach ($field['settings']['allowed_values'] as $tree) { - if ($vocabulary = taxonomy_vocabulary_load($tree['vocabulary'])) { - if ($terms = taxonomy_get_tree($vocabulary->id(), $tree['parent'], NULL, TRUE)) { - foreach ($terms as $term) { - $options[$term->tid] = str_repeat('-', $term->depth) . $term->label(); - } - } - } - } - return $options; -} - -/** - * Implements hook_field_formatter_prepare_view(). - * - * This preloads all taxonomy terms for multiple loaded objects at once and - * unsets values for invalid terms that do not exist. - */ -function taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) { - $tids = array(); - - // Collect every possible term attached to any of the fieldable entities. - foreach ($entities as $id => $entity) { - foreach ($items[$id] as $delta => $item) { - // Force the array key to prevent duplicates. - if ($item['tid'] != 'autocreate' && $item['tid'] !== FALSE) { - $tids[$item['tid']] = $item['tid']; - } - } - } - if ($tids) { - $terms = taxonomy_term_load_multiple($tids); - - // Iterate through the fieldable entities again to attach the loaded term data. - foreach ($entities as $id => $entity) { - $rekey = FALSE; - - foreach ($items[$id] as $delta => $item) { - // Check whether the taxonomy term field instance value could be loaded. - if (isset($terms[$item['tid']])) { - // Replace the instance value with the term data. - $items[$id][$delta]['entity'] = $terms[$item['tid']]; - } - // Terms to be created are not in $terms, but are still legitimate. - elseif ($item['tid'] == 'autocreate') { - // Leave the item in place. - } - // Otherwise, unset the instance value, since the term does not exist. - else { - unset($items[$id][$delta]); - $rekey = TRUE; - } - } - - if ($rekey) { - // Rekey the items array. - $items[$id] = array_values($items[$id]); - } - } - } -} - -/** * Title callback for term pages. * * @param Drupal\taxonomy\Plugin\Core\Entity\Term $term @@ -1239,52 +955,6 @@ function taxonomy_term_title(Term $term) { } /** - * Form element validate handler for taxonomy term autocomplete element. - */ -function taxonomy_autocomplete_validate($element, &$form_state) { - // Split the values into an array. - // @see Drupal\taxonomy\Plugin\field\widget\TaxonomyAutocompleteWidget:massageFormValues() - $typed_terms = array(); - if ($tags = $element['#value']) { - $typed_terms = drupal_explode_tags($tags); - } - form_set_value($element, $typed_terms, $form_state); -} - -/** - * Implements hook_field_settings_form(). - */ -function taxonomy_field_settings_form($field, $instance, $has_data) { - // Get proper values for 'allowed_values_function', which is a core setting. - $vocabularies = taxonomy_vocabulary_load_multiple(); - $options = array(); - foreach ($vocabularies as $vocabulary) { - $options[$vocabulary->id()] = $vocabulary->name; - } - $form['allowed_values'] = array( - '#tree' => TRUE, - ); - - foreach ($field['settings']['allowed_values'] as $delta => $tree) { - $form['allowed_values'][$delta]['vocabulary'] = array( - '#type' => 'select', - '#title' => t('Vocabulary'), - '#default_value' => $tree['vocabulary'], - '#options' => $options, - '#required' => TRUE, - '#description' => t('The vocabulary which supplies the options for this field.'), - '#disabled' => $has_data, - ); - $form['allowed_values'][$delta]['parent'] = array( - '#type' => 'value', - '#value' => $tree['parent'], - ); - } - - return $form; -} - -/** * Implements hook_rdf_mapping(). * * @return array @@ -1348,21 +1018,6 @@ function taxonomy_rdf_mapping() { */ /** - * Implements hook_field_presave(). - * - * Create any new terms defined in a freetagging vocabulary. - */ -function taxonomy_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) { - foreach ($items as $delta => $item) { - if (!$item['tid'] && isset($item['entity'])) { - unset($item['tid']); - taxonomy_term_save($item['entity']); - $items[$delta]['tid'] = $item['entity']->tid; - } - } -} - -/** * Implements hook_node_insert(). */ function taxonomy_node_insert(EntityInterface $node) { @@ -1402,7 +1057,7 @@ function taxonomy_build_node_index($node) { foreach (field_info_instances('node', $node->type) as $instance) { $field_name = $instance['field_name']; $field = field_info_field($field_name); - if ($field['module'] == 'taxonomy' && $field['storage']['type'] == 'field_sql_storage') { + if ($field['module'] == 'entity_reference' && $field['settings']['target_type'] == 'taxonomy_term' && $field['storage']['type'] == 'field_sql_storage') { // If a field value is not set in the node object when node_save() is // called, the old value from $node->original is used. if (isset($node->{$field_name})) { @@ -1417,7 +1072,7 @@ function taxonomy_build_node_index($node) { foreach (field_available_languages('node', $field) as $langcode) { if (!empty($items[$langcode])) { foreach ($items[$langcode] as $item) { - $tid_all[$item['tid']] = $item['tid']; + $tid_all[$item['target_id']] = $item['target_id']; } } } diff --git a/core/modules/taxonomy/taxonomy.pages.inc b/core/modules/taxonomy/taxonomy.pages.inc index b6f1329..75558af 100644 --- a/core/modules/taxonomy/taxonomy.pages.inc +++ b/core/modules/taxonomy/taxonomy.pages.inc @@ -77,86 +77,3 @@ function taxonomy_term_feed(Term $term) { return node_feed($nids, $channel); } - -/** - * Page callback: Outputs JSON for taxonomy autocomplete suggestions. - * - * This callback outputs term name suggestions in response to Ajax requests - * made by the taxonomy autocomplete widget for taxonomy term reference - * fields. The output is a JSON object of plain-text term suggestions, keyed by - * the user-entered value with the completed term name appended. Term names - * containing commas are wrapped in quotes. - * - * For example, suppose the user has entered the string 'red fish, blue' in the - * field, and there are two taxonomy terms, 'blue fish' and 'blue moon'. The - * JSON output would have the following structure: - * @code - * { - * "red fish, blue fish": "blue fish", - * "red fish, blue moon": "blue moon", - * }; - * @endcode - * - * @param $field_name - * The name of the term reference field. - * - * @see taxonomy_menu() - * @see taxonomy_field_widget_info() - */ -function taxonomy_autocomplete($field_name) { - // A comma-separated list of term names entered in the autocomplete form - // element. Only the last term is used for autocompletion. - $tags_typed = drupal_container()->get('request')->query->get('q'); - - // Make sure the field exists and is a taxonomy field. - if (!($field = field_info_field($field_name)) || $field['type'] !== 'taxonomy_term_reference') { - // Error string. The JavaScript handler will realize this is not JSON and - // will display it as debugging information. - print t('Taxonomy field @field_name not found.', array('@field_name' => $field_name)); - exit; - } - - // The user enters a comma-separated list of tags. We only autocomplete the last tag. - $tags_typed = drupal_explode_tags($tags_typed); - $tag_last = drupal_strtolower(array_pop($tags_typed)); - - $matches = array(); - if ($tag_last != '') { - - // Part of the criteria for the query come from the field's own settings. - $vids = array(); - foreach ($field['settings']['allowed_values'] as $tree) { - $vids[] = $tree['vocabulary']; - } - - $query = db_select('taxonomy_term_data', 't'); - $query->addTag('term_access'); - - // Do not select already entered terms. - if (!empty($tags_typed)) { - $query->condition('t.name', $tags_typed, 'NOT IN'); - } - // Select rows that match by term name. - $tags_return = $query - ->fields('t', array('tid', 'name')) - ->condition('t.vid', $vids) - ->condition('t.name', '%' . db_like($tag_last) . '%', 'LIKE') - ->range(0, 10) - ->execute() - ->fetchAllKeyed(); - - $prefix = count($tags_typed) ? drupal_implode_tags($tags_typed) . ', ' : ''; - - $term_matches = array(); - foreach ($tags_return as $tid => $name) { - $n = $name; - // Term names containing commas or quotes must be wrapped in quotes. - if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) { - $n = '"' . str_replace('"', '""', $name) . '"'; - } - $term_matches[$prefix . $n] = check_plain($name); - } - } - - return new JsonResponse($term_matches); -} diff --git a/core/modules/taxonomy/taxonomy.views.inc b/core/modules/taxonomy/taxonomy.views.inc index 0644f82..84f63b7 100644 --- a/core/modules/taxonomy/taxonomy.views.inc +++ b/core/modules/taxonomy/taxonomy.views.inc @@ -328,78 +328,6 @@ function taxonomy_views_data_alter(&$data) { } /** - * Implements hook_field_views_data(). - * - * Views integration for taxonomy_term_reference fields. Adds a term relationship to the default - * field data. - * - * @see field_views_field_default_views_data() - */ -function taxonomy_field_views_data($field) { - $data = field_views_field_default_views_data($field); - foreach ($data as $table_name => $table_data) { - foreach ($table_data as $field_name => $field_data) { - if (isset($field_data['filter']) && $field_name != 'delta') { - $data[$table_name][$field_name]['filter']['id'] = 'taxonomy_index_tid'; - $data[$table_name][$field_name]['filter']['vocabulary'] = $field['settings']['allowed_values'][0]['vocabulary']; - } - } - - // Add the relationship only on the tid field. - $data[$table_name][$field['field_name'] . '_tid']['relationship'] = array( - 'id' => 'standard', - 'base' => 'taxonomy_term_data', - 'base field' => 'tid', - 'label' => t('term from !field_name', array('!field_name' => $field['field_name'])), - ); - - } - - return $data; -} - -/** - * Implements hook_field_views_data_views_data_alter(). - * - * Views integration to provide reverse relationships on term references. - */ -function taxonomy_field_views_data_views_data_alter(&$data, $field) { - foreach ($field['bundles'] as $entity_type => $bundles) { - $entity_info = entity_get_info($entity_type); - $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type; - - list($label, $all_labels) = field_views_field_label($field['field_name']); - $entity = $entity_info['label']; - if ($entity == t('Node')) { - $entity = t('Content'); - } - - $data['taxonomy_term_data'][$pseudo_field_name]['relationship'] = array( - 'title' => t('@entity using @field', array('@entity' => $entity, '@field' => $label)), - 'help' => t('Relate each @entity with a @field set to the term.', array('@entity' => $entity, '@field' => $label)), - 'id' => 'entity_reverse', - 'field_name' => $field['field_name'], - 'field table' => _field_sql_storage_tablename($field), - 'field field' => $field['field_name'] . '_tid', - 'base' => $entity_info['base_table'], - 'base field' => $entity_info['entity_keys']['id'], - 'label' => t('!field_name', array('!field_name' => $field['field_name'])), - 'join_extra' => array( - 0 => array( - 'field' => 'entity_type', - 'value' => $entity_type, - ), - 1 => array( - 'field' => 'deleted', - 'value' => 0, - 'numeric' => TRUE, - ), - ), - ); - } -} - -/** * Helper function to set a breadcrumb for taxonomy. */ function views_taxonomy_set_breadcrumb(&$breadcrumb, &$argument) { diff --git a/core/modules/views/includes/ajax.inc b/core/modules/views/includes/ajax.inc index 65ecd3f..c88a941 100644 --- a/core/modules/views/includes/ajax.inc +++ b/core/modules/views/includes/ajax.inc @@ -148,7 +148,7 @@ function views_ajax_form_wrapper($form_id, &$form_state) { * @param $vid * The vocabulary id of the tags which should be returned. * - * @see taxonomy_autocomplete() + * @see \Drupal\entity_reference\Plugin\field\widget\AutocompleteTagsWidget */ function views_ajax_autocomplete_taxonomy($vid) { // The user enters a comma-separated list of tags. We only autocomplete the last tag. diff --git a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php index c4f90b8..e4b9c12 100644 --- a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php @@ -20,7 +20,7 @@ class DefaultViewsTest extends WebTestBase { * * @var array */ - public static $modules = array('views', 'node', 'search', 'comment', 'taxonomy', 'block'); + public static $modules = array('views', 'node', 'search', 'comment', 'entity_reference', 'taxonomy', 'block'); /** * An array of argument arrays to use for default views. @@ -62,15 +62,10 @@ protected function setUp() { $this->field_name = drupal_strtolower($this->randomName()); $this->field = array( 'field_name' => $this->field_name, - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->vocabulary->id(), - 'parent' => '0', - ), - ), - ) + 'target_type' => 'taxonomy_term', + ), ); field_create_field($this->field); $this->instance = array( @@ -80,11 +75,20 @@ protected function setUp() { 'widget' => array( 'type' => 'options_select', ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($this->instance); entity_get_display('node', 'page', 'full') ->setComponent($this->field_name, array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); @@ -96,7 +100,7 @@ protected function setUp() { $term = $this->createTerm($this->vocabulary); $values = array('created' => $time, 'type' => 'page'); - $values[$this->field_name][]['tid'] = $term->tid; + $values[$this->field_name][]['target_id'] = $term->tid; // Make every other node promoted. if ($i % 2) { diff --git a/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php b/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php index 6416193..b2f8be5 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Wizard/TaggedWithTest.php @@ -17,7 +17,7 @@ class TaggedWithTest extends WizardTestBase { * * @var array */ - public static $modules = array('taxonomy'); + public static $modules = array('entity_reference', 'taxonomy'); protected $node_type_with_tags; @@ -55,15 +55,10 @@ function setUp() { // Create the tag field itself. $this->tag_field = array( 'field_name' => 'field_views_testing_tags', - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $this->tag_vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($this->tag_field); @@ -75,20 +70,29 @@ function setUp() { 'entity_type' => 'node', 'bundle' => $this->node_type_with_tags->type, 'widget' => array( - 'type' => 'taxonomy_autocomplete', + 'type' => 'entity_reference_autocomplete_tags', + ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $this->tag_vocabulary->id(), + ), + 'auto_create' => TRUE, + ), ), ); field_create_instance($this->tag_instance); entity_get_display('node', $this->node_type_with_tags->type, 'default') ->setComponent('field_views_testing_tags', array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); entity_get_display('node', $this->node_type_with_tags->type, 'teaser') ->setComponent('field_views_testing_tags', array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); @@ -103,7 +107,7 @@ function testTaggedWith() { $node_add_path = 'node/add/' . $this->node_type_with_tags->type; // Create three nodes, with different tags. - $tag_field = $this->tag_field['field_name'] . '[' . LANGUAGE_NOT_SPECIFIED . ']'; + $tag_field = $this->tag_field['field_name'] . '[' . LANGUAGE_NOT_SPECIFIED . '][target_id]'; $edit = array(); $edit['title'] = $node_tag1_title = $this->randomName(); $edit[$tag_field] = 'tag1'; diff --git a/core/modules/views/views_ui/admin.inc b/core/modules/views/views_ui/admin.inc index df344d2..15c313a 100644 --- a/core/modules/views/views_ui/admin.inc +++ b/core/modules/views/views_ui/admin.inc @@ -222,23 +222,15 @@ function views_ui_nojs_submit($form, &$form_state) { function views_ui_taxonomy_autocomplete_validate($element, &$form_state) { $value = array(); if ($tags = $element['#value']) { - // Get the machine names of the vocabularies we will search, keyed by the - // vocabulary IDs. $field = field_info_field($element['#field_name']); - $vocabularies = array(); - if (!empty($field['settings']['allowed_values'])) { - foreach ($field['settings']['allowed_values'] as $tree) { - if ($vocabulary = entity_load('taxonomy_vocabulary', $tree['vocabulary'])) { - $vocabularies[$vocabulary->id()] = $tree['vocabulary']; - } - } - } + $instance = $element['#field_instance']; + $handler = entity_reference_get_selection_handler($field, $instance); + // Store the term ID of each (valid) tag that the user typed. $typed_terms = drupal_explode_tags($tags); foreach ($typed_terms as $typed_term) { - if ($terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($typed_term), 'vid' => array_keys($vocabularies)))) { - $term = array_pop($terms); - $value['tids'][] = $term->tid; + if ($entity_id = $handler->validateAutocompleteInput($typed_term, $element, $form_state, NULL, FALSE)) { + $value['tids'][] = $entity_id; } } // Store the term IDs along with the name of the vocabulary. Currently @@ -247,7 +239,7 @@ function views_ui_taxonomy_autocomplete_validate($element, &$form_state) { // one. if (!empty($value['tids'])) { $value['tids'] = array_unique($value['tids']); - $value['vocabulary'] = array_pop($vocabularies); + $value['vocabulary'] = array_pop($instance['settings']['handler_settings']['target_bundles']); } } form_set_value($element, $value, $form_state); diff --git a/core/profiles/standard/standard.info.yml b/core/profiles/standard/standard.info.yml index a0ff1f9..271dea7 100644 --- a/core/profiles/standard/standard.info.yml +++ b/core/profiles/standard/standard.info.yml @@ -17,6 +17,7 @@ dependencies: - custom_block - edit - editor + - entity_reference - help - image - menu diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 13463b9..2f92d42 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -107,16 +107,11 @@ function standard_install() { $field = array( 'field_name' => 'field_' . $vocabulary->id(), - 'type' => 'taxonomy_term_reference', + 'type' => 'entity_reference', // Set cardinality to unlimited for tagging. 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'settings' => array( - 'allowed_values' => array( - array( - 'vocabulary' => $vocabulary->id(), - 'parent' => 0, - ), - ), + 'target_type' => 'taxonomy_term', ), ); field_create_field($field); @@ -128,22 +123,33 @@ function standard_install() { 'bundle' => 'article', 'description' => $vocabulary->help, 'widget' => array( - 'type' => 'taxonomy_autocomplete', + 'type' => 'entity_reference_autocomplete_tags', 'weight' => -4, ), + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + 'target_bundles' => array( + $vocabulary->id(), + ), + 'auto_create' => TRUE, + ), + ), ); field_create_instance($instance); // Assign display settings for the 'default' and 'teaser' view modes. entity_get_display('node', 'article', 'default') ->setComponent($instance['field_name'], array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', + 'settings' => array('link' => TRUE), 'weight' => 10, )) ->save(); entity_get_display('node', 'article', 'teaser') ->setComponent($instance['field_name'], array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', + 'settings' => array('link' => TRUE), 'weight' => 10, )) ->save(); diff --git a/core/themes/bartik/css/style-rtl.css b/core/themes/bartik/css/style-rtl.css index dc7c2a4..68eaa13 100644 --- a/core/themes/bartik/css/style-rtl.css +++ b/core/themes/bartik/css/style-rtl.css @@ -88,11 +88,11 @@ ul.tips { margin-left: 20px; margin-right: 0; } -.field-type-taxonomy-term-reference .field-label { +.field-type-entity-reference-taxonomy-term .field-label { padding-left: 5px; padding-right: 0; } -.field-type-taxonomy-term-reference ul.links li { +.field-type-entity-reference-taxonomy-term ul.links li { padding: 0 0 0 1em; float: right; } diff --git a/core/themes/bartik/css/style.css b/core/themes/bartik/css/style.css index 26534c7..4e783e8 100644 --- a/core/themes/bartik/css/style.css +++ b/core/themes/bartik/css/style.css @@ -117,7 +117,7 @@ ul.contextual-links, ul.links, ul.primary, .item-list .pager, -div.field-type-taxonomy-term-reference, +div.field-type-entity-reference-taxonomy-term, div.messages, div.meta, p.comment-time, @@ -626,28 +626,28 @@ h1#page-title { float: left; /* LTR */ margin: 1px 20px 0 0; /* LTR */ } -.field-type-taxonomy-term-reference { +.field-type-entity-reference-taxonomy-term { margin: 0 0 1.2em; } -.field-type-taxonomy-term-reference .field-label { +.field-type-entity-reference-taxonomy-term .field-label { font-weight: normal; margin: 0; padding-right: 5px; /* LTR */ } -.field-type-taxonomy-term-reference .field-label, -.field-type-taxonomy-term-reference ul.links { +.field-type-entity-reference-taxonomy-term .field-label, +.field-type-entity-reference-taxonomy-term ul.links { font-size: 0.8em; } -.view-mode-teaser .field-type-taxonomy-term-reference .field-label, -.view-mode-teaser .field-type-taxonomy-term-reference ul.links { +.view-mode-teaser .field-type-entity-reference-taxonomy-term .field-label, +.view-mode-teaser .field-type-entity-reference-taxonomy-term ul.links { font-size: 0.821em; } -.field-type-taxonomy-term-reference ul.links { +.field-type-entity-reference-taxonomy-term ul.links { padding: 0; margin: 0; list-style: none; } -.field-type-taxonomy-term-reference ul.links li { +.field-type-entity-reference-taxonomy-term ul.links li { float: left; /* LTR */ padding: 0 1em 0 0; /* LTR */ white-space: nowrap; diff --git a/core/themes/bartik/template.php b/core/themes/bartik/template.php index 9a6c8a9..82775a3 100644 --- a/core/themes/bartik/template.php +++ b/core/themes/bartik/template.php @@ -130,7 +130,13 @@ function bartik_menu_tree($variables) { /** * Implements theme_field__field_type(). */ -function bartik_field__taxonomy_term_reference($variables) { +function bartik_field__entity_reference($variables) { + // We only want to customize the output of fields that reference taxonomy_term + // entities. + if (isset($variables['element']['#entity_reference_target_type']) && $variables['element']['#entity_reference_target_type'] != 'taxonomy_term') { + return theme_field($variables); + } + $output = ''; // Render the label, if it's not hidden. @@ -141,10 +147,13 @@ function bartik_field__taxonomy_term_reference($variables) { // Render the items. $output .= ($variables['element']['#label_display'] == 'inline') ? '