You are here

public function CustomPublishingOption::postSave in Custom Publishing Options 8

Commit the save as normal, and create or update corresponding field as necessary.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage:

bool $update:

Overrides EntityBase::postSave

File

src/Entity/CustomPublishingOption.php, line 100

Class

CustomPublishingOption
Defines the Custom publishing option entity.

Namespace

Drupal\custom_pub\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);
  $storage_definition = \Drupal::entityDefinitionUpdateManager()
    ->getFieldStorageDefinition($this
    ->id(), 'node');
  if (!isset($storage_definition)) {

    // create the field definition for the node
    $storage_definition = BaseFieldDefinition::create('boolean')
      ->setLabel(t('@label', [
      '@label' => $this
        ->label(),
    ]))
      ->setDescription(t('@description', [
      '@description' => $this
        ->getDescription(),
    ]))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE)
      ->setDefaultValue(FALSE)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'settings' => [
        'display_label' => TRUE,
      ],
      'weight' => 20,
    ]);
    \Drupal::entityDefinitionUpdateManager()
      ->installFieldStorageDefinition($this
      ->id(), 'node', 'custom_pub', $storage_definition);
  }
  else {

    // update the label and description on the definition
    $storage_definition
      ->setLabel(t('@label', [
      '@label' => $this
        ->label(),
    ]))
      ->setDescription(t('@description', [
      '@description' => $this
        ->getDescription(),
    ]));
    \Drupal::entityDefinitionUpdateManager()
      ->updateFieldStorageDefinition($storage_definition);
  }
  \Drupal::cache('config')
    ->deleteAll();
}