Unverified Commit 9b7d24a6 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3481695 by phenaproxima, a.dmitriiev, thejimbirch, b_sharpe, alexpott:...

Issue #3481695 by phenaproxima, a.dmitriiev, thejimbirch, b_sharpe, alexpott: Entity displays cloning requires special config action

(cherry picked from commit a1b47eaf)
parent 25ad53d3
Loading
Loading
Loading
Loading
Loading
+5 −3
Changes for core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/EntityMethod.php: 5 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -138,12 +138,14 @@ private function applySingle(ConfigEntityInterface $entity, mixed $value): Confi
      if ($this->numberOfRequiredParams !== 1 && $this->numberOfParams !== 1) {
        throw new EntityMethodException(sprintf('Entity method config action \'%s\' requires an array value. The number of parameters or required parameters for %s::%s() is not 1', $this->pluginId, $entity->getEntityType()->getClass(), $this->method));
      }
      $entity->{$this->method}($value);
      $result = $entity->{$this->method}($value);
    }
    else {
      $entity->{$this->method}(...$value);
      $result = $entity->{$this->method}(...$value);
    }
    return $entity;
    // If an instance of the entity (either itself, or a clone) was returned
    // by the method, return that.
    return is_a($result, get_class($entity)) ? $result : $entity;
  }

}
+17 −0
Changes for core/lib/Drupal/Core/Entity/EntityDisplayBase.php: 17 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -324,6 +324,7 @@ public function toArray() {
  /**
   * {@inheritdoc}
   */
  #[ActionMethod(adminLabel: new TranslatableMarkup('Copy to another mode'), pluralize: FALSE)]
  public function createCopy($mode) {
    $display = $this->createDuplicate();
    $display->mode = $display->originalMode = $mode;
@@ -588,4 +589,20 @@ protected function getLogger() {
    return \Drupal::logger('system');
  }

  /**
   * {@inheritdoc}
   */
  public function set($property_name, $value): static {
    // If changing the entity ID, also update the target entity type, bundle,
    // and view mode.
    if ($this->isNew() && $property_name === $this->getEntityType()->getKey('id')) {
      if (substr_count($value, '.') !== 2) {
        throw new \InvalidArgumentException("'$value' is not a valid entity display ID.");
      }
      [$this->targetEntityType, $this->bundle, $this->mode] = explode('.', $value);
    }
    parent::set($property_name, $value);
    return $this;
  }

}
+1 −0
Changes for core/modules/layout_builder/tests/src/Kernel/LayoutBuilderEntityViewDisplayValidationTest.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ public function testLabelValidation(): void {
   */
  public function testImmutableProperties(array $valid_values = []): void {
    parent::testImmutableProperties([
      'id' => 'entity_test_with_bundle.two.full',
      'targetEntityType' => 'entity_test_with_bundle',
      'bundle' => 'two',
    ]);
+19 −0
Changes for core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

use Drupal\comment\Entity\CommentType;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
@@ -170,4 +171,22 @@ public function testOnDependencyRemoval(): void {
    $this->assertSame($expected_dependencies, $entity_display->getDependencies());
  }

  /**
   * Tests that changing the entity ID updates related properties.
   */
  public function testChangeId(): void {
    /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
    $display = $this->container->get(EntityDisplayRepositoryInterface::class)
      ->getViewDisplay('entity_test', 'entity_test');
    $this->assertSame('entity_test.entity_test.default', $display->id());
    $display->set('id', 'node.page.rss');
    $this->assertSame('node', $display->getTargetEntityTypeId());
    $this->assertSame('page', $display->getTargetBundle());
    $this->assertSame('rss', $display->getMode());

    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage("'a.b' is not a valid entity display ID.");
    $display->set('id', 'a.b');
  }

}
+1 −0
Changes for core/tests/Drupal/KernelTests/Core/Entity/EntityFormDisplayValidationTest.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ public function testTargetBundleMustExist(): void {
   */
  public function testImmutableProperties(array $valid_values = []): void {
    parent::testImmutableProperties([
      'id' => 'entity_test_with_bundle.two.default',
      'targetEntityType' => 'entity_test_with_bundle',
      'bundle' => 'two',
    ]);
Loading