Updated: Comment #0

Problem/Motivation

All entity types that implement EntityChangedInterface have implemented the method getChangedTime() in the exact same way. This duplication could be avoided by using a trait.

Proposed resolution

Provide an EntityChangedTrait that provides EntityChangedTrait::getChangedTime().

Because this method relies on the changed field of an entity, it makes sense for the trait to provide that field as well. Since there can only be one baseFieldDefinitions(), however, we cannot provide that directly in the trait. For this reason a private function changedFieldDefinitions() is introduced as part of the trait that entity types can call in their baseFieldDefinitions().

Remaining tasks

User interface changes

-

API changes

-

Comments

tstoeckler’s picture

StatusFileSize
new10.27 KB

Here we go.

tstoeckler’s picture

Status: Active » Needs review
berdir’s picture

Hm, the field definitions thing does affect the order in which the field is defined, we also lose the context-specific description...

tstoeckler’s picture

StatusFileSize
new15.62 KB
new148.07 KB

That's true. Reverted the order to how it was previously. I updated the description to include the entity type. It's now pretty much identical to how it was before except for 'edited' vs. 'changed', which I changed (no pun intended) because the latter is actually more correct.

I slightly expanded the scope of this issue to rename $entity_type to $entity_type_id in baseFieldDefinitions(). It seemed wrong to introduce this incorrectly as $entity_type in changedFieldDefinitions(), and I also didn't want to introduce a further inconsistency. I can revert that, if people are worried about kitten safety.

Status: Needs review » Needs work

The last submitted patch, 4: 2209971-4-entity-changed-trait.patch, failed testing.

berdir’s picture

People are not because your issue is now going to conflict on every instance that you changed anyway because HEAD is now using EntityTypeInterface $entity_type as argument for that method ;)

The same will happen once the changed field type is commited, might be easier to wait on that with further re-rolls? :)

tstoeckler’s picture

Right I definitely want to wait on the changed issue, but I wanted to post this to get some feedback if people agree with this in principle first.

Will re-roll.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new10.48 KB

Here's a re-roll.

Status: Needs review » Needs work

The last submitted patch, 8: 220991-8-entity-changed-trait.patch, failed testing.

dawehner’s picture

+++ b/core/lib/Drupal/Core/Entity/EntityChangedTrait.php
@@ -0,0 +1,55 @@
+  public function getChangedTime() {
+    return $this->get('changed')->value;
...
+    $fields['changed'] = FieldDefinition::create('integer')

There might be a couple of entities which uses timestamp instead. Could we just pull 'changed' from a property?

berdir’s picture

See #2182239: Improve ContentEntityBase::id() for better DX. We could just add a changed entity key. But not sure about adding too many of those.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new1.93 KB
new10.24 KB

Hmm... not sure. I'm not so keen on entity keys in general, but it sort of does seem it would be consistent here.

Anyway, here's a re-roll, which should pass. And this implements #10 for now. Thoughts?

Status: Needs review » Needs work

The last submitted patch, 12: 2209971-12-entity-changed-trait.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new1.24 KB
new10.25 KB

Well, that was not particularly smart... :-)

Status: Needs review » Needs work

The last submitted patch, 14: 2209971-14-entity-changed-trait.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new525 bytes
new10.25 KB

Oh lord, this is so embarassing...

tstoeckler’s picture

StatusFileSize
new9.66 KB

This needed a re-roll after the changed field type.

Here we go.

tstoeckler’s picture

Oh, forgot to mention this: I changed the implementation to not return an array of field definitions but return the single field definition directly. This made for a more fluent API when setting additional stuff on the field definition (i.e. isRevisionable() or isTranslatable()) like CustomBlock or Node do. This can be seen in the patch context.

Status: Needs review » Needs work

The last submitted patch, 17: 2209971-16-entity-changed-trait.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tstoeckler’s picture

Title: Consider adding a trait for EntityChangedInterface » Automatically provide a changed field definition

So #2506213: Update content entity changed timestamp on UI save already "fixed" this, however in a problematic way, because it hardcoded the name of the changed field to 'changed'. However, maybe after #2635224: ContentEntityBase should provide field definitions for key fields we can use this to auto-generate the changed field and fix the hardcoding.

tstoeckler’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs work » Needs review
StatusFileSize
new9.1 KB

Let's see.

Status: Needs review » Needs work

The last submitted patch, 24: 2209971-24.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new9.1 KB

Hmm.. should apply to 8.3.x, though, so re-uploading.

tstoeckler’s picture

Status: Needs review » Needs work

The last submitted patch, 26: 2209971-24.patch, failed testing.

berdir’s picture

+++ b/core/lib/Drupal/Core/Entity/ContentEntityType.php
@@ -16,6 +16,11 @@ public function __construct($definition) {
+    if (is_subclass_of($this->class, EntityChangedInterface::class)) {
+      $this->entity_keys += array(
+        'changed' => 'changed',
+      );
+    }
   }

Adding a key enforces an index on that, and by adding it automatically, we add it to all entity types.

I think we shouldn't have that logic (all keys automatically have an index) but no idea how to avoid that in a non BC way. Removing it will also result in schema changes, just in the opposite direction.

Maybe start to introduce a blacklist of entity keys that shoudn't get a key with a @todo to change it to a whitelist for 9.x?

Or maybe we actually want that index by default? Node adds one by hand at the moment..

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

timmillwood’s picture

+++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
@@ -1199,6 +1199,17 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
+      if ($entity_type->isRevisionable()) {
+        $fields[$entity_type->getKey('changed')]->setRevisionable(TRUE);
+      }
+      if ($entity_type->isTranslatable()) {
+        $fields[$entity_type->getKey('changed')]->setTranslatable(TRUE);
+      }

Is there any harm in making it revisionable and translatable even if the entity type isn't?

amateescu’s picture

hchonov’s picture

Re #29:

Adding a key enforces an index on that, and by adding it automatically, we add it to all entity types.

Just talked with @berdir and @amateescu in IRC about this and what @berdir was pointing out is that the entity keys are flagged as NOT NULL, which according to @amateescu will change in #2841291: Fix NOT NULL handling in the entity storage and 'primary key' changes when updating the storage definition of an identifier field "and only required fields will be marked as NOT NULL".

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

andypost’s picture

In related #2086125: Last read comment field/filter/argument uses still the node.changed instead of node_field_data.changed column views needs to make sure that changed field exists for entity type
I used to add todo here because there's no way now to make sure that commented entity has the field (except checking for interface)

Patch is re-roll and fix remaining entities

Status: Needs review » Needs work

The last submitted patch, 40: 2209971-40.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

tstoeckler’s picture

The interdiff in #40 is incorrect, but the patch itself looks good, it fixes the media and workspace entity types to no longer declare the changed entity type.

Will pick this up and attempt to fix #29, i.e. remove the "automatic" adding of the entity key. I'm fine with having to add this explicitly, but I think we should then do that for all core entity types and provide respective update paths to add them.

I think we can also still provide the field in ContentEntityBase and I think we should then update EntityChangedTrait to fall back on 'changed' as the field name, but with a deprecation notice, so that in Drupal 10 a 'changed' entity key will be required for using EntityChangedTrait. Of course, feedback on all parts of this plan is much appreciated.

tstoeckler’s picture

Status: Needs work » Needs review
StatusFileSize
new19.4 KB
new24.68 KB

OK, here's an updated patch that should be fair bit along the path laid out in #43. In detail the following patch:

  • Adds a default description to the changed field. This is taken from what most of the fields are currently using. I didn't (re-)introduce the descriptions for minor textual differences in the description (in particular "that the" vs. "the") but I did (re-)add an overridden description for files as the difference between "editing" and "changing" seemed noteworthy there, as files are not actually ever edited in Drupal.
  • Removes the default changed key per #29
  • Updates EntityChangedTrait to fall back to hardcoding changed as the field name (with deprecation). There is not yet deprecation testing for this.
  • Added an override to SqlContentEntityStorageSchema to not mark the changed entity key as NOT NULL in the schema. This is also mentioned in #29 already.
  • Adds a changed key to all core entity types with an according update function (except for the test entity types). UpdatePathTestBaseFilledTest passes with this applied, so this seems to work. Not sure if we need dedicated update path tests for this because any outstanding schema changes would already fail all existing update tests. I think not, but not sure.
  • Adds checking for the changed key to ContentTranslationHandler and ContentTranslationMetadataWrapper. I found this by just checking where 'changed' is referenced in core. The fallback to hardcoding changed is still there with deprecation. The deprecation testing for this is also still missing.
tstoeckler’s picture

StatusFileSize
new720 bytes
new25 KB

Sorry, not yet in the habit of running the pre-commit script locally. This one should be better.

Status: Needs review » Needs work

The last submitted patch, 45: 2209971-45.patch, failed testing. View results

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

rassoni’s picture

Status: Needs work » Needs review
StatusFileSize
new49.83 KB
new29.08 KB

Reroll patched

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.