diff --git a/core/modules/field/field.crud.inc b/core/modules/field/field.crud.inc index f6ee8a2..1f78f6f 100644 --- a/core/modules/field/field.crud.inc +++ b/core/modules/field/field.crud.inc @@ -542,7 +542,6 @@ function field_create_instance(&$instance) { * @see field_create_instance() */ function field_update_instance($instance) { - // Check that the specified field exists. $field = field_read_field($instance['field_name']); if (empty($field)) { throw new FieldException(t('Attempt to update an instance of a nonexistent field @field.', array('@field' => $instance['field_name']))); @@ -605,6 +604,7 @@ function _field_write_instance(&$instance, $update = FALSE) { 'type' => $field_type['default_widget'], 'settings' => array(), ); + // If no weight specified, make sure the field sinks at the bottom. if (!isset($instance['widget']['weight'])) { $max_weight = field_info_max_weight($instance['entity_type'], $instance['bundle'], 'form'); diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 695fd89..02e8a8b 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -5,6 +5,8 @@ * Install, update, and uninstall functions for the Field module. */ +use Drupal\Component\Utility\NestedArray; + /** * Implements hook_schema(). */ @@ -170,9 +172,14 @@ 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 +235,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(); - // Create storage for the field. - field_sql_storage_field_storage_create_field($field); + if ($update) { + db_update('field_config') + ->condition('id', $field['id']) + ->fields($record) + ->execute(); + } + else { + // We don't use drupal_write_record() here because it depends on the schema. + $field['id'] = db_insert('field_config') + ->fields($record) + ->execute(); + + // 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 +375,16 @@ 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); } /** @@ -484,6 +543,120 @@ function field_update_8002() { } /** + * Convert taxonomy term fields to entity reference field types. + */ +function field_update_8003() { + if (!$fields = field_read_fields(array('type' => 'taxonomy_term_reference'), array('include_inactive' => 1))) { + return; + } + + module_load_install('entity_reference'); + + // Clear the fields cache, so we can get the widget definition. + field_cache_clear(); + $widget_info = field_info_widget_types('entity_reference_autocomplete_tags'); + + 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: Deal with ['allowed_values'][0]['parent']. + $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'] = $widget_info; + $instance['widget']['type'] = '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 = entity_get_info($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'))); + } + } + } + } + } +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php index 09c8fda..b5d092e 100644 --- a/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php +++ b/core/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php @@ -9,6 +9,7 @@ use Drupal\Core\Database\Query\SelectInterface; use Drupal\Core\Entity\Query\QueryException; +use Drupal\Core\Entity\EntityNG; /** * Adds tables and fields to the SQL entity query. 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 b/core/modules/forum/forum.info index 79317ea..a5428aa 100644 --- a/core/modules/forum/forum.info +++ b/core/modules/forum/forum.info @@ -3,6 +3,7 @@ description = Provides discussion forums. dependencies[] = node dependencies[] = history dependencies[] = taxonomy +dependencies[] = entity_reference dependencies[] = comment package = Core version = VERSION 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 872eb95..7995695 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -293,11 +293,11 @@ function forum_node_validate(Node $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; @@ -326,11 +326,11 @@ function forum_node_presave(Node $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); } } } @@ -528,7 +528,7 @@ function forum_field_storage_pre_insert(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, @@ -564,7 +564,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 5070396..87c91bb 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumTest.php @@ -2,7 +2,7 @@ /** * @file - * Tests for forum.module. + * Contains Drupal\forum\Tests\ForumTest */ namespace Drupal\forum\Tests; @@ -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 a7f4f88..ab1540b 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,7 +280,7 @@ 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') { + if ($instance['widget']['type'] == 'entity_reference_autocomplete_tags') { $tag_fields[] = $instance['field_name']; } } @@ -307,6 +307,7 @@ protected function build_filters(&$form, &$form_state) { '#size' => 30, '#maxlength' => 1024, '#field_name' => $tag_field_name, + // @todo: Replace? '#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..c47964c 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessBaseTableTest.php @@ -41,6 +41,42 @@ public function setUp() { node_access_rebuild(); state()->set('node_access_test.private', TRUE); + + // Create a vocabulary. + $vocabulary = entity_create('taxonomy_vocabulary', array( + 'name' => $this->randomName(), + 'description' => $this->randomName(), + 'vid' => drupal_strtolower($this->randomName()), + 'langcode' => LANGUAGE_NOT_SPECIFIED, + 'help' => '', + 'weight' => mt_rand(0, 10), + )); + $vocabulary->save(); + + // Create the public and private terms. + // @todo: We can no longer do $edit['field_tags[und]'] = 'private'; in a loop + // as the first time the term will be created, however the next time entity-reference + // expects the format to be $edit['field_tags[und]'] = 'private (1)'; (i.e. with the term ID). + // This should be resolved once we allow to hide the entity ID from the widget. + $this->public_term = entity_create('taxonomy_term', array( + 'name' => 'public', + 'description' => $this->randomName(), + // Use the first available text format. + 'format' => db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(), + 'vid' => $vocabulary->id(), + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $this->public_term->save(); + + $this->private_term = entity_create('taxonomy_term', array( + 'name' => 'private', + 'description' => $this->randomName(), + // Use the first available text format. + 'format' => db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(), + 'vid' => $vocabulary->id(), + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $this->private_term->save(); } /** @@ -68,6 +104,7 @@ function testNodeAccessBasic() { for ($i = 0; $i < $num_simple_users; $i++) { $simple_users[$i] = $this->drupalCreateUser(array('access content', 'create article content')); } + foreach ($simple_users as $this->webUser) { $this->drupalLogin($this->webUser); foreach (array(0 => 'Public', 1 => 'Private') as $is_private => $type) { @@ -75,17 +112,24 @@ function testNodeAccessBasic() { 'title' => t('@private_public Article created by @user', array('@private_public' => $type, '@user' => $this->webUser->name)), ); if ($is_private) { - $edit['private'] = TRUE; - $edit['body[und][0][value]'] = 'private node'; - $edit['field_tags[und]'] = 'private'; + $settings = array( + 'type' => 'article', + 'private' => TRUE, + ); + $settings['body'][LANGUAGE_NOT_SPECIFIED][0]['value'] = 'private node'; + $settings['field_tags'][LANGUAGE_NOT_SPECIFIED][0]['target_id'] = $this->private_term->id(); } else { - $edit['body[und][0][value]'] = 'public node'; - $edit['field_tags[und]'] = 'public'; + $settings = array( + 'type' => 'article', + 'private' => FALSE, + ); + $settings['body'][LANGUAGE_NOT_SPECIFIED][0]['value'] = 'public node'; + $settings['field_tags'][LANGUAGE_NOT_SPECIFIED][0]['target_id'] = $this->public_term->id(); } - $this->drupalPost('node/add/article', $edit, t('Save')); - $nid = db_query('SELECT nid FROM {node} WHERE title = :title', array(':title' => $edit['title']))->fetchField(); + $node = $this->drupalCreateNode($settings); + $nid = $node->id(); $private_status = db_query('SELECT private FROM {node_access_test} where nid = :nid', array(':nid' => $nid))->fetchField(); $this->assertTrue($is_private == $private_status, 'The private status of the node was properly set in the node_access_test table.'); if ($is_private) { diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php index de24f45..b5b4505 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php @@ -87,7 +87,7 @@ public function testForumPager() { 'type' => 'forum', 'taxonomy_forums' => array( LANGUAGE_NOT_SPECIFIED => array( - array('tid' => $tid, 'vid' => $vid), + array('target_id' => $tid, 'vid' => $vid), ), ), )); diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index 255fc97..d1cd377 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -781,22 +781,38 @@ 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['taxonomy_term']. - if (isset($item['taxonomy_term'])) { - $term = $item['taxonomy_term']; - 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/rest/lib/Drupal/rest/Tests/ReadTest.php b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php index 0578fc3..0c1619b 100644 --- a/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php +++ b/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\rest\test\ReadTest. + * Contains Drupal\rest\Tests\ReadTest. */ namespace Drupal\rest\Tests; 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 8d5fa81..055d319 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryRelationshipTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Core\Entity\Tests\EntityQueryRelationshipTest. + * Contains Drupal\system\Tests\Entity\EntityQueryRelationshipTest. */ namespace Drupal\system\Tests\Entity; @@ -19,7 +19,7 @@ class EntityQueryRelationshipTest extends WebTestBase { * * @var array */ - public static $modules = array('entity_test', 'taxonomy'); + public static $modules = array('entity_test', 'taxonomy', 'entity_reference'); /** * @var \Drupal\field_sql_storage\Entity\QueryFactory @@ -81,16 +81,29 @@ protected 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++) { @@ -152,7 +165,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/Module/DependencyTest.php b/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php index 1c5527b..b20ff72 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Module/DependencyTest.php @@ -148,6 +148,7 @@ function testModuleEnableOrder() { $this->assertModules(array('forum'), FALSE); $this->assertText(t('You must enable the History, Taxonomy, Options, 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][taxonomy][enable]'] = 'taxonomy'; $edit['modules[Core][comment][enable]'] = 'comment'; 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 27f3e6a..5ba606b 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(LANGUAGE_NOT_SPECIFIED => array(array('tid' => $this->term->tid))), + 'field_tags' => array(LANGUAGE_NOT_SPECIFIED => 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 0d5dfd4..0000000 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/widget/TaxonomyAutocompleteWidget.php +++ /dev/null @@ -1,107 +0,0 @@ - 'textfield', - '#title' => t('Placeholder'), - '#default_value' => $this->getSetting('placeholder'), - '#description' => t('The placeholder is a short hint (a word or short phrase) intended to aid the user with data entry. A hint could be 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. - $terms = 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); - } - else { - $vocabulary = reset($vocabularies); - $term = array( - 'tid' => 'autocreate', - 'vid' => $vocabulary->id(), - 'name' => $value, - ); - } - $terms[] = (array)$term; - } - - return $terms; - } - -} 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 de848da..532292a 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/RssTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/RssTest.php index 3d28ec0..a99b011 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'); + public static $modules = array('node', 'field_ui', '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/TaxonomyTestBase.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTestBase.php index fab42f0..688add8 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('taxonomy', 'entity_reference'); 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..daf9d63 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php @@ -17,7 +17,7 @@ class TermFieldMultipleVocabularyTest extends TaxonomyTestBase { * * @var array */ - public static $modules = array('field_test'); + public static $modules = array('field_test', 'entity_reference'); protected $instance; protected $vocabulary1; @@ -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 index d5de5fa..4101eb1 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php @@ -19,7 +19,7 @@ class TermFieldTest extends TaxonomyTestBase { * * @var array */ - public static $modules = array('field_test'); + public static $modules = array('field_test', 'entity_reference'); protected $instance; protected $vocabulary; @@ -43,15 +43,10 @@ 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( @@ -61,11 +56,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('test_entity', 'test_bundle', 'full') ->setComponent($this->field_name, array( - 'type' => 'taxonomy_term_reference_link', + 'type' => 'entity_reference_label', )) ->save(); } @@ -89,7 +93,7 @@ function testTaxonomyTermFieldValidation() { $entity = field_test_create_entity(); $bad_term = $this->createTerm($this->createVocabulary()); - $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid; + $entity->{$this->field_name}[$langcode][0]['target_id'] = $bad_term->tid; try { field_attach_validate($entity); $this->fail('Wrong term causes validation error.'); @@ -139,33 +143,24 @@ function testTaxonomyTermFieldWidgets() { * 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 + // Add several entries in the 'target_bundles' 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', - ), + $this->instance['settings']['handler_settings']['target_bundles'] = array( + $this->vocabulary->id(), + $this->vocabulary->id(), + 'foo', ); - field_update_field($this->field); + field_update_instance($this->instance); // 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.'); + $instance = field_info_instance('test_entity', $this->field_name, 'test_bundle'); + $target_bundles = $instance['settings']['handler_settings']['target_bundles']; + $this->assertEqual($target_bundles[0], $new_name, 'Index 0: Machine name was updated correctly.'); + $this->assertEqual($target_bundles[1], $new_name, 'Index 1: Machine name was updated correctly.'); + $this->assertEqual($target_bundles[2], '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..a447950 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.', ), @@ -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 . ']'] = $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 ec91070..b76754e 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/Views/TaxonomyTestBase.php @@ -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 index 7f7ab02..d2c404f 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\Field\FieldItemBase; /** - * Defines the 'taxonomy_term_reference' entity field item. + * Defines the 'entity_reference' entity field item. */ class TaxonomyTermReferenceItem extends FieldItemBase { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php index b14fc2f..ad4c10e 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\taxonomy\VocabularyStorageController. + * Contains Drupal\taxonomy\VocabularyStorageController. */ namespace Drupal\taxonomy; @@ -25,18 +25,31 @@ protected function postSave(EntityInterface $entity, $update) { 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; + foreach (field_info_instances() as $entity_type => $bundles) { + foreach ($bundles as $bundle => $field_names) { + foreach ($field_names as $field_name => $instance) { + $field = field_info_field($field_name); + if ($field['type'] != 'entity_reference' || $field['settings']['target_type'] != 'taxonomy_term') { + // Not an entity reference field, referencing taxonomy term. + continue; + } + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + // Instance is referencing all bundles. + continue; + } + + + + $update_field = FALSE; + foreach ($instance['settings']['handler_settings']['target_bundles'] as $delta=> $bundle) { + if ($bundle == $entity->getOriginalID()) { + $instance['settings']['handler_settings']['target_bundles'][$delta] = $entity->id(); + $update_field = TRUE; + } + } + if ($update_field) { + field_update_instance($instance); } - } - if ($update_field) { - field_update_field($field); } } } @@ -67,29 +80,42 @@ protected function postDelete($entities) { foreach ($entities as $vocabulary) { $vocabularies[$vocabulary->id()] = $vocabulary->id(); } - // Load all Taxonomy module fields and delete those which use only this + // Iterate over all entity reference fields referencing taxonomy terms 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 (field_info_instances() as $entity_type => $bundles) { + foreach ($bundles as $bundle => $field_names) { + foreach ($field_names as $field_name => $instance) { + $field = field_info_field($field_name); + if ($field['type'] != 'entity_reference' || $field['settings']['target_type'] != 'taxonomy_term') { + // Not an entity reference field, referencing taxonomy term. + continue; + } + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + // Instance is referencing all bundles. + continue; + } + + $modified_field = FALSE; + + foreach ($instance['settings']['handler_settings']['target_bundles'] as $delta => $bundle) { + if (isset($vocabularies[$bundle])) { + unset($instance['settings']['handler_settings']['target_bundles'][$delta]); + $modified_field = TRUE; + } + } + if ($modified_field) { + if (empty($instance['settings']['handler_settings']['target_bundles'])) { + field_delete_instance($instance); + } + else { + // Update the instance definition with the new target bundles. + field_update_instance($instance); + } + } } } } + // Reset caches. $this->resetCache(array_keys($vocabularies)); } diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 16d1019..9414989 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) { @@ -321,14 +313,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', @@ -984,273 +968,13 @@ 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. + * @todo: How do we maintain this in entity reference? * - 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) { - if (!is_array($item) || (empty($item['tid']) && (string) $item['tid'] !== '0')) { - return TRUE; - } - return FALSE; -} - -/** - * 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['taxonomy_term'] 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['taxonomy_term']; - $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['taxonomy_term']->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['taxonomy_term']->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') { - $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]['taxonomy_term'] = $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]); - } - } - } } /** @@ -1267,52 +991,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 @@ -1376,23 +1054,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'] == 'autocreate') { - unset($item['tid']); - $term = entity_create('taxonomy_term', $item); - $term->langcode = $langcode; - taxonomy_term_save($term); - $items[$delta]['tid'] = $term->tid; - } - } -} - -/** * Implements hook_node_insert(). */ function taxonomy_node_insert(Node $node) { @@ -1432,7 +1093,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})) { @@ -1447,7 +1108,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 fbd29f1..cf77a20 100644 --- a/core/modules/taxonomy/taxonomy.pages.inc +++ b/core/modules/taxonomy/taxonomy.pages.inc @@ -78,86 +78,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('translatable'); - $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 d683d68..000e680 100644 --- a/core/modules/taxonomy/taxonomy.views.inc +++ b/core/modules/taxonomy/taxonomy.views.inc @@ -330,7 +330,7 @@ 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 + * Views integration for entity_reference fields. Adds a term relationship to the default * field data. * * @see field_views_field_default_views_data() diff --git a/core/modules/views/includes/ajax.inc b/core/modules/views/includes/ajax.inc index 73c3d02..fb3bed3 100644 --- a/core/modules/views/includes/ajax.inc +++ b/core/modules/views/includes/ajax.inc @@ -284,7 +284,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 entity_reference_autocomplete_tags() */ 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 7e8fc93..385ee92 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', 'taxonomy', 'block', 'entity_reference'); /** * An array of argument arrays to use for default views. @@ -59,15 +59,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( @@ -77,11 +72,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(); @@ -93,7 +97,7 @@ protected function setUp() { $term = $this->createTerm($this->vocabulary); $values = array('created' => $time, 'type' => 'page'); - $values[$this->field_name][LANGUAGE_NOT_SPECIFIED][]['tid'] = $term->tid; + $values[$this->field_name][LANGUAGE_NOT_SPECIFIED][]['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..7dea069 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('taxonomy', 'entity_reference'); 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(); diff --git a/core/profiles/standard/standard.info b/core/profiles/standard/standard.info index 2773abe..05db08e 100644 --- a/core/profiles/standard/standard.info +++ b/core/profiles/standard/standard.info @@ -13,6 +13,7 @@ dependencies[] = contextual dependencies[] = contact dependencies[] = custom_block dependencies[] = edit +dependencies[] = entity_reference dependencies[] = help dependencies[] = image dependencies[] = menu diff --git a/core/profiles/standard/standard.install b/core/profiles/standard/standard.install index 8802bbc..f58f578 100644 --- a/core/profiles/standard/standard.install +++ b/core/profiles/standard/standard.install @@ -105,16 +105,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); @@ -126,22 +121,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/template.php b/core/themes/bartik/template.php index 9a6c8a9..39e06aa 100644 --- a/core/themes/bartik/template.php +++ b/core/themes/bartik/template.php @@ -130,7 +130,7 @@ function bartik_menu_tree($variables) { /** * Implements theme_field__field_type(). */ -function bartik_field__taxonomy_term_reference($variables) { +function bartik_field__entity_reference($variables) { $output = ''; // Render the label, if it's not hidden.