Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
View.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\View\Result\Page as ResultPage;
10 
16 {
22  protected $messageGroups;
23 
29  protected $_coreRegistry = null;
30 
36  protected $_catalogProduct = null;
37 
43  protected $_catalogDesign;
44 
50  protected $_catalogSession;
51 
55  protected $messageManager;
56 
61 
65  private $string;
66 
80  public function __construct(
81  \Magento\Framework\App\Helper\Context $context,
82  \Magento\Catalog\Model\Session $catalogSession,
83  \Magento\Catalog\Model\Design $catalogDesign,
84  \Magento\Catalog\Helper\Product $catalogProduct,
85  \Magento\Framework\Registry $coreRegistry,
86  \Magento\Framework\Message\ManagerInterface $messageManager,
87  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
88  array $messageGroups = [],
89  \Magento\Framework\Stdlib\StringUtils $string = null
90  ) {
91  $this->_catalogSession = $catalogSession;
92  $this->_catalogDesign = $catalogDesign;
93  $this->_catalogProduct = $catalogProduct;
94  $this->_coreRegistry = $coreRegistry;
95  $this->messageGroups = $messageGroups;
96  $this->messageManager = $messageManager;
97  $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
98  $this->string = $string ?: \Magento\Framework\App\ObjectManager::getInstance()
99  ->get(\Magento\Framework\Stdlib\StringUtils::class);
100  parent::__construct($context);
101  }
102 
110  private function preparePageMetadata(ResultPage $resultPage, $product)
111  {
112  $pageLayout = $resultPage->getLayout();
113  $pageConfig = $resultPage->getConfig();
114 
115  $metaTitle = $product->getMetaTitle();
116  $pageConfig->setMetaTitle($metaTitle);
117  $pageConfig->getTitle()->set($metaTitle ?: $product->getName());
118 
119  $keyword = $product->getMetaKeyword();
120  $currentCategory = $this->_coreRegistry->registry('current_category');
121  if ($keyword) {
122  $pageConfig->setKeywords($keyword);
123  } elseif ($currentCategory) {
124  $pageConfig->setKeywords($product->getName());
125  }
126 
127  $description = $product->getMetaDescription();
128  if ($description) {
129  $pageConfig->setDescription($description);
130  } else {
131  $pageConfig->setDescription($this->string->substr(strip_tags($product->getDescription()), 0, 255));
132  }
133 
134  if ($this->_catalogProduct->canUseCanonicalTag()) {
135  $pageConfig->addRemotePageAsset(
136  $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]),
137  'canonical',
138  ['attributes' => ['rel' => 'canonical']]
139  );
140  }
141 
142  $pageMainTitle = $pageLayout->getBlock('page.main.title');
143  if ($pageMainTitle) {
144  $pageMainTitle->setPageTitle($product->getName());
145  }
146 
147  return $this;
148  }
149 
160  public function initProductLayout(ResultPage $resultPage, $product, $params = null)
161  {
162  $settings = $this->_catalogDesign->getDesignSettings($product);
163  $pageConfig = $resultPage->getConfig();
164 
165  if ($settings->getCustomDesign()) {
166  $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
167  }
168 
169  // Apply custom page layout
170  if ($settings->getPageLayout()) {
171  $pageConfig->setPageLayout($settings->getPageLayout());
172  }
173 
174  $urlSafeSku = rawurlencode($product->getSku());
175 
176  // Load default page handles and page configurations
177  if ($params && $params->getBeforeHandles()) {
178  foreach ($params->getBeforeHandles() as $handle) {
179  $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false);
180  $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle);
181  }
182  }
183 
184  $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], null, false);
185  $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku]);
186 
187  if ($params && $params->getAfterHandles()) {
188  foreach ($params->getAfterHandles() as $handle) {
189  $resultPage->addPageLayoutHandles(['type' => $product->getTypeId()], $handle, false);
190  $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku], $handle);
191  }
192  }
193 
194  // Apply custom layout update once layout is loaded
195  $update = $resultPage->getLayout()->getUpdate();
196  $layoutUpdates = $settings->getLayoutUpdates();
197  if ($layoutUpdates) {
198  if (is_array($layoutUpdates)) {
199  foreach ($layoutUpdates as $layoutUpdate) {
200  $update->addUpdate($layoutUpdate);
201  }
202  }
203  }
204 
205  $currentCategory = $this->_coreRegistry->registry('current_category');
206  $controllerClass = $this->_request->getFullActionName();
207  if ($controllerClass != 'catalog-product-view') {
208  $pageConfig->addBodyClass('catalog-product-view');
209  }
210  $pageConfig->addBodyClass('product-' . $product->getUrlKey());
211  if ($currentCategory instanceof \Magento\Catalog\Model\Category) {
212  $pageConfig->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($currentCategory))
213  ->addBodyClass('category-' . $currentCategory->getUrlKey());
214  }
215 
216  return $this;
217  }
218 
236  public function prepareAndRender(ResultPage $resultPage, $productId, $controller, $params = null)
237  {
243  $defaultActionHandle = $resultPage->getDefaultLayoutHandle();
244  $handles = $resultPage->getLayout()->getUpdate()->getHandles();
245  if (in_array($defaultActionHandle, $handles)) {
246  $resultPage->getLayout()->getUpdate()->removeHandle($resultPage->getDefaultLayoutHandle());
247  }
248 
249  if (!$controller instanceof \Magento\Catalog\Controller\Product\View\ViewInterface) {
250  throw new \Magento\Framework\Exception\LocalizedException(
251  __('Bad controller interface for showing product')
252  );
253  }
254  // Prepare data
255  $productHelper = $this->_catalogProduct;
256  if (!$params) {
257  $params = new \Magento\Framework\DataObject();
258  }
259 
260  // Standard algorithm to prepare and render product view page
261  $product = $productHelper->initProduct($productId, $controller, $params);
262  if (!$product) {
263  throw new \Magento\Framework\Exception\NoSuchEntityException(__('Product is not loaded'));
264  }
265 
266  $buyRequest = $params->getBuyRequest();
267  if ($buyRequest) {
268  $productHelper->prepareProductOptions($product, $buyRequest);
269  }
270 
271  if ($params->hasConfigureMode()) {
272  $product->setConfigureMode($params->getConfigureMode());
273  }
274 
275  $this->_eventManager->dispatch('catalog_controller_product_view', ['product' => $product]);
276 
277  $this->_catalogSession->setLastViewedProductId($product->getId());
278 
279  if (in_array($defaultActionHandle, $handles)) {
280  $resultPage->addDefaultHandle();
281  }
282 
283  $this->initProductLayout($resultPage, $product, $params);
284  $this->preparePageMetadata($resultPage, $product);
285  return $this;
286  }
287 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
$settings
Definition: bootstrap.php:29
initProductLayout(ResultPage $resultPage, $product, $params=null)
Definition: View.php:160
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\Session $catalogSession, \Magento\Catalog\Model\Design $catalogDesign, \Magento\Catalog\Helper\Product $catalogProduct, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, array $messageGroups=[], \Magento\Framework\Stdlib\StringUtils $string=null)
Definition: View.php:80
$handle
$controller
Definition: info.phtml:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
prepareAndRender(ResultPage $resultPage, $productId, $controller, $params=null)
Definition: View.php:236