Issue #2635224 by dawehner, bojanz: ContentEntityBase should provide field definitions for key fields

8.1.x
Nathaniel Catchpole 2016-02-15 09:27:14 +09:00
parent d5f4e3b8ff
commit 7e4b50e68f
13 changed files with 82 additions and 79 deletions

View File

@ -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}
*/

View File

@ -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(),

View File

@ -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());

View File

@ -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'))

View File

@ -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'))

View File

@ -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",

View File

@ -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",
* }
* )
*/

View File

@ -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",
* }
* )
*/

View File

@ -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;
}
}

View File

@ -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",
* }
* )
*/

View File

@ -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 {
}
}
}

View File

@ -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');

View File

@ -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');