You are here

public function Wishlist::postSave in Commerce Wishlist 8.3

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 ContentEntityBase::postSave

File

src/Entity/Wishlist.php, line 342

Class

Wishlist
Defines the wishlist entity class.

Namespace

Drupal\commerce_wishlist\Entity

Code

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

  /** @var \Drupal\commerce_wishlist\WishlistStorageInterface $storage */
  parent::postSave($storage, $update);

  // Ensure there's a back-reference on each wishlist item.
  foreach ($this
    ->getItems() as $wishlist_item) {
    if ($wishlist_item->wishlist_id
      ->isEmpty()) {
      $wishlist_item->wishlist_id = $this
        ->id();
      $wishlist_item
        ->save();
    }
  }
  if ($this
    ->getOwnerId()) {
    $default = $this
      ->isDefault();
    $original_default = $this->original ? $this->original
      ->isDefault() : FALSE;
    if ($default && !$original_default) {
      $wishlists = $storage
        ->loadMultipleByUser($this
        ->getOwner(), $this
        ->bundle());
      foreach ($wishlists as $wishlist) {
        if ($wishlist
          ->id() != $this
          ->id() && $wishlist
          ->isDefault()) {
          $wishlist
            ->setDefault(FALSE);
          $wishlist
            ->save();
        }
      }
    }
  }
}