You are here

public function StorageManager::addToFormDisplay in Paragraph View Mode 8

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

Add paragraph view mode field to paragraph entity form display.

Parameters

string $bundle: Paragraph entity bundle.

string $form_mode: Form mode machine name.

Overrides StorageManagerInterface::addToFormDisplay

File

src/StorageManager.php, line 142

Class

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

Namespace

Drupal\paragraph_view_mode

Code

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,
    ]);
  }
}