class EntityCreate
@internal This API is experimental.
Attributes
#[ConfigAction(id: 'entity_create', deriver: EntityCreateDeriver::class)]
  Hierarchy
- class \Drupal\Core\Config\Action\Plugin\ConfigAction\EntityCreate implements \Drupal\Core\Config\Action\ConfigActionPluginInterface, \Drupal\Core\Plugin\ContainerFactoryPluginInterface
 
Expanded class hierarchy of EntityCreate
File
- 
              core/
lib/ Drupal/ Core/ Config/ Action/ Plugin/ ConfigAction/ EntityCreate.php, line 20  
Namespace
Drupal\Core\Config\Action\Plugin\ConfigActionView source
final class EntityCreate implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
  
  /**
   * Constructs a EntityCreate object.
   *
   * @param \Drupal\Core\Config\ConfigManagerInterface $configManager
   *   The config manager.
   * @param \Drupal\Core\Config\Action\Exists $exists
   *   Determines behavior of action depending on entity existence.
   */
  public function __construct(protected readonly ConfigManagerInterface $configManager, protected readonly Exists $exists) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
    assert(is_array($plugin_definition) && is_array($plugin_definition['constructor_args']), '$plugin_definition contains the expected settings');
    return new static($container->get('config.manager'), ...$plugin_definition['constructor_args']);
  }
  
  /**
   * {@inheritdoc}
   */
  public function apply(string $configName, mixed $value) : void {
    if (!is_array($value)) {
      throw new ConfigActionException(sprintf("The value provided to create %s must be an array", $configName));
    }
    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface|null $entity */
    $entity = $this->configManager
      ->loadConfigEntityByName($configName);
    if ($this->exists
      ->returnEarly($configName, $entity)) {
      return;
    }
    $entity_type_manager = $this->configManager
      ->getEntityTypeManager();
    $entity_type_id = $this->configManager
      ->getEntityTypeIdByName($configName);
    if ($entity_type_id === NULL) {
      throw new ConfigActionException(sprintf("Cannot determine a config entity type from %s", $configName));
    }
    /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type */
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    $id = substr($configName, strlen($entity_type->getConfigPrefix()) + 1);
    $entity_type_manager->getStorage($entity_type->id())
      ->create($value + [
      'id' => $id,
    ])
      ->save();
  }
}
Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| EntityCreate::apply | public | function | Applies the config action. | Overrides ConfigActionPluginInterface::apply | 
| EntityCreate::create | public static | function | Creates an instance of the plugin. | Overrides ContainerFactoryPluginInterface::create | 
| EntityCreate::__construct | public | function | Constructs a EntityCreate object. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.