diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 1e198fe014d..9e838141584 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -9,9 +9,11 @@ namespace Drupal\Core\Entity; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Entity\Plugin\DataType\EntityReference; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\TypedData\TypedDataInterface; /** @@ -1114,6 +1116,64 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C return $value; } + /** + * {@inheritdoc} + */ + public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { + $fields = []; + if ($entity_type->hasKey('id')) { + $fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer') + ->setLabel(new TranslatableMarkup('ID')) + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); + } + if ($entity_type->hasKey('uuid')) { + $fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('uuid') + ->setLabel(new TranslatableMarkup('UUID')) + ->setReadOnly(TRUE); + } + if ($entity_type->hasKey('revision')) { + $fields[$entity_type->getKey('revision')] = BaseFieldDefinition::create('integer') + ->setLabel(new TranslatableMarkup('Revision ID')) + ->setReadOnly(TRUE) + ->setSetting('unsigned', TRUE); + } + if ($entity_type->hasKey('langcode')) { + $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language') + ->setLabel(new TranslatableMarkup('Language')) + ->setDisplayOptions('view', [ + 'type' => 'hidden', + ]) + ->setDisplayOptions('form', [ + 'type' => 'language_select', + 'weight' => 2, + ]); + if ($entity_type->isRevisionable()) { + $fields[$entity_type->getKey('langcode')]->setRevisionable(TRUE); + } + if ($entity_type->isTranslatable()) { + $fields[$entity_type->getKey('langcode')]->setTranslatable(TRUE); + } + } + if ($entity_type->hasKey('bundle')) { + if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) { + $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('entity_reference') + ->setLabel($entity_type->getBundleLabel()) + ->setSetting('target_type', $bundle_entity_type_id) + ->setRequired(TRUE) + ->setReadOnly(TRUE); + } + else { + $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('string') + ->setLabel($entity_type->getBundleLabel()) + ->setRequired(TRUE) + ->setReadOnly(TRUE); + } + } + + return $fields; + } + /** * {@inheritdoc} */ diff --git a/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php b/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php index b002106cfb3..184021813cd 100644 --- a/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php +++ b/core/modules/field_ui/src/Tests/EntityFormDisplayTest.php @@ -81,7 +81,7 @@ class EntityFormDisplayTest extends KernelTestBase { $default_widget = $field_type_info['default_widget']; $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget); $expected = array( - 'weight' => 0, + 'weight' => 3, 'type' => $default_widget, 'settings' => $widget_settings, 'third_party_settings' => array(), diff --git a/core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php b/core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php index 5422a10df92..326cccd8fc9 100644 --- a/core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php +++ b/core/modules/locale/src/Tests/LocaleTranslatedSchemaDefinitionTest.php @@ -46,17 +46,17 @@ class LocaleTranslatedSchemaDefinitionTest extends WebTestBase { $stringStorage = \Drupal::service('locale.storage'); $source = $stringStorage->createString(array( - 'source' => 'The node ID.', + 'source' => 'Revision ID', ))->save(); $stringStorage->createTranslation(array( 'lid' => $source->lid, 'language' => 'fr', - 'translation' => 'Translated node ID', + 'translation' => 'Translated Revision ID', ))->save(); // Ensure that the field is translated when access through the API. - $this->assertEqual('Translated node ID', \Drupal::entityManager()->getBaseFieldDefinitions('node')['nid']->getDescription()); + $this->assertEqual('Translated Revision ID', \Drupal::entityManager()->getBaseFieldDefinitions('node')['vid']->getLabel()); // Assert there are no updates. $this->assertFalse(\Drupal::service('entity.definition_update_manager')->needsUpdates()); diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index cd9b4e51a5a..7b20a3b8210 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -325,41 +325,7 @@ class Node extends ContentEntityBase implements NodeInterface { * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields['nid'] = BaseFieldDefinition::create('integer') - ->setLabel(t('Node ID')) - ->setDescription(t('The node ID.')) - ->setReadOnly(TRUE) - ->setSetting('unsigned', TRUE); - - $fields['uuid'] = BaseFieldDefinition::create('uuid') - ->setLabel(t('UUID')) - ->setDescription(t('The node UUID.')) - ->setReadOnly(TRUE); - - $fields['vid'] = BaseFieldDefinition::create('integer') - ->setLabel(t('Revision ID')) - ->setDescription(t('The node revision ID.')) - ->setReadOnly(TRUE) - ->setSetting('unsigned', TRUE); - - $fields['type'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Type')) - ->setDescription(t('The node type.')) - ->setSetting('target_type', 'node_type') - ->setReadOnly(TRUE); - - $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language')) - ->setDescription(t('The node language code.')) - ->setTranslatable(TRUE) - ->setRevisionable(TRUE) - ->setDisplayOptions('view', array( - 'type' => 'hidden', - )) - ->setDisplayOptions('form', array( - 'type' => 'language_select', - 'weight' => 2, - )); + $fields = parent::baseFieldDefinitions($entity_type); $fields['title'] = BaseFieldDefinition::create('string') ->setLabel(t('Title')) diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php index 1197c2cd34a..dc46e902558 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php @@ -69,21 +69,7 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface { * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields['id'] = BaseFieldDefinition::create('integer') - ->setLabel(t('ID')) - ->setDescription(t('The ID of the test entity.')) - ->setReadOnly(TRUE) - ->setSetting('unsigned', TRUE); - - $fields['uuid'] = BaseFieldDefinition::create('uuid') - ->setLabel(t('UUID')) - ->setDescription(t('The UUID of the test entity.')) - ->setReadOnly(TRUE); - - $fields['langcode'] = BaseFieldDefinition::create('language') - ->setLabel(t('Language code')) - ->setDescription(t('The language code of the test entity.')) - ->setTranslatable(TRUE); + $fields = parent::baseFieldDefinitions($entity_type); $fields['name'] = BaseFieldDefinition::create('string') ->setLabel(t('Name')) @@ -100,12 +86,6 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface { 'weight' => -5, )); - // @todo: Add allowed values validation. - $fields['type'] = BaseFieldDefinition::create('string') - ->setLabel(t('Type')) - ->setDescription(t('The bundle of the test entity.')) - ->setRequired(TRUE); - $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Authored on')) ->setDescription(t('Time the entity was created')) diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php index d2f83780a4a..8a3d90f3786 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php @@ -33,7 +33,8 @@ use Drupal\entity_test\FieldStorageDefinition; * "id" = "id", * "label" = "name", * "uuid" = "uuid", - * "bundle" = "type" + * "bundle" = "type", + * "langcode" = "langcode", * }, * links = { * "canonical" = "/entity_test_base_field_display/{entity_test_base_field_display}/edit", diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabel.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabel.php index 1ff02640fe8..6d9506610bd 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabel.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabel.php @@ -20,9 +20,11 @@ namespace Drupal\entity_test\Entity; * base_table = "entity_test", * render_cache = FALSE, * entity_keys = { + * "uuid" = "uuid", * "id" = "id", * "label" = "name", - * "bundle" = "type" + * "bundle" = "type", + * "langcode" = "langcode", * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabelCallback.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabelCallback.php index 1088114bab2..632ae0cf5a1 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabelCallback.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestLabelCallback.php @@ -18,7 +18,9 @@ namespace Drupal\entity_test\Entity; * label_callback = "entity_test_label_callback", * entity_keys = { * "id" = "id", - * "bundle" = "type" + * "bundle" = "type", + * "uuid" = "uuid", + * "langcode" = "langcode", * } * ) */ diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php index 3f794e5b0b1..b2eed9f4954 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php @@ -50,14 +50,4 @@ use Drupal\Core\Entity\EntityTypeInterface; */ class EntityTestMulLangcodeKey extends EntityTest { - /** - * {@inheritdoc} - */ - public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { - $fields = parent::baseFieldDefinitions($entity_type); - $fields['custom_langcode_key'] = $fields['langcode']; - unset($fields['langcode']); - return $fields; - } - } diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestViewBuilder.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestViewBuilder.php index bc83cebae77..7381587d36e 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestViewBuilder.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestViewBuilder.php @@ -21,8 +21,10 @@ namespace Drupal\entity_test\Entity; * render_cache = FALSE, * entity_keys = { * "id" = "id", + * "uuid" = "uuid", * "label" = "name", - * "bundle" = "type" + * "bundle" = "type", + * "langcode" = "langcode", * } * ) */ diff --git a/core/modules/views/tests/src/Unit/EntityViewsDataTest.php b/core/modules/views/tests/src/Unit/EntityViewsDataTest.php index 67771b5c8d3..e6d8f9f01aa 100644 --- a/core/modules/views/tests/src/Unit/EntityViewsDataTest.php +++ b/core/modules/views/tests/src/Unit/EntityViewsDataTest.php @@ -94,6 +94,7 @@ class EntityViewsDataTest extends UnitTestCase { 'id' => 'entity_test', 'label' => 'Entity test', 'entity_keys' => [ + 'uuid' => 'uuid', 'id' => 'id', 'langcode' => 'langcode', 'bundle' => 'type', @@ -991,4 +992,3 @@ namespace Drupal\entity_test\Entity { } } } - diff --git a/core/modules/views_ui/src/Tests/FilterUITest.php b/core/modules/views_ui/src/Tests/FilterUITest.php index 8745ea33342..6ed031beffa 100644 --- a/core/modules/views_ui/src/Tests/FilterUITest.php +++ b/core/modules/views_ui/src/Tests/FilterUITest.php @@ -72,7 +72,7 @@ class FilterUITest extends ViewTestBase { $this->drupalGet('admin/structure/views/view/test_filter_groups'); - $this->assertLink('Content: Node ID (= 1)', 0, 'Content: Node ID (= 1) link appears correctly.'); + $this->assertLink('Content: ID (= 1)', 0, 'Content: ID (= 1) link appears correctly.'); // Tests that we can create a new filter group from UI. $this->drupalGet('admin/structure/views/nojs/rearrange-filter/test_filter_groups/page'); diff --git a/core/modules/views_ui/src/Tests/HandlerTest.php b/core/modules/views_ui/src/Tests/HandlerTest.php index 0394f10f11e..bc3e018d8ea 100644 --- a/core/modules/views_ui/src/Tests/HandlerTest.php +++ b/core/modules/views_ui/src/Tests/HandlerTest.php @@ -245,9 +245,9 @@ class HandlerTest extends UITestBase { $add_handler_url = 'admin/structure/views/nojs/add-handler/test_node_view/default/' . $handler_type; $this->drupalGet($add_handler_url); - $this->assertNoDuplicateField('Node ID', 'Content'); - $this->assertNoDuplicateField('Node ID', 'Content revision'); - $this->assertNoDuplicateField('Type', 'Content'); + $this->assertNoDuplicateField('ID', 'Content'); + $this->assertNoDuplicateField('ID', 'Content revision'); + $this->assertNoDuplicateField('Content type', 'Content'); $this->assertNoDuplicateField('UUID', 'Content'); $this->assertNoDuplicateField('Revision ID', 'Content'); $this->assertNoDuplicateField('Revision ID', 'Content revision');