You are here

public function MailingList::postSave in Mailing List 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/MailingList.php, line 231

Class

MailingList
Defines the mailing list configuration entity.

Namespace

Drupal\mailing_list\Entity

Code

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

    // Update subscriptions mailing list.
    if ($this
      ->getOriginalId() != $this
      ->id()) {
      $update_count = $this
        ->entityTypeManager()
        ->getStorage('subscription')
        ->updateType($this
        ->getOriginalId(), $this
        ->id());
      if ($update_count) {
        drupal_set_message(\Drupal::translation()
          ->formatPlural($update_count, 'Changed the mailing list of 1 subscription from %old-type to %type.', 'Changed the mailing list of @count subscriptions from %old-type to %type.', [
          '%old-type' => $this
            ->getOriginalId(),
          '%type' => $this
            ->id(),
        ]));
      }
    }
  }
  else {

    // Create block form display mode.

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $block_display_mode */
    $block_display_mode = $this
      ->entityTypeManager()
      ->getStorage('entity_form_display')
      ->create([
      'id' => 'mailing_list_subscription.' . $this
        ->id() . '.block',
      'targetEntityType' => 'mailing_list_subscription',
      'bundle' => $this
        ->id(),
      'mode' => 'block',
      'status' => TRUE,
    ]);

    // Disable admin components.
    $block_display_mode
      ->removeComponent('uid')
      ->removeComponent('created')
      ->removeComponent('status')
      ->removeComponent('langcode')
      ->save();
  }
}