You are here

class StorageManager in Paragraph View Mode 8

Same name and namespace in other branches
  1. 2.x src/StorageManager.php \Drupal\paragraph_view_mode\StorageManager

Provides fields and forms storage operations required by the module.

@package Drupal\paragraph_view_mode

Hierarchy

Expanded class hierarchy of StorageManager

1 string reference to 'StorageManager'
paragraph_view_mode.services.yml in ./paragraph_view_mode.services.yml
paragraph_view_mode.services.yml
1 service uses StorageManager
paragraph_view_mode.storage_manager in ./paragraph_view_mode.services.yml
Drupal\paragraph_view_mode\StorageManager

File

src/StorageManager.php, line 24

Namespace

Drupal\paragraph_view_mode
View source
class StorageManager implements StorageManagerInterface {
  use MessengerTrait;
  use LoggerChannelTrait;
  use StringTranslationTrait;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Entity form display storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $formDisplay;

  /**
   * Logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * StorageManager constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->formDisplay = $this->entityTypeManager
      ->getStorage('entity_form_display');
    $this->logger = $this
      ->getLogger(StorageManagerInterface::CONFIG_NAME);
  }

  /**
   * {@inheritdoc}
   */
  public function addField(string $bundle) : bool {
    $field = $this
      ->getField($bundle);
    if (!$field) {
      try {
        $this
          ->createField($bundle);
        $this
          ->messenger()
          ->addMessage($this
          ->t('%label field has been enabled on %type bundle %bundle', [
          '%label' => StorageManagerInterface::FIELD_LABEL,
          '%type' => StorageManagerInterface::ENTITY_TYPE,
          '%bundle' => $bundle,
        ]));
        return TRUE;
      } catch (EntityStorageException $exception) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('Unable to craete %label for %type bundle %bundle', [
          '%label' => StorageManagerInterface::FIELD_LABEL,
          '%type' => StorageManagerInterface::ENTITY_TYPE,
          '%bundle' => $bundle,
        ]), MessengerInterface::TYPE_ERROR);
        return FALSE;
      }
    }
    else {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteField(string $bundle) : bool {
    $field = $this
      ->getField($bundle);
    if ($field) {
      try {
        $field
          ->delete();
        $this
          ->messenger()
          ->addMessage($this
          ->t('%label field has been deleted on %type bundle %bundle', [
          '%label' => StorageManagerInterface::FIELD_LABEL,
          '%type' => StorageManagerInterface::ENTITY_TYPE,
          '%bundle' => $bundle,
        ]));
        return TRUE;
      } catch (EntityStorageException $exception) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('Unable to delete %label for %type bundle %bundle', [
          '%label' => StorageManagerInterface::FIELD_LABEL,
          '%type' => StorageManagerInterface::ENTITY_TYPE,
          '%bundle' => $bundle,
        ]), MessengerInterface::TYPE_ERROR);
        return FALSE;
      }
    }
    else {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setFieldLabel(string $bundle, string $label) : void {
    $field = $this
      ->getField($bundle);
    if ($field) {
      $field
        ->set('label', $label);
      $field
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function addToFormDisplay(string $bundle, string $form_mode = 'default') : void {
    $form_display_id = implode('.', [
      StorageManagerInterface::ENTITY_TYPE,
      $bundle,
      $form_mode,
    ]);
    try {
      $form_display = $this->formDisplay
        ->load($form_display_id);
      if ($form_display instanceof EntityDisplayInterface) {
        $form_display
          ->setComponent(StorageManagerInterface::FIELD_NAME, [
          'type' => StorageManagerInterface::FIELD_TYPE,
          'weight' => -100,
          'settings' => ParagraphViewModeWidget::defaultSettings(),
        ])
          ->save();
        $this->logger
          ->info('%label field has been placed on the %bundle form display %mode', [
          '%label' => StorageManagerInterface::FIELD_LABEL,
          '%bundle' => $bundle,
          '%mode' => $form_mode,
        ]);
      }
      else {
        throw new EntityStorageException();
      }
    } catch (EntityStorageException $exception) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Unable to place %label field on the %bundle form display %mode, please place it manually.', [
        '%label' => StorageManagerInterface::FIELD_LABEL,
        '%bundle' => $bundle,
        '%mode' => $form_mode,
      ]), MessengerInterface::TYPE_WARNING);
      $this->logger
        ->error('Unable to load form display %mode for %type of bundle %bundle', [
        '%mode' => $form_mode,
        '%type' => StorageManagerInterface::FIELD_TYPE,
        '%bundle' => $bundle,
      ]);
    }
  }

  /**
   * Get field config.
   *
   * @param string $bundle
   *   Paragraph entity bundle.
   *
   * @return \Drupal\field\FieldConfigInterface|null
   *   Field config.
   */
  protected function getField(string $bundle) : ?FieldConfigInterface {
    return FieldConfig::loadByName($type = StorageManagerInterface::ENTITY_TYPE, $bundle, $field_name = StorageManagerInterface::FIELD_NAME);
  }

  /**
   * Create new field config.
   *
   * @param string $bundle
   *   Paragraph entity bundle.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createField(string $bundle) : void {
    $field = FieldConfig::create([
      'field_storage' => $this
        ->getFieldStorage(),
      'bundle' => $bundle,
      'label' => $this
        ->t(StorageManagerInterface::FIELD_LABEL),
    ]);
    $field
      ->save();
  }

  /**
   * Get field config storage.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   Field storage configuration entity.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function getFieldStorage() : EntityInterface {
    $field_storage = $this
      ->loadFieldStorage();
    if (!$field_storage) {
      $field_storage = $this
        ->createFieldStorage();
    }
    return $field_storage;
  }

  /**
   * Create field storage configuration entity.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   Field storage configuration entity.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createFieldStorage() : EntityInterface {
    $field_storage = FieldStorageConfig::create([
      'entity_type' => StorageManagerInterface::ENTITY_TYPE,
      'field_name' => StorageManagerInterface::FIELD_NAME,
      'type' => StorageManagerInterface::FIELD_TYPE,
      'locked' => TRUE,
    ]);
    $field_storage
      ->save();
    return $field_storage;
  }

  /**
   * Load existing field storage.
   *
   * @return \Drupal\field\FieldStorageConfigInterface|null
   *   Field storage configuration entity.
   */
  protected function loadFieldStorage() : ?FieldStorageConfigInterface {
    return FieldStorageConfig::loadByName($type = StorageManagerInterface::ENTITY_TYPE, $field_name = StorageManagerInterface::FIELD_NAME);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
StorageManager::$entityTypeManager protected property Entity type manager.
StorageManager::$formDisplay protected property Entity form display storage.
StorageManager::$logger protected property Logger.
StorageManager::addField public function Add field to the given bundle. Overrides StorageManagerInterface::addField
StorageManager::addToFormDisplay public function Add paragraph view mode field to paragraph entity form display. Overrides StorageManagerInterface::addToFormDisplay
StorageManager::createField protected function Create new field config.
StorageManager::createFieldStorage protected function Create field storage configuration entity.
StorageManager::deleteField public function Delete field from the given bundle. Overrides StorageManagerInterface::deleteField
StorageManager::getField protected function Get field config.
StorageManager::getFieldStorage protected function Get field config storage.
StorageManager::loadFieldStorage protected function Load existing field storage.
StorageManager::setFieldLabel public function Sets field label. Overrides StorageManagerInterface::setFieldLabel
StorageManager::__construct public function StorageManager constructor.
StorageManagerInterface::CONFIG_NAME constant Module configuration name.
StorageManagerInterface::ENTITY_TYPE constant Entity type name.
StorageManagerInterface::FIELD_LABEL constant View mode field label.
StorageManagerInterface::FIELD_NAME constant View mode field name.
StorageManagerInterface::FIELD_TYPE constant View mode field type.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.