devel_wizard-2.x-dev/src/Plugin/DevelWizard/Spell/ConfigEntitySpellBase.php

src/Plugin/DevelWizard/Spell/ConfigEntitySpellBase.php
<?php

declare(strict_types=1);

namespace Drupal\devel_wizard\Plugin\DevelWizard\Spell;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;

/**
 * @todo Re-usable ::doIt() method.
 */
abstract class ConfigEntitySpellBase extends SpellBase implements
  PluginFormInterface,
  ConfigurableInterface,
  ContainerFactoryPluginInterface {

  /**
   * Machine-name of the module which provides the entity type.
   *
   * @abstract
   */
  protected string $provider = '';

  /**
   * @abstract
   */
  protected string $configEntityTypeId = '';

  protected ?ConfigEntityTypeInterface $configEntityType = NULL;

  protected EntityTypeManagerInterface $entityTypeManager;

  protected function getConfigEntityType(): ?ConfigEntityTypeInterface {
    if ($this->configEntityType === NULL
      && $this->entityTypeManager->hasDefinition($this->configEntityTypeId)
    ) {
      /* @noinspection PhpUnhandledExceptionInspection */
      $this->configEntityType = $this->entityTypeManager->getDefinition($this->configEntityTypeId);
    }

    return $this->configEntityType;
  }

  protected function getConfigStorage(): ?ConfigEntityStorageInterface {
    return $this->entityTypeManager->hasDefinition($this->configEntityTypeId) ?
      $this->entityTypeManager->getStorage($this->configEntityTypeId)
      : NULL;
  }

  protected string $contentEntityTypeId = '';

  protected ?ContentEntityTypeInterface $contentEntityType = NULL;

  protected function getContentEntityType(): ?ContentEntityTypeInterface {
    if ($this->contentEntityType === NULL
      && $this->entityTypeManager->hasDefinition($this->contentEntityTypeId)
    ) {
      /* @noinspection PhpUnhandledExceptionInspection */
      $this->contentEntityType = $this->entityTypeManager->getDefinition($this->contentEntityTypeId);
    }

    return $this->contentEntityType;
  }

  protected function getContentStorage(): ?ContentEntityStorageInterface {
    return $this->entityTypeManager->hasDefinition($this->contentEntityTypeId) ?
      $this->entityTypeManager->getStorage($this->contentEntityTypeId)
      : NULL;
  }

  /**
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  protected function createConfigInstance(array $values): ConfigEntityInterface {
    $storage = $this->getConfigStorage();
    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
    $entity = $storage->create($values);
    $entity->save();
    $this->messageConfigEntityCreate($entity);

    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  public function getRequiredModules(): array {
    $modules = parent::getRequiredModules();
    if ($this->provider) {
      $modules[$this->provider] = TRUE;
    }

    return $modules;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc