Skip to content

Commit 0aa5522

Browse files
author
Dmytro Voskoboinikov
committed
Merge branch '2.2-develop' into 2.2.4-PR-20170111
2 parents 3feed10 + 6790f9a commit 0aa5522

File tree

4 files changed

+65
-61
lines changed

4 files changed

+65
-61
lines changed

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -120,50 +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-
$product = $this->getProduct();
131-
if (!$product) {
132-
return parent::_prepareLayout();
133-
}
134-
135-
$title = $product->getMetaTitle();
136-
if ($title) {
137-
$this->pageConfig->getTitle()->set($title);
138-
}
139-
$keyword = $product->getMetaKeyword();
140-
$currentCategory = $this->_coreRegistry->registry('current_category');
141-
if ($keyword) {
142-
$this->pageConfig->setKeywords($keyword);
143-
} elseif ($currentCategory) {
144-
$this->pageConfig->setKeywords($product->getName());
145-
}
146-
$description = $product->getMetaDescription();
147-
if ($description) {
148-
$this->pageConfig->setDescription($description);
149-
} else {
150-
$this->pageConfig->setDescription($this->string->substr($product->getDescription(), 0, 255));
151-
}
152-
if ($this->_productHelper->canUseCanonicalTag()) {
153-
$this->pageConfig->addRemotePageAsset(
154-
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]),
155-
'canonical',
156-
['attributes' => ['rel' => 'canonical']]
157-
);
158-
}
159-
160-
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
161-
if ($pageMainTitle) {
162-
$pageMainTitle->setPageTitle($product->getName());
163-
}
164-
return parent::_prepareLayout();
165-
}
166-
167123
/**
168124
* Retrieve current product model
169125
*

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

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

64+
/**
65+
* @var \Magento\Framework\Stdlib\StringUtils
66+
*/
67+
private $string;
68+
6469
/**
6570
* Constructor
6671
*
@@ -72,6 +77,7 @@ class View extends \Magento\Framework\App\Helper\AbstractHelper
7277
* @param \Magento\Framework\Message\ManagerInterface $messageManager
7378
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
7479
* @param array $messageGroups
80+
* @param \Magento\Framework\Stdlib\StringUtils|null $string
7581
*/
7682
public function __construct(
7783
\Magento\Framework\App\Helper\Context $context,
@@ -81,7 +87,8 @@ public function __construct(
8187
\Magento\Framework\Registry $coreRegistry,
8288
\Magento\Framework\Message\ManagerInterface $messageManager,
8389
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
84-
array $messageGroups = []
90+
array $messageGroups = [],
91+
\Magento\Framework\Stdlib\StringUtils $string = null
8592
) {
8693
$this->_catalogSession = $catalogSession;
8794
$this->_catalogDesign = $catalogDesign;
@@ -90,9 +97,58 @@ public function __construct(
9097
$this->messageGroups = $messageGroups;
9198
$this->messageManager = $messageManager;
9299
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
100+
$this->string = $string ?: \Magento\Framework\App\ObjectManager::getInstance()
101+
->get(\Magento\Framework\Stdlib\StringUtils::class);
93102
parent::__construct($context);
94103
}
95104

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

229285
$this->initProductLayout($resultPage, $product, $params);
286+
$this->preparePageMetadata($resultPage, $product);
230287
return $this;
231288
}
232289
}

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)