Skip to content
Snippets Groups Projects
Verified Commit 4976f3a4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2810355 by joseph.olstad, Leksat, ravi.shankar, Berdir, jungle,...

Issue #2810355 by joseph.olstad, Leksat, ravi.shankar, Berdir, jungle, suranga.gamage, adityasingh, benjifisher, Jelle_S, alexpott, Wim Leers, xjm, smustgrave, catch, hchonov, star-szr, webchick, wluisi, dawehner, daffie, cilefen: $entity->isDefaultTranslation() behaves incorrectly when changing default translation, causing file/image field usage to be set to zero, causing files to be deleted

(cherry picked from commit aa3ecb2c)
parent 57352211
Branches
Tags
21 merge requests!8376Drupal views: adding more granularity to the ‘use ajax’ functionality,!8300Issue #3443586 View area displays even when parent view has no results.,!7567Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7565Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7509Change label "Block description" to "Block type",!7344Issue #3292350 by O'Briat, KlemenDEV, hswong3i, smustgrave, quietone: Update...,!6922Issue #3412959 by quietone, smustgrave, longwave: Fix 12 'un' words,!6848Issue #3417553 by longwave: Remove withConsecutive() in CacheCollectorTest,!6720Revert "Issue #3358581 by pfrenssen, _tarik_, a.dmitriiev, smustgrave:...,!6560Update ClaroPreRender.php, confirming classes provided are in array format,!6528Issue #3414261 by catch: Add authenticated user umami performance tests,!6501Issue #3263668 by omkar-pd, Wim Leers, hooroomoo: Re-enable inline form errors...,!6354Draft: Issue #3380392 by phma: Updating language weight from the overview reverts label if translated,!6324Issue #3416723 by Ludo.R: Provide a "node type" views default argument,!6119Issue #3405704 by Spokje, longwave: symfony/psr-http-message-bridge major version bump,!5950Issue #3403653 by alexpott, longwave: Incorporate improvements to how contrib runs PHPStan to core,!5858Issue #3401971 by fjgarlin: Test-only job shouldn't require constant rebases...,!5716Draft: Issue #3401102 by Spokje, longwave, smustgrave: Nightwatch artifacts on GitLab not retained,!5674Transaction autocommit during shutdown relies on unreliable object destruction order,!5644Issue #3395563 by nireneko, marvil07, lauriii, borisson_, smustgrave, Wim...,!5041SDC ComponentElement: Transform slots scalar values to #plain_text instead of throwing an exception
Pipeline #33177 failed
Pipeline: drupal

#33182

    Pipeline: drupal

    #33181

      Pipeline: drupal

      #33180

        +1
        ......@@ -86,6 +86,18 @@ abstract class ContentEntityBase extends EntityBase implements \IteratorAggregat
        */
        protected $activeLangcode = LanguageInterface::LANGCODE_DEFAULT;
        /**
        * Override the result of isDefaultTranslation().
        *
        * Under certain circumstances, such as when changing default translation, the
        * default value needs to be overridden.
        *
        * @var bool|null
        *
        * @internal
        */
        protected ?bool $enforceDefaultTranslation = NULL;
        /**
        * Local cache for the default language code.
        *
        ......@@ -409,10 +421,28 @@ public function setRevisionTranslationAffectedEnforced($enforced) {
        return $this;
        }
        /**
        * Set or clear an override of the isDefaultTranslation() result.
        *
        * @param bool|null $enforce_default_translation
        * If boolean value is passed, the value will override the result of
        * isDefaultTranslation() method. If NULL is passed, the default logic will
        * be used.
        *
        * @return $this
        */
        public function setDefaultTranslationEnforced(?bool $enforce_default_translation): static {
        $this->enforceDefaultTranslation = $enforce_default_translation;
        return $this;
        }
        /**
        * {@inheritdoc}
        */
        public function isDefaultTranslation() {
        if ($this->enforceDefaultTranslation !== NULL) {
        return $this->enforceDefaultTranslation;
        }
        return $this->activeLangcode === LanguageInterface::LANGCODE_DEFAULT;
        }
        ......
        ......@@ -944,7 +944,21 @@ protected function invokeFieldMethod($method, ContentEntityInterface $entity) {
        if ($method == 'postSave' && !empty($entity->original)) {
        $original_langcodes = array_keys($entity->original->getTranslationLanguages());
        foreach (array_diff($original_langcodes, $langcodes) as $removed_langcode) {
        /** @var \Drupal\Core\Entity\ContentEntityInterface $translation */
        $translation = $entity->original->getTranslation($removed_langcode);
        // Fields may rely on the isDefaultTranslation() method to determine
        // what is going to be deleted - the whole entity or a particular
        // translation.
        if ($translation->isDefaultTranslation()) {
        if (method_exists($translation, 'setDefaultTranslationEnforced')) {
        $translation->setDefaultTranslationEnforced(FALSE);
        }
        else {
        @trigger_error('Not providing a setDefaultTranslationEnforced() method when implementing \Drupal\Core\TypedData\TranslatableInterface is deprecated in drupal:10.2.0 and is required from drupal:11.0.0. See https://www.drupal.org/node/3376146', E_USER_DEPRECATED);
        }
        }
        $fields = $translation->getTranslatableFields();
        foreach ($fields as $name => $items) {
        $items->delete();
        ......
        ......@@ -215,4 +215,40 @@ public function testSyncedFiles() {
        $this->assertTrue($file->isTemporary());
        }
        /**
        * Tests if file field tracks file usages correctly on translated nodes.
        */
        public function testFileUsage() {
        /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
        $file_usage = \Drupal::service('file.usage');
        // Enable language selector on the page edit form.
        $edit = [
        'language_configuration[language_alterable]' => 1,
        ];
        $this->drupalGet('admin/structure/types/manage/page');
        $this->submitForm($edit, 'Save');
        // Create a node and upload a file.
        $node = $this->drupalCreateNode(['type' => 'page']);
        $edit = [
        'files[' . $this->fieldName . '_0]' => \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri),
        ];
        $this->drupalGet('node/' . $node->id() . '/edit');
        $this->submitForm($edit, 'Save');
        // Check if the file usage is correct.
        $file = File::load($this->getLastFileId());
        $this->assertEquals($file_usage->listUsage($file), ['file' => ['node' => [$node->id() => '1']]]);
        // Check if the file usage is tracked correctly when changing the original
        // language of an entity.
        $edit = [
        'langcode[0][value]' => 'fr',
        ];
        $this->drupalGet('node/' . $node->id() . '/edit');
        $this->submitForm($edit, 'Save');
        $this->assertEquals($file_usage->listUsage($file), ['file' => ['node' => [$node->id() => '1']]]);
        }
        }
        ......@@ -297,7 +297,7 @@ public function testEntityPropertiesModifications() {
        // Retrieve the entity properties.
        $reflection = new \ReflectionClass($entity);
        $properties = $reflection->getProperties(~\ReflectionProperty::IS_STATIC);
        $translation_unique_properties = ['activeLangcode', 'translationInitialize', 'fieldDefinitions', 'languages', 'langcodeKey', 'defaultLangcode', 'defaultLangcodeKey', 'revisionTranslationAffectedKey', 'validated', 'validationRequired', 'entityTypeId', 'typedData', 'cacheContexts', 'cacheTags', 'cacheMaxAge', '_serviceIds', '_entityStorages'];
        $translation_unique_properties = ['activeLangcode', 'translationInitialize', 'fieldDefinitions', 'languages', 'langcodeKey', 'defaultLangcode', 'defaultLangcodeKey', 'revisionTranslationAffectedKey', 'validated', 'validationRequired', 'entityTypeId', 'typedData', 'cacheContexts', 'cacheTags', 'cacheMaxAge', '_serviceIds', '_entityStorages', 'enforceDefaultTranslation'];
        foreach ($properties as $property) {
        // Modify each entity property on the clone and assert that the change is
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment