Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit b8cf2d5

Browse files
slopukhovduhon
authored andcommitted
MAGETWO-87612: Product View Block initialization isn't optimal
1 parent 3aa0a16 commit b8cf2d5

File tree

4 files changed

+68
-62
lines changed

4 files changed

+68
-62
lines changed

app/code/Magento/Catalog/Block/Product/View.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -120,51 +120,6 @@ public function getWishlistOptions()
120120
return ['productType' => $this->getProduct()->getTypeId()];
121121
}
122122

123-
/**
124-
* Add meta information from product to head block
125-
*
126-
* @return \Magento\Catalog\Block\Product\View
127-
*/
128-
protected function _prepareLayout()
129-
{
130-
$this->getLayout()->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);
131-
$product = $this->getProduct();
132-
if (!$product) {
133-
return parent::_prepareLayout();
134-
}
135-
136-
$title = $product->getMetaTitle();
137-
if ($title) {
138-
$this->pageConfig->getTitle()->set($title);
139-
}
140-
$keyword = $product->getMetaKeyword();
141-
$currentCategory = $this->_coreRegistry->registry('current_category');
142-
if ($keyword) {
143-
$this->pageConfig->setKeywords($keyword);
144-
} elseif ($currentCategory) {
145-
$this->pageConfig->setKeywords($product->getName());
146-
}
147-
$description = $product->getMetaDescription();
148-
if ($description) {
149-
$this->pageConfig->setDescription($description);
150-
} else {
151-
$this->pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 255));
152-
}
153-
if ($this->_productHelper->canUseCanonicalTag()) {
154-
$this->pageConfig->addRemotePageAsset(
155-
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]),
156-
'canonical',
157-
['attributes' => ['rel' => 'canonical']]
158-
);
159-
}
160-
161-
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
162-
if ($pageMainTitle) {
163-
$pageMainTitle->setPageTitle($product->getName());
164-
}
165-
return parent::_prepareLayout();
166-
}
167-
168123
/**
169124
* Retrieve current product model
170125
*

app/code/Magento/Catalog/Helper/Product/View.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class View extends \Magento\Framework\App\Helper\AbstractHelper
5959
*/
6060
protected $categoryUrlPathGenerator;
6161

62+
/**
63+
* @var \Magento\Framework\Stdlib\StringUtils
64+
*/
65+
private $string;
66+
6267
/**
6368
* Constructor
6469
*
@@ -70,6 +75,7 @@ class View extends \Magento\Framework\App\Helper\AbstractHelper
7075
* @param \Magento\Framework\Message\ManagerInterface $messageManager
7176
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
7277
* @param array $messageGroups
78+
* @param \Magento\Framework\Stdlib\StringUtils|null $string
7379
*/
7480
public function __construct(
7581
\Magento\Framework\App\Helper\Context $context,
@@ -79,7 +85,8 @@ public function __construct(
7985
\Magento\Framework\Registry $coreRegistry,
8086
\Magento\Framework\Message\ManagerInterface $messageManager,
8187
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
82-
array $messageGroups = []
88+
array $messageGroups = [],
89+
\Magento\Framework\Stdlib\StringUtils $string = null
8390
) {
8491
$this->_catalogSession = $catalogSession;
8592
$this->_catalogDesign = $catalogDesign;
@@ -88,9 +95,61 @@ public function __construct(
8895
$this->messageGroups = $messageGroups;
8996
$this->messageManager = $messageManager;
9097
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
98+
$this->string = $string ?: \Magento\Framework\App\ObjectManager::getInstance()
99+
->get(\Magento\Framework\Stdlib\StringUtils::class);
91100
parent::__construct($context);
92101
}
93102

103+
/**
104+
* Add meta information from product to layout
105+
*
106+
* @param \Magento\Framework\View\Result\Page $resultPage
107+
* @param \Magento\Catalog\Model\Product $product
108+
* @return \Magento\Framework\View\Result\Page
109+
*/
110+
private function preparePageMetadata(ResultPage $resultPage, $product)
111+
{
112+
$pageLayout = $resultPage->getLayout();
113+
$pageLayout->createBlock(\Magento\Catalog\Block\Breadcrumbs::class);
114+
115+
$pageConfig = $resultPage->getConfig();
116+
117+
$title = $product->getMetaTitle();
118+
if ($title) {
119+
$pageConfig->getTitle()->set($title);
120+
}
121+
122+
$keyword = $product->getMetaKeyword();
123+
$currentCategory = $this->_coreRegistry->registry('current_category');
124+
if ($keyword) {
125+
$pageConfig->setKeywords($keyword);
126+
} elseif ($currentCategory) {
127+
$pageConfig->setKeywords($product->getName());
128+
}
129+
130+
$description = $product->getMetaDescription();
131+
if ($description) {
132+
$pageConfig->setDescription($description);
133+
} else {
134+
$pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 255));
135+
}
136+
137+
if ($this->_catalogProduct->canUseCanonicalTag()) {
138+
$pageConfig->addRemotePageAsset(
139+
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]),
140+
'canonical',
141+
['attributes' => ['rel' => 'canonical']]
142+
);
143+
}
144+
145+
$pageMainTitle = $pageLayout->getBlock('page.main.title');
146+
if ($pageMainTitle) {
147+
$pageMainTitle->setPageTitle($product->getName());
148+
}
149+
150+
return $this;
151+
}
152+
94153
/**
95154
* Init layout for viewing product page
96155
*
@@ -225,6 +284,7 @@ public function prepareAndRender(ResultPage $resultPage, $productId, $controller
225284
}
226285

227286
$this->initProductLayout($resultPage, $product, $params);
287+
$this->preparePageMetadata($resultPage, $product);
228288
return $this;
229289
}
230290
}

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ViewTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,10 @@ public function testSetLayout()
4141

4242
/** @var $layout \Magento\Framework\View\Layout */
4343
$layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
44-
/** @var $pageConfig \Magento\Framework\View\Page\Config */
45-
$pageConfig = $objectManager->get(\Magento\Framework\View\Page\Config::class);
4644

47-
$layout->createBlock(\Magento\Catalog\Block\Product\View::class);
45+
$productView = $layout->createBlock(\Magento\Catalog\Block\Product\View::class);
4846

49-
$this->assertNotEmpty($pageConfig->getTitle()->get());
50-
$this->assertEquals($this->_product->getMetaTitle(), $pageConfig->getTitle()->get());
51-
$this->assertEquals($this->_product->getMetaKeyword(), $pageConfig->getKeywords());
52-
$this->assertEquals($this->_product->getMetaDescription(), $pageConfig->getDescription());
47+
$this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $productView->getLayout());
5348
}
5449

5550
public function testGetProduct()

lib/internal/Magento/Framework/Stdlib/StringUtils.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function split($value, $length = 1, $keepWords = false, $trim = false, $w
148148
*/
149149
public function strlen($string)
150150
{
151-
return iconv_strlen($string, self::ICONV_CHARSET);
151+
return mb_strlen($string, self::ICONV_CHARSET);
152152
}
153153

154154
/**
@@ -159,15 +159,11 @@ public function strlen($string)
159159
*/
160160
public function cleanString($string)
161161
{
162-
if ('"libiconv"' == ICONV_IMPL) {
163-
return iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string);
164-
} else {
165-
return $string;
166-
}
162+
return mb_convert_encoding($string, self::ICONV_CHARSET);
167163
}
168164

169165
/**
170-
* Pass through to iconv_substr()
166+
* Pass through to mb_substr()
171167
*
172168
* @param string $string
173169
* @param int $offset
@@ -180,7 +176,7 @@ public function substr($string, $offset, $length = null)
180176
if ($length === null) {
181177
$length = $this->strlen($string) - $offset;
182178
}
183-
return iconv_substr($string, $offset, $length, self::ICONV_CHARSET);
179+
return mb_substr($string, $offset, $length, self::ICONV_CHARSET);
184180
}
185181

186182
/**
@@ -212,6 +208,6 @@ public function strrev($str)
212208
*/
213209
public function strpos($haystack, $needle, $offset = null)
214210
{
215-
return iconv_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
211+
return mb_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
216212
}
217213
}

0 commit comments

Comments
 (0)