You are here

protected function WishlistProvider::loadWishlistData in Commerce Wishlist 8.3

Loads the wishlist data for the given user.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user. If empty, the current user is assumed.

Return value

array The wishlist data.

2 calls to WishlistProvider::loadWishlistData()
WishlistProvider::getWishlistId in src/WishlistProvider.php
Gets the wishlist entity ID for the given user.
WishlistProvider::getWishlistIds in src/WishlistProvider.php
Gets all wishlist entity ids for the given user.

File

src/WishlistProvider.php, line 181

Class

WishlistProvider
Default implementation of the wishlist provider.

Namespace

Drupal\commerce_wishlist

Code

protected function loadWishlistData(AccountInterface $account = NULL) {
  $account = $account ?: $this->currentUser;
  $uid = $account
    ->id();
  if (isset($this->wishlistData[$uid])) {
    return $this->wishlistData[$uid];
  }
  if ($account
    ->isAuthenticated()) {
    $query = $this->wishlistStorage
      ->getQuery()
      ->condition('uid', $account
      ->id())
      ->sort('is_default', 'DESC')
      ->sort('wishlist_id', 'DESC')
      ->accessCheck(FALSE);
    $wishlist_ids = $query
      ->execute();
  }
  else {
    $wishlist_ids = $this->wishlistSession
      ->getWishlistIds();
  }
  $this->wishlistData[$uid] = [];
  if (!$wishlist_ids) {
    return [];
  }

  // Getting the wishlist data and validating the wishlist ids received from
  // the session requires loading the entities. This is a performance hit, but
  // it's assumed that these entities would be loaded at one point anyway.

  /** @var \Drupal\commerce_wishlist\Entity\WishlistInterface[] $wishlists */
  $wishlists = $this->wishlistStorage
    ->loadMultiple($wishlist_ids);
  foreach ($wishlists as $wishlist) {
    if ($wishlist
      ->getOwnerId() != $uid) {

      // Skip wishlists that are no longer eligible.
      continue;
    }
    $this->wishlistData[$uid][$wishlist
      ->id()] = [
      'type' => $wishlist
        ->bundle(),
    ];
  }
  return $this->wishlistData[$uid];
}