Issue #2635224 by dawehner, bojanz: ContentEntityBase should provide field definitions for key fields
parent
d5f4e3b8ff
commit
7e4b50e68f
|
@ -9,9 +9,11 @@ namespace Drupal\Core\Entity;
|
||||||
|
|
||||||
use Drupal\Component\Utility\SafeMarkup;
|
use Drupal\Component\Utility\SafeMarkup;
|
||||||
use Drupal\Core\Entity\Plugin\DataType\EntityReference;
|
use Drupal\Core\Entity\Plugin\DataType\EntityReference;
|
||||||
|
use Drupal\Core\Field\BaseFieldDefinition;
|
||||||
use Drupal\Core\Language\Language;
|
use Drupal\Core\Language\Language;
|
||||||
use Drupal\Core\Language\LanguageInterface;
|
use Drupal\Core\Language\LanguageInterface;
|
||||||
use Drupal\Core\Session\AccountInterface;
|
use Drupal\Core\Session\AccountInterface;
|
||||||
|
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||||
use Drupal\Core\TypedData\TypedDataInterface;
|
use Drupal\Core\TypedData\TypedDataInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1114,6 +1116,64 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
|
||||||
return $value;
|
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}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -81,7 +81,7 @@ class EntityFormDisplayTest extends KernelTestBase {
|
||||||
$default_widget = $field_type_info['default_widget'];
|
$default_widget = $field_type_info['default_widget'];
|
||||||
$widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
|
$widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'weight' => 0,
|
'weight' => 3,
|
||||||
'type' => $default_widget,
|
'type' => $default_widget,
|
||||||
'settings' => $widget_settings,
|
'settings' => $widget_settings,
|
||||||
'third_party_settings' => array(),
|
'third_party_settings' => array(),
|
||||||
|
|
|
@ -46,17 +46,17 @@ class LocaleTranslatedSchemaDefinitionTest extends WebTestBase {
|
||||||
$stringStorage = \Drupal::service('locale.storage');
|
$stringStorage = \Drupal::service('locale.storage');
|
||||||
|
|
||||||
$source = $stringStorage->createString(array(
|
$source = $stringStorage->createString(array(
|
||||||
'source' => 'The node ID.',
|
'source' => 'Revision ID',
|
||||||
))->save();
|
))->save();
|
||||||
|
|
||||||
$stringStorage->createTranslation(array(
|
$stringStorage->createTranslation(array(
|
||||||
'lid' => $source->lid,
|
'lid' => $source->lid,
|
||||||
'language' => 'fr',
|
'language' => 'fr',
|
||||||
'translation' => 'Translated node ID',
|
'translation' => 'Translated Revision ID',
|
||||||
))->save();
|
))->save();
|
||||||
|
|
||||||
// Ensure that the field is translated when access through the API.
|
// 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.
|
// Assert there are no updates.
|
||||||
$this->assertFalse(\Drupal::service('entity.definition_update_manager')->needsUpdates());
|
$this->assertFalse(\Drupal::service('entity.definition_update_manager')->needsUpdates());
|
||||||
|
|
|
@ -325,41 +325,7 @@ class Node extends ContentEntityBase implements NodeInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||||
$fields['nid'] = BaseFieldDefinition::create('integer')
|
$fields = parent::baseFieldDefinitions($entity_type);
|
||||||
->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['title'] = BaseFieldDefinition::create('string')
|
$fields['title'] = BaseFieldDefinition::create('string')
|
||||||
->setLabel(t('Title'))
|
->setLabel(t('Title'))
|
||||||
|
|
|
@ -69,21 +69,7 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||||
$fields['id'] = BaseFieldDefinition::create('integer')
|
$fields = parent::baseFieldDefinitions($entity_type);
|
||||||
->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['name'] = BaseFieldDefinition::create('string')
|
$fields['name'] = BaseFieldDefinition::create('string')
|
||||||
->setLabel(t('Name'))
|
->setLabel(t('Name'))
|
||||||
|
@ -100,12 +86,6 @@ class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
|
||||||
'weight' => -5,
|
'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')
|
$fields['created'] = BaseFieldDefinition::create('created')
|
||||||
->setLabel(t('Authored on'))
|
->setLabel(t('Authored on'))
|
||||||
->setDescription(t('Time the entity was created'))
|
->setDescription(t('Time the entity was created'))
|
||||||
|
|
|
@ -33,7 +33,8 @@ use Drupal\entity_test\FieldStorageDefinition;
|
||||||
* "id" = "id",
|
* "id" = "id",
|
||||||
* "label" = "name",
|
* "label" = "name",
|
||||||
* "uuid" = "uuid",
|
* "uuid" = "uuid",
|
||||||
* "bundle" = "type"
|
* "bundle" = "type",
|
||||||
|
* "langcode" = "langcode",
|
||||||
* },
|
* },
|
||||||
* links = {
|
* links = {
|
||||||
* "canonical" = "/entity_test_base_field_display/{entity_test_base_field_display}/edit",
|
* "canonical" = "/entity_test_base_field_display/{entity_test_base_field_display}/edit",
|
||||||
|
|
|
@ -20,9 +20,11 @@ namespace Drupal\entity_test\Entity;
|
||||||
* base_table = "entity_test",
|
* base_table = "entity_test",
|
||||||
* render_cache = FALSE,
|
* render_cache = FALSE,
|
||||||
* entity_keys = {
|
* entity_keys = {
|
||||||
|
* "uuid" = "uuid",
|
||||||
* "id" = "id",
|
* "id" = "id",
|
||||||
* "label" = "name",
|
* "label" = "name",
|
||||||
* "bundle" = "type"
|
* "bundle" = "type",
|
||||||
|
* "langcode" = "langcode",
|
||||||
* }
|
* }
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -18,7 +18,9 @@ namespace Drupal\entity_test\Entity;
|
||||||
* label_callback = "entity_test_label_callback",
|
* label_callback = "entity_test_label_callback",
|
||||||
* entity_keys = {
|
* entity_keys = {
|
||||||
* "id" = "id",
|
* "id" = "id",
|
||||||
* "bundle" = "type"
|
* "bundle" = "type",
|
||||||
|
* "uuid" = "uuid",
|
||||||
|
* "langcode" = "langcode",
|
||||||
* }
|
* }
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -50,14 +50,4 @@ use Drupal\Core\Entity\EntityTypeInterface;
|
||||||
*/
|
*/
|
||||||
class EntityTestMulLangcodeKey extends EntityTest {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,8 +21,10 @@ namespace Drupal\entity_test\Entity;
|
||||||
* render_cache = FALSE,
|
* render_cache = FALSE,
|
||||||
* entity_keys = {
|
* entity_keys = {
|
||||||
* "id" = "id",
|
* "id" = "id",
|
||||||
|
* "uuid" = "uuid",
|
||||||
* "label" = "name",
|
* "label" = "name",
|
||||||
* "bundle" = "type"
|
* "bundle" = "type",
|
||||||
|
* "langcode" = "langcode",
|
||||||
* }
|
* }
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -94,6 +94,7 @@ class EntityViewsDataTest extends UnitTestCase {
|
||||||
'id' => 'entity_test',
|
'id' => 'entity_test',
|
||||||
'label' => 'Entity test',
|
'label' => 'Entity test',
|
||||||
'entity_keys' => [
|
'entity_keys' => [
|
||||||
|
'uuid' => 'uuid',
|
||||||
'id' => 'id',
|
'id' => 'id',
|
||||||
'langcode' => 'langcode',
|
'langcode' => 'langcode',
|
||||||
'bundle' => 'type',
|
'bundle' => 'type',
|
||||||
|
@ -991,4 +992,3 @@ namespace Drupal\entity_test\Entity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ class FilterUITest extends ViewTestBase {
|
||||||
|
|
||||||
$this->drupalGet('admin/structure/views/view/test_filter_groups');
|
$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.
|
// Tests that we can create a new filter group from UI.
|
||||||
$this->drupalGet('admin/structure/views/nojs/rearrange-filter/test_filter_groups/page');
|
$this->drupalGet('admin/structure/views/nojs/rearrange-filter/test_filter_groups/page');
|
||||||
|
|
|
@ -245,9 +245,9 @@ class HandlerTest extends UITestBase {
|
||||||
$add_handler_url = 'admin/structure/views/nojs/add-handler/test_node_view/default/' . $handler_type;
|
$add_handler_url = 'admin/structure/views/nojs/add-handler/test_node_view/default/' . $handler_type;
|
||||||
$this->drupalGet($add_handler_url);
|
$this->drupalGet($add_handler_url);
|
||||||
|
|
||||||
$this->assertNoDuplicateField('Node ID', 'Content');
|
$this->assertNoDuplicateField('ID', 'Content');
|
||||||
$this->assertNoDuplicateField('Node ID', 'Content revision');
|
$this->assertNoDuplicateField('ID', 'Content revision');
|
||||||
$this->assertNoDuplicateField('Type', 'Content');
|
$this->assertNoDuplicateField('Content type', 'Content');
|
||||||
$this->assertNoDuplicateField('UUID', 'Content');
|
$this->assertNoDuplicateField('UUID', 'Content');
|
||||||
$this->assertNoDuplicateField('Revision ID', 'Content');
|
$this->assertNoDuplicateField('Revision ID', 'Content');
|
||||||
$this->assertNoDuplicateField('Revision ID', 'Content revision');
|
$this->assertNoDuplicateField('Revision ID', 'Content revision');
|
||||||
|
|
Loading…
Reference in New Issue