Commit 3fcf0794 authored by effulgentsia's avatar effulgentsia

Issue #2936511 by hchonov, Berdir, plach, Wim Leers, catch, amateescu,...

Issue #2936511 by hchonov, Berdir, plach, Wim Leers, catch, amateescu, sdewitt: ContentEntityType::getRevisionMetadataKeys() never considers the BC logic because of the new "revision_default" key
parent ef5b216c
......@@ -14,6 +14,18 @@ class ContentEntityType extends EntityType implements ContentEntityTypeInterface
*/
protected $revision_metadata_keys = [];
/**
* The required revision metadata keys.
*
* This property should only be filled in the constructor. This ensures that
* only new instances get newly added required revision metadata keys.
* Unserialized objects will only retrieve the keys that they already have
* been cached with.
*
* @var array
*/
protected $requiredRevisionMetadataKeys = [];
/**
* {@inheritdoc}
*/
......@@ -25,9 +37,23 @@ public function __construct($definition) {
'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
];
$this->revision_metadata_keys += [
'revision_default' => 'revision_default',
];
// Only new instances should provide the required revision metadata keys.
// The cached instances should return only what already has been stored
// under the property $revision_metadata_keys. The BC layer in
// ::getRevisionMetadataKeys() has to detect if the revision metadata keys
// have been provided by the entity type annotation, therefore we add keys
// to the property $requiredRevisionMetadataKeys only if those keys aren't
// set in the entity type annotation.
if (!isset($this->revision_metadata_keys['revision_default'])) {
$this->requiredRevisionMetadataKeys['revision_default'] = 'revision_default';
}
// Add the required revision metadata fields here instead in the getter
// method, so that they are serialized as part of the object even if the
// getter method doesn't get called. This allows the list to be further
// extended. Only new instances of the class will contain the new list,
// while the cached instances contain the previous version of the list.
$this->revision_metadata_keys += $this->requiredRevisionMetadataKeys;
}
/**
......@@ -59,7 +85,7 @@ protected function checkStorageClass($class) {
public function getRevisionMetadataKeys($include_backwards_compatibility_field_names = TRUE) {
// Provide backwards compatibility in case the revision metadata keys are
// not defined in the entity annotation.
if (!$this->revision_metadata_keys && $include_backwards_compatibility_field_names) {
if ((!$this->revision_metadata_keys || ($this->revision_metadata_keys == $this->requiredRevisionMetadataKeys)) && $include_backwards_compatibility_field_names) {
$base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->id());
if ((isset($base_fields['revision_uid']) && $revision_user = 'revision_uid') || (isset($base_fields['revision_user']) && $revision_user = 'revision_user')) {
@trigger_error('The revision_user revision metadata key is not set.', E_USER_DEPRECATED);
......
......@@ -224,7 +224,9 @@ protected function buildBaseFieldDefinitions($entity_type_id) {
// Make sure that revisionable entity types are correctly defined.
if ($entity_type->isRevisionable()) {
$field_name = $entity_type->getRevisionMetadataKey('revision_default');
// Disable the BC layer to prevent a recursion, this only needs the
// revision_default key that is always set.
$field_name = $entity_type->getRevisionMetadataKeys(FALSE)['revision_default'];
$base_field_definitions[$field_name] = BaseFieldDefinition::create('boolean')
->setLabel($this->t('Default revision'))
->setDescription($this->t('A flag indicating whether this was a default revision when it was saved.'))
......
......@@ -2092,9 +2092,23 @@ function system_update_8501() {
// also to code using the latest installed definition.
$installed_entity_type = $definition_update_manager->getEntityType($entity_type_id);
$revision_metadata_keys = $installed_entity_type->get('revision_metadata_keys');
$revision_metadata_keys['revision_default'] = $field_name;
$installed_entity_type->set('revision_metadata_keys', $revision_metadata_keys);
$definition_update_manager->updateEntityType($installed_entity_type);
if (!isset($revision_metadata_keys['revision_default'])) {
// Update the property holding the required revision metadata keys,
// which is used by the BC layer for retrieving the revision metadata
// keys.
// @see \Drupal\Core\Entity\ContentEntityType::getRevisionMetadataKeys().
$required_revision_metadata_keys = $installed_entity_type->get('requiredRevisionMetadataKeys');
$required_revision_metadata_keys['revision_default'] = $field_name;
$installed_entity_type->set('requiredRevisionMetadataKeys', $required_revision_metadata_keys);
// Update the revision metadata keys to add the new required revision
// metadata key "revision_default".
$revision_metadata_keys['revision_default'] = $required_revision_metadata_keys['revision_default'];
$installed_entity_type->set('revision_metadata_keys', $revision_metadata_keys);
$definition_update_manager->updateEntityType($installed_entity_type);
}
$storage_definition = BaseFieldDefinition::create('boolean')
->setLabel(t('Default revision'))
......@@ -2110,5 +2124,14 @@ function system_update_8501() {
$definition_update_manager
->installFieldStorageDefinition($field_name, $entity_type_id, $entity_type_id, $storage_definition);
}
else {
$variables = ['@entity_type_label' => $entity_type->getLabel()];
if ($field_name === 'revision_default') {
\Drupal::logger('system')->error('An existing "Default revision" field was found for the @entity_type_label entity type, but no "revision_default" revision metadata key was found in its definition.', $variables);
}
else {
\Drupal::logger('system')->error('An existing "Default revision" field was found for the @entity_type_label entity type.', $variables);
}
}
}
}
......@@ -5,6 +5,9 @@
/**
* Defines the test entity class.
*
* This entity type does not define revision_metadata_keys on purpose to test
* the BC layer.
*
* @ContentEntityType(
* id = "entity_test_mul_revlog",
* label = @Translation("Test entity - data table, revisions log"),
......@@ -21,11 +24,6 @@
* "label" = "name",
* "langcode" = "langcode",
* },
* revision_metadata_keys = {
* "revision_user" = "revision_user",
* "revision_created" = "revision_created",
* "revision_log_message" = "revision_log_message"
* },
* )
*/
class EntityTestMulWithRevisionLog extends EntityTestWithRevisionLog {
......
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