You are here

public function TransactionType::postSave in Transaction 8

Acts on a saved entity before the insert or update hook is invoked.

Used after the entity is saved, but before invoking the insert or update hook. Note that in case of translatable content entities this callback is only fired on their current translation. It is up to the developer to iterate over all translations if needed.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.

Overrides ConfigEntityBundleBase::postSave

File

src/Entity/TransactionType.php, line 257

Class

TransactionType
Provides a type of transaction.

Namespace

Drupal\transaction\Entity

Code

public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  parent::postSave($storage, $update);

  // Following only applies to transaction type creation outside config sync.
  if ($update || \Drupal::isConfigSyncing()) {
    return;
  }

  // Create the list view display mode if it does not exist.
  $entity_display_id = 'transaction.' . $this
    ->id() . '.list';
  $entity_view_display_storage = $this
    ->entityTypeManager()
    ->getStorage('entity_view_display');
  if ($entity_view_display_storage
    ->load($entity_display_id)) {
    return;
  }

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $list_display_mode */
  $list_display_mode = $entity_view_display_storage
    ->create([
    'id' => $entity_display_id,
    'targetEntityType' => 'transaction',
    'bundle' => $this
      ->id(),
    'mode' => 'list',
    'status' => TRUE,
  ]);

  // Hide labels for all components in list view mode, table header used
  // instead.
  foreach ($list_display_mode
    ->getComponents() as $name => $component) {
    if (isset($component['label']) && $component['label'] != 'hidden') {
      $component['label'] = 'hidden';
      $list_display_mode
        ->setComponent($name, $component);
    }
  }
  $list_display_mode
    ->save();
}