You are here

public function StorageManager::addField in Paragraph View Mode 2.x

Same name and namespace in other branches
  1. 8 src/StorageManager.php \Drupal\paragraph_view_mode\StorageManager::addField()

Add field to the given bundle.

Parameters

string $bundle: Paragraph entity bundle.

Return value

bool True if the field exist or was created successfully.

Overrides StorageManagerInterface::addField

File

src/StorageManager.php, line 68

Class

StorageManager
Provides fields and forms storage operations required by the module.

Namespace

Drupal\paragraph_view_mode

Code

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;
  }
}