Verified Commit f5217923 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420521 by narendraR, phenaproxima, Wim Leers, alexpott: Create a...

Issue #3420521 by narendraR, phenaproxima, Wim Leers, alexpott: Create a config action that can instantiate a field on every bundle of the target entity type

(cherry picked from commit 824456063fb7af8500abf86fcce8e78e508bca0c)
parent cbef7444
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\field\Plugin\ConfigAction;

use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Config\Action\ConfigActionPluginInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\field\FieldStorageConfigInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Adds a field to all bundles of its target entity type.
 *
 * @ConfigAction(
 *   id = "field_storage_config:addToAllBundles",
 *   label = @Translation("Add a field to all bundles"),
 *   entity_types = {"field_storage_config"},
 *   description = @Translation("Add this field on every bundle of its target entity type.")
 * )
 *
 * @internal
 *   This API is experimental.
 */
final class AddToAllBundles implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {

  public function __construct(
    private readonly EntityTypeManagerInterface $entityTypeManager,
    private readonly EntityTypeBundleInfoInterface $entityTypeBundleInfo,
    private readonly ConfigManagerInterface $configManager,
  ) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    return new static(
      $container->get(EntityTypeManagerInterface::class),
      $container->get(EntityTypeBundleInfoInterface::class),
      $container->get(ConfigManagerInterface::class),
    );

  }

  /**
   * {@inheritdoc}
   */
  public function apply(string $configName, mixed $value): void {
    assert(is_array($value));

    $field_storage = $this->configManager->loadConfigEntityByName($configName);
    assert($field_storage instanceof FieldStorageConfigInterface);

    $storage = $this->entityTypeManager->getStorage('field_config');

    $entity_type_id = $field_storage->getTargetEntityTypeId();
    $field_name = $field_storage->getName();

    $existing_fields = $storage->getQuery()
      ->condition('entity_type', $entity_type_id)
      ->condition('field_name', $field_name)
      ->execute();

    // Get all bundles of the target entity type.
    $bundles = array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
    foreach ($bundles as $bundle) {
      $id = "$entity_type_id.$bundle.$field_name";
      if (in_array($id, $existing_fields, TRUE)) {
        if (empty($value['fail_if_exists'])) {
          continue;
        }
        throw new ConfigActionException(sprintf('Field %s already exists.', $id));
      }
      $storage->create([
        'label' => $value['label'],
        'bundle' => $bundle,
        'description' => $value['description'],
        'field_storage' => $field_storage,
      ])->save();
    }
  }

}
+150 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\field\Kernel;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Action\ConfigActionException;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Recipe\Recipe;
use Drupal\Core\Recipe\RecipeRunner;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;

/**
 * @covers \Drupal\field\Plugin\ConfigAction\AddToAllBundles
 *
 * @group Recipe
 * @group field
 */
class AddToAllBundlesConfigActionTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['field', 'node', 'system', 'text', 'user'];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    NodeType::create([
      'type' => 'one',
      'name' => 'One',
    ])->save();
    NodeType::create([
      'type' => 'two',
      'name' => 'Two',
    ])->save();
  }

  /**
   * Tests instantiating a field on all bundles of an entity type.
   */
  public function testInstantiateNewFieldOnAllBundles(): void {
    // Ensure the body field doesn't actually exist yet.
    $storage_definitions = $this->container->get(EntityFieldManagerInterface::class)
      ->getFieldStorageDefinitions('node');
    $this->assertArrayNotHasKey('body', $storage_definitions);

    $this->applyAction('field.storage.node.body');

    // Fields and expected data exist.
    /** @var \Drupal\field\FieldConfigInterface[] $body_fields */
    $body_fields = $this->container->get(EntityTypeManagerInterface::class)
      ->getStorage('field_config')
      ->loadByProperties([
        'entity_type' => 'node',
        'field_name' => 'body',
      ]);
    ksort($body_fields);
    $this->assertSame(['node.one.body', 'node.two.body'], array_keys($body_fields));
    foreach ($body_fields as $field) {
      $this->assertSame('Body field label', $field->label());
      $this->assertSame('Set by config actions.', $field->getDescription());
    }

    // Expect an error when the 'addToAllBundles' action is invoked on anything
    // other than a field storage config entity.
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "addToAllBundles" plugin does not exist.');
    $this->applyAction('user.role.anonymous');
  }

  /**
   * Tests that the action can be set to fail if the field already exists.
   */
  public function testFailIfExists(): void {
    $this->installConfig('node');
    node_add_body_field(NodeType::load('one'));

    $this->expectException(ConfigActionException::class);
    $this->expectExceptionMessage('Field node.one.body already exists.');
    $this->applyAction('field.storage.node.body', TRUE);
  }

  /**
   * Tests that the action will ignore existing fields by default.
   */
  public function testIgnoreExistingFields(): void {
    $this->installConfig('node');

    node_add_body_field(NodeType::load('one'))
      ->setLabel('Original label')
      ->setDescription('Original description')
      ->save();

    $this->applyAction('field.storage.node.body');

    // The existing field should not be changed.
    $field = FieldConfig::loadByName('node', 'one', 'body');
    $this->assertInstanceOf(FieldConfig::class, $field);
    $this->assertSame('Original label', $field->label());
    $this->assertSame('Original description', $field->getDescription());

    // But the new field should be created as expected.
    $field = FieldConfig::loadByName('node', 'two', 'body');
    $this->assertInstanceOf(FieldConfig::class, $field);
    $this->assertSame('Body field label', $field->label());
    $this->assertSame('Set by config actions.', $field->getDescription());
  }

  /**
   * Applies a recipe with the addToAllBundles action.
   *
   * @param string $config_name
   *   The name of the config object which should run the addToAllBundles
   *   action.
   * @param bool $fail_if_exists
   *   (optional) Whether the action should fail if the field already exists on
   *   any bundle. Defaults to FALSE.
   */
  private function applyAction(string $config_name, bool $fail_if_exists = FALSE): void {
    $fail_if_exists = var_export($fail_if_exists, TRUE);
    $contents = <<<YAML
name: Instantiate field on all bundles
config:
  import:
    node:
      - field.storage.node.body
  actions:
    $config_name:
      addToAllBundles:
        label: Body field label
        description: Set by config actions.
        fail_if_exists: $fail_if_exists
YAML;
    $dir = uniqid('public://');
    mkdir($dir);
    file_put_contents($dir . '/recipe.yml', $contents);

    $recipe = Recipe::createFromDirectory($dir);
    RecipeRunner::processRecipe($recipe);
  }

}