class StorageManager in Paragraph View Mode 2.x
Same name and namespace in other branches
- 8 src/StorageManager.php \Drupal\paragraph_view_mode\StorageManager
Provides fields and forms storage operations required by the module.
@package Drupal\paragraph_view_mode
Hierarchy
- class \Drupal\paragraph_view_mode\StorageManager implements StorageManagerInterface uses LoggerChannelTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of StorageManager
1 string reference to 'StorageManager'
1 service uses StorageManager
File
- src/
StorageManager.php, line 24
Namespace
Drupal\paragraph_view_modeView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
StorageManager:: |
protected | property | Entity type manager. | |
StorageManager:: |
protected | property | Entity form display storage. | |
StorageManager:: |
protected | property | Logger. | |
StorageManager:: |
public | function |
Add field to the given bundle. Overrides StorageManagerInterface:: |
|
StorageManager:: |
public | function |
Add paragraph view mode field to paragraph entity form display. Overrides StorageManagerInterface:: |
|
StorageManager:: |
protected | function | Create new field config. | |
StorageManager:: |
protected | function | Create field storage configuration entity. | |
StorageManager:: |
public | function |
Delete field from the given bundle. Overrides StorageManagerInterface:: |
|
StorageManager:: |
protected | function | Get field config. | |
StorageManager:: |
protected | function | Get field config storage. | |
StorageManager:: |
protected | function | Load existing field storage. | |
StorageManager:: |
public | function |
Sets field label. Overrides StorageManagerInterface:: |
|
StorageManager:: |
public | function | StorageManager constructor. | |
StorageManagerInterface:: |
constant | Module configuration name. | ||
StorageManagerInterface:: |
constant | Entity type name. | ||
StorageManagerInterface:: |
constant | View mode field label. | ||
StorageManagerInterface:: |
constant | View mode field name. | ||
StorageManagerInterface:: |
constant | View mode field type. | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |