You are here

function uc_wishlist_create_wishlist in UC Wish List 8

Same name and namespace in other branches
  1. 6 uc_wishlist.module \uc_wishlist_create_wishlist()

Creates a new wishlist for the current authenticated or anonymous user.

1 call to uc_wishlist_create_wishlist()
uc_wishlist_add_item in ./uc_wishlist.module
Adds an item to a user's wish list.

File

./uc_wishlist.module, line 195
Allows users to create public shopping/wish lists.

Code

function uc_wishlist_create_wishlist($title = NULL) {
  $user = \Drupal::currentUser();

  // Abort if user is not logged in and anonymous wish lists are not allowed.
  if (!$user
    ->id() && !$user
    ->hasPermission('create wish lists')) {
    drupal_set_message(t('You must be logged in to create a wish list. Please <a href="@login_url">login</a> or <a href="@register_url">register</a>.', [
      '@login_url' => Url::fromRoute('user.login'),
      '@register_url' => Url::fromRoute('user.register'),
    ]));
    return FALSE;
  }

  // Get the current user ID for the wish list.
  $uid = uc_wishlist_get_uid();
  $config = \Drupal::config('uc_wishlist.settings');
  if (empty($title)) {
    $title = $config
      ->get('uc_wishlist_default_title');
  }
  $title = '%user\'s wish list.';
  if (strpos($title, '%user') !== FALSE) {
    $uname = $user
      ->getAccountName();
    $title = str_replace('%user', $uname, $title);
  }
  $expiration = REQUEST_TIME + $config
    ->get('uc_wishlist_default_length');
  $private = $config
    ->get('uc_wishlist_default_private') ? $config
    ->get('uc_wishlist_default_private') : 0;
  $wishlist_manager = \Drupal::service('uc_wishlist.manager');
  $fields = [
    'uid',
    'title',
    'expiration',
    'private',
  ];
  $values = [
    $uid,
    $title,
    $expiration,
    $private,
  ];
  $wishlist_manager = \Drupal::service('uc_wishlist.manager');
  $result = $id = $wishlist_manager
    ->createWishlist($fields, $values);
  if ($result) {
    return $id;
  }
  return FALSE;
}