WishlistController.php in Commerce Wishlist 8.3
File
src/Controller/WishlistController.php
View source
<?php
namespace Drupal\commerce_wishlist\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\commerce_wishlist\WishlistProviderInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class WishlistController implements ContainerInjectionInterface {
protected $configFactory;
protected $currentUser;
protected $entityTypeManager;
protected $formBuilder;
protected $routeMatch;
protected $wishlistProvider;
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, FormBuilderInterface $form_builder, RouteMatchInterface $route_match, WishlistProviderInterface $wishlist_provider) {
$this->configFactory = $config_factory;
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->formBuilder = $form_builder;
$this->routeMatch = $route_match;
$this->wishlistProvider = $wishlist_provider;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('form_builder'), $container
->get('current_route_match'), $container
->get('commerce_wishlist.wishlist_provider'));
}
public function wishlistPage() {
$wishlist = $this->wishlistProvider
->getWishlist($this
->getDefaultWishlistType());
if (!$wishlist || !$wishlist
->hasItems()) {
return [
'#theme' => 'commerce_wishlist_empty_page',
];
}
$rel = $this->currentUser
->isAuthenticated() ? 'user-form' : 'canonical';
$url = $wishlist
->toUrl($rel, [
'absolute' => TRUE,
]);
return new RedirectResponse($url
->toString());
}
public function userPage() {
$wishlist = $this->wishlistProvider
->getWishlist($this
->getDefaultWishlistType());
if (!$wishlist || !$wishlist
->hasItems()) {
return [
'#theme' => 'commerce_wishlist_empty_page',
];
}
$url = $wishlist
->toUrl('user-form', [
'absolute' => TRUE,
]);
return new RedirectResponse($url
->toString());
}
public function userForm() {
$form_object = $this
->getFormObject('user');
$form_state = new FormState();
return $this->formBuilder
->buildForm($form_object, $form_state);
}
public function shareForm() {
$form_object = $this
->getFormObject('share');
$form_state = new FormState();
return $this->formBuilder
->buildForm($form_object, $form_state);
}
protected function getFormObject($operation) {
$code = $this->routeMatch
->getRawParameter('code');
$wishlist_storage = $this->entityTypeManager
->getStorage('commerce_wishlist');
$wishlist = $wishlist_storage
->loadByCode($code);
if (!$wishlist) {
throw new NotFoundHttpException();
}
$form_object = $this->entityTypeManager
->getFormObject('commerce_wishlist', $operation);
$form_object
->setEntity($wishlist);
return $form_object;
}
protected function getDefaultWishlistType() {
return $this->configFactory
->get('commerce_wishlist.settings')
->get('default_type');
}
}