Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Product.php
Go to the documentation of this file.
1 <?php
7 
11 
18 {
24  protected $_coreRegistry = null;
25 
29  protected $_formKeyValidator;
30 
34  protected $sendFriend;
35 
39  protected $productRepository;
40 
48  public function __construct(
49  \Magento\Framework\App\Action\Context $context,
50  \Magento\Framework\Registry $coreRegistry,
51  \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
52  \Magento\SendFriend\Model\SendFriend $sendFriend,
53  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
54  ) {
55  parent::__construct($context);
56  $this->_coreRegistry = $coreRegistry;
57  $this->_formKeyValidator = $formKeyValidator;
58  $this->sendFriend = $sendFriend;
59  $this->productRepository = $productRepository;
60  }
61 
71  {
72  /* @var $helper \Magento\SendFriend\Helper\Data */
73  $helper = $this->_objectManager->get(\Magento\SendFriend\Helper\Data::class);
74  /* @var $session \Magento\Customer\Model\Session */
75  $session = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
76 
77  if (!$helper->isEnabled()) {
78  throw new NotFoundException(__('Page not found.'));
79  }
80 
81  if (!$helper->isAllowForGuest() && !$session->authenticate()) {
82  $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
83  if ($this->getRequest()->getActionName() == 'sendemail') {
84  $session->setBeforeAuthUrl($this->_url->getUrl('sendfriend/product/send', ['_current' => true]));
85  $this->_objectManager->get(\Magento\Catalog\Model\Session::class)
86  ->setSendfriendFormData($request->getPostValue());
87  }
88  }
89  return parent::dispatch($request);
90  }
91 
97  protected function _initProduct()
98  {
99  $productId = (int)$this->getRequest()->getParam('id');
100  if (!$productId) {
101  return false;
102  }
103  try {
104  $product = $this->productRepository->getById($productId);
105  if (!$product->isVisibleInCatalog()) {
106  return false;
107  }
108  } catch (NoSuchEntityException $noEntityException) {
109  return false;
110  }
111 
112  $this->_coreRegistry->register('product', $product);
113  return $product;
114  }
115 }
$helper
Definition: iframe.phtml:13
__construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator, \Magento\SendFriend\Model\SendFriend $sendFriend, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository)
Definition: Product.php:48
__()
Definition: __.php:13
dispatch(RequestInterface $request)
Definition: Product.php:70