Issue #3483353 by a.dmitriiev, phenaproxima, atul_ghate, alexpott, roderik: Remove the createCopy action from EntityDisplayBase, and make cloneAs compatible with wildcards

(cherry picked from commit 3de28c0f67)
merge-requests/9350/merge
Alex Pott 2024-11-25 23:24:47 +00:00
parent 41f0ccb54d
commit 21d16eedcc
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
3 changed files with 18 additions and 12 deletions

View File

@ -56,6 +56,21 @@ final class EntityClone implements ConfigActionPluginInterface, ContainerFactory
if (empty($original)) {
throw new ConfigActionException("Cannot clone '$configName' because it does not exist.");
}
// Treat the original ID like a period-separated array of strings, and
// replace any `%` parts in the clone's ID with the corresponding part of
// the original ID. For example, if we're cloning an entity view display
// with the ID `node.foo.teaser`, and the clone's ID is
// `node.%.search_result`, the final ID of the clone will be
// `node.foo.search_result`.
$original_id_parts = explode('.', $original->id());
$clone_id_parts = explode('.', $value['id']);
assert(count($original_id_parts) === count($clone_id_parts));
foreach ($clone_id_parts as $index => $part) {
$clone_id_parts[$index] = $part === '%' ? $original_id_parts[$index] : $part;
}
$value['id'] = implode('.', $clone_id_parts);
$clone = $original->createDuplicate();
$clone->set($original->getEntityType()->getKey('id'), $value['id']);

View File

@ -324,7 +324,6 @@ abstract class EntityDisplayBase extends ConfigEntityBase implements EntityDispl
/**
* {@inheritdoc}
*/
#[ActionMethod(adminLabel: new TranslatableMarkup('Copy to another mode'), pluralize: FALSE)]
public function createCopy($mode) {
$display = $this->createDuplicate();
$display->mode = $display->originalMode = $mode;

View File

@ -93,9 +93,9 @@ class EntityCloneConfigActionTest extends KernelTestBase {
}
/**
* Tests cloning entity displays, which have specialized logic for that.
* Tests wildcard support, which allows positional tokens in the clone's ID.
*/
public function testCloneEntityDisplay(): void {
public function testCloneWithWildcards(): void {
$this->container->get(ModuleInstallerInterface::class)->install(['node']);
$this->createContentType(['type' => 'alpha']);
$this->createContentType(['type' => 'beta']);
@ -112,17 +112,9 @@ class EntityCloneConfigActionTest extends KernelTestBase {
// Use the action to clone the default view displays to the `rss` view mode.
/** @var \Drupal\Core\Config\Action\ConfigActionManager $manager */
$manager = $this->container->get('plugin.manager.config_action');
$manager->applyAction('cloneAs', 'core.entity_view_display.node.alpha.default', 'node.alpha.rss');
$manager->applyAction('entity_method:core.entity_view_display:createCopy', 'core.entity_view_display.node.beta.default', 'rss');
$manager->applyAction('cloneAs', 'core.entity_view_display.node.*.default', 'node.%.rss');
$this->assertFalse($display_repository->getViewDisplay('node', 'alpha', 'rss')->isNew());
$this->assertFalse($display_repository->getViewDisplay('node', 'beta', 'rss')->isNew());
// Ensure that this also works with wildcards.
$this->assertTrue($display_repository->getViewDisplay('node', 'alpha', 'search_result')->isNew());
$this->assertTrue($display_repository->getViewDisplay('node', 'beta', 'search_result')->isNew());
$manager->applyAction('entity_method:core.entity_view_display:createCopy', 'core.entity_view_display.node.*.default', 'search_result');
$this->assertFalse($display_repository->getViewDisplay('node', 'alpha', 'search_result')->isNew());
$this->assertFalse($display_repository->getViewDisplay('node', 'beta', 'search_result')->isNew());
}
}