Problem/Motivation

FieldItem::delete is being called in two cases:

1. When the whole entity is being deleted - for all translatable and non-translatable fields.
2. When a translation is removed - for all translatable fields of the removed translation.

However we are unable of determining if the parent is being deleted or only a translation is being removed from it.

Proposed resolution

Add a parameter to FieldItem::delete to specify the action we are currently doing.

Remaining tasks

User interface changes

API changes

Data model changes

Problem/Motivation

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

hchonov created an issue. See original summary.

hchonov’s picture

StatusFileSize
new6.08 KB
berdir’s picture

+++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php
@@ -217,8 +217,13 @@ public function postSave($update);
+   *
+   * @param bool $parent_delete
+   *   Specifies whether the whole parent is being deleted or only a
+   *   translation of it. TRUE if the whole parent is being deleted, FALSE if
+   *   only a translation is being deleted. Defaults to TRUE.
    */
-  public function delete();
+  public function delete($parent_delete = TRUE);
 
   /**

The problem is that even an optional argument is a BC change because all implementations are forced to implement it.

Maybe we can think of something that allows to identify this case in an uglier but backwards compatible way. A flag on the entity object for example.

hchonov’s picture

You are right, but I am not really a fan of setting dynamically a property on the entity as this involves the magic getter and setter. What if we call the delete method with a parameter without defining it in the interface until D9, but provide test for this and in D9 put the parameter into the interface?

berdir’s picture

That might work, nice idea.

hchonov’s picture

StatusFileSize
new7.42 KB
new5.36 KB
hchonov’s picture

StatusFileSize
new7.84 KB
new1.08 KB

I've added also a test for the non-translatable field.

berdir’s picture

  1. +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php
    @@ -201,7 +201,7 @@ public function postSave($update) { }
        * {@inheritdoc}
        */
    -  public function delete() { }
    +  public function delete($parent_delete = TRUE) { }
    

    We should document this somehow. Possibly add a paragraph on the interface and explain that the argument exists but is not enforced in the interface, with a @todo to add it in 9.x

  2. +++ b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php
    @@ -77,8 +77,8 @@ public function postSave($update) {
        */
    -  public function delete() {
    -    parent::delete();
    +  public function delete($parent_delete = TRUE) {
    +    parent::delete($parent_delete);
         $entity = $this->getEntity();
    

    of course that means that not passing the argument along could then result in bugs in code that relies on it :(

hchonov’s picture

1. This is probably the better way.

2. Well you are right but we actually already have such an inconsistency with the $include_computed parameter so without defining the parameter in the interface there is no other way around this.

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.

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.

idebr’s picture

StatusFileSize
new899 bytes
new8.76 KB

Reroll against 8.6.x and implemented the following changes:

#8.1 Added documentation to \Drupal\Core\Field\FieldItemInterface::delete() that it is called with an additional '$parent_delete' parameter that is not enforced through the interface.

#8.2 Not sure on the implications here. I have been unable to find any implementations of FieldItemInterface::delete outside of Drupal core.

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.

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.

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.

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.