Commit 7e4b50e6 authored by catch's avatar catch

Issue #2635224 by dawehner, bojanz: ContentEntityBase should provide field...

Issue #2635224 by dawehner, bojanz: ContentEntityBase should provide field definitions for key fields
parent d5f4e3b8
......@@ -9,9 +9,11 @@
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 @@ protected function getEntityKey($key) {
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}
*/
......
......@@ -81,7 +81,7 @@ public function testFieldComponent() {
$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(),
......
......@@ -46,17 +46,17 @@ function testTranslatedSchemaDefinition() {
$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());
......
......@@ -325,41 +325,7 @@ public function setRevisionAuthorId($uid) {
* {@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'))
......
......@@ -69,21 +69,7 @@ public static function preCreate(EntityStorageInterface $storage, array &$values
* {@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 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
'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'))
......
......@@ -33,7 +33,8 @@
* "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",
......
......@@ -20,9 +20,11 @@
* base_table = "entity_test",
* render_cache = FALSE,
* entity_keys = {
* "uuid" = "uuid",
* "id" = "id",
* "label" = "name",
* "bundle" = "type"
* "bundle" = "type",
* "langcode" = "langcode",
* }
* )
*/
......
......@@ -18,7 +18,9 @@
* label_callback = "entity_test_label_callback",
* entity_keys = {
* "id" = "id",
* "bundle" = "type"
* "bundle" = "type",
* "uuid" = "uuid",
* "langcode" = "langcode",
* }
* )
*/
......
......@@ -50,14 +50,4 @@
*/
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 @@
* render_cache = FALSE,
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "label" = "name",
* "bundle" = "type"
* "bundle" = "type",
* "langcode" = "langcode",
* }
* )
*/
......
......@@ -94,6 +94,7 @@ protected function setUp() {
'id' => 'entity_test',
'label' => 'Entity test',
'entity_keys' => [
'uuid' => 'uuid',
'id' => 'id',
'langcode' => 'langcode',
'bundle' => 'type',
......@@ -991,4 +992,3 @@ function t($string, array $args = []) {
}
}
}
......@@ -72,7 +72,7 @@ public function testFiltersUI() {
$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');
......
......@@ -245,9 +245,9 @@ public function testNoDuplicateFields() {
$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');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment