Loading core/modules/file/src/Hook/TokenHooks.php +6 −3 Original line number Diff line number Diff line Loading @@ -88,10 +88,13 @@ public function tokens($type, $tokens, array $data, array $options, BubbleableMe break; case 'owner': $owner = $file->getOwner(); // Only replace the token when the file has a valid owner. When the // owner is missing the token is left unreplaced, consistent with // the chained [file:owner:*] tokens handled below. if ($owner = $file->getOwner()) { $bubbleable_metadata->addCacheableDependency($owner); $name = $owner->label(); $replacements[$original] = $name; $replacements[$original] = $owner->label(); } break; } } Loading core/modules/file/tests/src/Functional/FileTokenReplaceTest.php +11 −0 Original line number Diff line number Diff line Loading @@ -109,6 +109,17 @@ public function testFileTokenReplacement(): void { ]); $this->assertEquals($expected, $output, "Unsanitized file token $input replaced."); } // Assign the file to a non-existent user so that getOwner() returns NULL, // then verify that the owner tokens are left unreplaced rather than causing // an error. Both [file:owner] and the chained [file:owner:*] tokens are // skipped when there is no user entity to derive their values from. $file->setOwnerId(999999)->save(); $this->assertNull($file->getOwner()); foreach (['[file:owner]', '[file:owner:uid]', '[file:owner:name]'] as $input) { $output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId()]); $this->assertSame($input, $output, "File token $input is left unreplaced when the owner is missing."); } } } Loading
core/modules/file/src/Hook/TokenHooks.php +6 −3 Original line number Diff line number Diff line Loading @@ -88,10 +88,13 @@ public function tokens($type, $tokens, array $data, array $options, BubbleableMe break; case 'owner': $owner = $file->getOwner(); // Only replace the token when the file has a valid owner. When the // owner is missing the token is left unreplaced, consistent with // the chained [file:owner:*] tokens handled below. if ($owner = $file->getOwner()) { $bubbleable_metadata->addCacheableDependency($owner); $name = $owner->label(); $replacements[$original] = $name; $replacements[$original] = $owner->label(); } break; } } Loading
core/modules/file/tests/src/Functional/FileTokenReplaceTest.php +11 −0 Original line number Diff line number Diff line Loading @@ -109,6 +109,17 @@ public function testFileTokenReplacement(): void { ]); $this->assertEquals($expected, $output, "Unsanitized file token $input replaced."); } // Assign the file to a non-existent user so that getOwner() returns NULL, // then verify that the owner tokens are left unreplaced rather than causing // an error. Both [file:owner] and the chained [file:owner:*] tokens are // skipped when there is no user entity to derive their values from. $file->setOwnerId(999999)->save(); $this->assertNull($file->getOwner()); foreach (['[file:owner]', '[file:owner:uid]', '[file:owner:name]'] as $input) { $output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId()]); $this->assertSame($input, $output, "File token $input is left unreplaced when the owner is missing."); } } }