Skip to content

Commit b5e21b6

Browse files
ENGCOM-1911: FIX for issue #15501 - M2.2.4 missing meta title tag and doesn't show… #15532
2 parents 8bd693b + 34c6de2 commit b5e21b6

File tree

4 files changed

+100
-46
lines changed

4 files changed

+100
-46
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ private function preparePageMetadata(ResultPage $resultPage, $product)
113113
{
114114
$pageConfig = $resultPage->getConfig();
115115

116-
$title = $product->getMetaTitle();
117-
if ($title) {
118-
$pageConfig->getTitle()->set($title);
119-
}
116+
$metaTitle = $product->getMetaTitle();
117+
$pageConfig->setMetaTitle($metaTitle);
118+
$pageConfig->getTitle()->set($metaTitle ?: $product->getName());
120119

121120
$keyword = $product->getMetaKeyword();
122121
$currentCategory = $this->_coreRegistry->registry('current_category');

lib/internal/Magento/Framework/View/Page/Config.php

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Framework\View\Page;
88

99
use Magento\Framework\App;
10+
use Magento\Framework\App\Area;
1011
use Magento\Framework\View;
1112

1213
/**
@@ -34,6 +35,15 @@ class Config
3435
const ELEMENT_TYPE_HEAD = 'head';
3536
/**#@-*/
3637

38+
const META_DESCRIPTION = 'description';
39+
const META_CONTENT_TYPE = 'content_type';
40+
const META_MEDIA_TYPE = 'media_type';
41+
const META_CHARSET = 'charset';
42+
const META_TITLE = 'title';
43+
const META_KEYWORDS = 'keywords';
44+
const META_ROBOTS = 'robots';
45+
const META_X_UI_COMPATIBLE = 'x_ua_compatible';
46+
3747
/**
3848
* Constant body attribute class
3949
*/
@@ -244,7 +254,7 @@ public function getMetadata()
244254
*/
245255
public function setContentType($contentType)
246256
{
247-
$this->setMetadata('content_type', $contentType);
257+
$this->setMetadata(self::META_CONTENT_TYPE, $contentType);
248258
}
249259

250260
/**
@@ -255,10 +265,10 @@ public function setContentType($contentType)
255265
public function getContentType()
256266
{
257267
$this->build();
258-
if (strtolower($this->metadata['content_type']) === 'auto') {
259-
$this->metadata['content_type'] = $this->getMediaType() . '; charset=' . $this->getCharset();
268+
if (strtolower($this->metadata[self::META_CONTENT_TYPE]) === 'auto') {
269+
$this->metadata[self::META_CONTENT_TYPE] = $this->getMediaType() . '; charset=' . $this->getCharset();
260270
}
261-
return $this->metadata['content_type'];
271+
return $this->metadata[self::META_CONTENT_TYPE];
262272
}
263273

264274
/**
@@ -267,7 +277,7 @@ public function getContentType()
267277
*/
268278
public function setMediaType($mediaType)
269279
{
270-
$this->setMetadata('media_type', $mediaType);
280+
$this->setMetadata(self::META_MEDIA_TYPE, $mediaType);
271281
}
272282

273283
/**
@@ -278,13 +288,13 @@ public function setMediaType($mediaType)
278288
public function getMediaType()
279289
{
280290
$this->build();
281-
if (empty($this->metadata['media_type'])) {
282-
$this->metadata['media_type'] = $this->scopeConfig->getValue(
291+
if (empty($this->metadata[self::META_MEDIA_TYPE])) {
292+
$this->metadata[self::META_MEDIA_TYPE] = $this->scopeConfig->getValue(
283293
'design/head/default_media_type',
284294
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
285295
);
286296
}
287-
return $this->metadata['media_type'];
297+
return $this->metadata[self::META_MEDIA_TYPE];
288298
}
289299

290300
/**
@@ -293,7 +303,7 @@ public function getMediaType()
293303
*/
294304
public function setCharset($charset)
295305
{
296-
$this->setMetadata('charset', $charset);
306+
$this->setMetadata(self::META_CHARSET, $charset);
297307
}
298308

299309
/**
@@ -304,13 +314,13 @@ public function setCharset($charset)
304314
public function getCharset()
305315
{
306316
$this->build();
307-
if (empty($this->metadata['charset'])) {
308-
$this->metadata['charset'] = $this->scopeConfig->getValue(
317+
if (empty($this->metadata[self::META_CHARSET])) {
318+
$this->metadata[self::META_CHARSET] = $this->scopeConfig->getValue(
309319
'design/head/default_charset',
310320
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
311321
);
312322
}
313-
return $this->metadata['charset'];
323+
return $this->metadata[self::META_CHARSET];
314324
}
315325

316326
/**
@@ -319,7 +329,7 @@ public function getCharset()
319329
*/
320330
public function setDescription($description)
321331
{
322-
$this->setMetadata('description', $description);
332+
$this->setMetadata(self::META_DESCRIPTION, $description);
323333
}
324334

325335
/**
@@ -330,13 +340,36 @@ public function setDescription($description)
330340
public function getDescription()
331341
{
332342
$this->build();
333-
if (empty($this->metadata['description'])) {
334-
$this->metadata['description'] = $this->scopeConfig->getValue(
343+
if (empty($this->metadata[self::META_DESCRIPTION])) {
344+
$this->metadata[self::META_DESCRIPTION] = $this->scopeConfig->getValue(
335345
'design/head/default_description',
336346
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
337347
);
338348
}
339-
return $this->metadata['description'];
349+
return $this->metadata[self::META_DESCRIPTION];
350+
}
351+
352+
/**
353+
* @param string $title
354+
*/
355+
public function setMetaTitle($title)
356+
{
357+
$this->setMetadata(self::META_TITLE, $title);
358+
}
359+
360+
/**
361+
* Retrieve meta title
362+
*
363+
* @return string
364+
*/
365+
public function getMetaTitle()
366+
{
367+
$this->build();
368+
if (empty($this->metadata[self::META_TITLE])) {
369+
return '';
370+
}
371+
372+
return $this->metadata[self::META_TITLE];
340373
}
341374

342375
/**
@@ -345,7 +378,7 @@ public function getDescription()
345378
*/
346379
public function setKeywords($keywords)
347380
{
348-
$this->setMetadata('keywords', $keywords);
381+
$this->setMetadata(self::META_KEYWORDS, $keywords);
349382
}
350383

351384
/**
@@ -356,13 +389,13 @@ public function setKeywords($keywords)
356389
public function getKeywords()
357390
{
358391
$this->build();
359-
if (empty($this->metadata['keywords'])) {
360-
$this->metadata['keywords'] = $this->scopeConfig->getValue(
392+
if (empty($this->metadata[self::META_KEYWORDS])) {
393+
$this->metadata[self::META_KEYWORDS] = $this->scopeConfig->getValue(
361394
'design/head/default_keywords',
362395
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
363396
);
364397
}
365-
return $this->metadata['keywords'];
398+
return $this->metadata[self::META_KEYWORDS];
366399
}
367400

368401
/**
@@ -371,27 +404,28 @@ public function getKeywords()
371404
*/
372405
public function setRobots($robots)
373406
{
374-
$this->setMetadata('robots', $robots);
407+
$this->setMetadata(self::META_ROBOTS, $robots);
375408
}
376409

377410
/**
378411
* Retrieve URL to robots file
379412
*
380413
* @return string
414+
* @throws \Magento\Framework\Exception\LocalizedException
381415
*/
382416
public function getRobots()
383417
{
384-
if ($this->getAreaResolver()->getAreaCode() !== 'frontend') {
418+
if ($this->getAreaResolver()->getAreaCode() !== Area::AREA_FRONTEND) {
385419
return 'NOINDEX,NOFOLLOW';
386420
}
387421
$this->build();
388-
if (empty($this->metadata['robots'])) {
389-
$this->metadata['robots'] = $this->scopeConfig->getValue(
422+
if (empty($this->metadata[self::META_ROBOTS])) {
423+
$this->metadata[self::META_ROBOTS] = $this->scopeConfig->getValue(
390424
'design/search_engine_robots/default_robots',
391425
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
392426
);
393427
}
394-
return $this->metadata['robots'];
428+
return $this->metadata[self::META_ROBOTS];
395429
}
396430

397431
/**

lib/internal/Magento/Framework/View/Page/Config/Renderer.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\View\Page\Config;
78

9+
use Magento\Framework\Exception\LocalizedException;
810
use Magento\Framework\View\Asset\GroupedCollection;
911
use Magento\Framework\View\Page\Config;
1012

@@ -21,7 +23,7 @@ class Renderer implements RendererInterface
2123
protected $assetTypeOrder = ['css', 'ico', 'js'];
2224

2325
/**
24-
* @var \Magento\Framework\View\Page\Config
26+
* @var Config
2527
*/
2628
protected $pageConfig;
2729

@@ -51,7 +53,7 @@ class Renderer implements RendererInterface
5153
protected $urlBuilder;
5254

5355
/**
54-
* @param \Magento\Framework\View\Page\Config $pageConfig
56+
* @param Config $pageConfig
5557
* @param \Magento\Framework\View\Asset\MergeService $assetMergeService
5658
* @param \Magento\Framework\UrlInterface $urlBuilder
5759
* @param \Magento\Framework\Escaper $escaper
@@ -131,15 +133,35 @@ public function renderMetadata()
131133
/**
132134
* @param string $name
133135
* @param string $content
134-
* @return mixed
136+
* @return string
135137
*/
136138
protected function processMetadataContent($name, $content)
137139
{
138-
$method = 'get' . $this->string->upperCaseWords($name, '_', '');
139-
if (method_exists($this->pageConfig, $method)) {
140-
$content = $this->pageConfig->$method();
140+
switch ($name) {
141+
case Config::META_DESCRIPTION:
142+
return $this->pageConfig->getDescription();
143+
144+
case Config::META_CONTENT_TYPE:
145+
return $this->pageConfig->getContentType();
146+
147+
case Config::META_MEDIA_TYPE:
148+
return $this->pageConfig->getMediaType();
149+
150+
case Config::META_CHARSET:
151+
return $this->pageConfig->getCharset();
152+
153+
case Config::META_KEYWORDS:
154+
return $this->pageConfig->getKeywords();
155+
156+
case Config::META_ROBOTS:
157+
return $this->pageConfig->getRobots();
158+
159+
case Config::META_TITLE:
160+
return $this->pageConfig->getMetaTitle();
161+
162+
default:
163+
return $content;
141164
}
142-
return $content;
143165
}
144166

145167
/**
@@ -153,19 +175,19 @@ protected function getMetadataTemplate($name)
153175
}
154176

155177
switch ($name) {
156-
case 'charset':
178+
case Config::META_CHARSET:
157179
$metadataTemplate = '<meta charset="%content"/>' . "\n";
158180
break;
159181

160-
case 'content_type':
182+
case Config::META_CONTENT_TYPE:
161183
$metadataTemplate = '<meta http-equiv="Content-Type" content="%content"/>' . "\n";
162184
break;
163185

164-
case 'x_ua_compatible':
186+
case Config::META_X_UI_COMPATIBLE:
165187
$metadataTemplate = '<meta http-equiv="X-UA-Compatible" content="%content"/>' . "\n";
166188
break;
167189

168-
case 'media_type':
190+
case Config::META_MEDIA_TYPE:
169191
$metadataTemplate = false;
170192
break;
171193

@@ -352,7 +374,7 @@ protected function renderAssetHtml(\Magento\Framework\View\Asset\PropertyGroup $
352374
);
353375
$result .= sprintf($template, $asset->getUrl());
354376
}
355-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
377+
} catch (LocalizedException $e) {
356378
$this->logger->critical($e);
357379
$result .= sprintf($template, $this->urlBuilder->getUrl('', ['_direct' => 'core/index/notFound']));
358380
}

lib/internal/Magento/Framework/View/Test/Unit/Page/Config/RendererTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,14 @@ public function testRenderMetadata()
157157
. '<meta http-equiv="X-UA-Compatible" content="x_ua_compatible_value"/>' . "\n"
158158
. '<meta property="og:video:secure_url" content="secure_url"/>' . "\n";
159159

160-
$this->stringMock->expects($this->at(0))
161-
->method('upperCaseWords')
162-
->with('charset', '_', '')
163-
->willReturn('Charset');
164-
165160
$this->pageConfigMock->expects($this->once())
166161
->method('getCharset')
167162
->willReturn($metadataValueCharset);
168163

164+
$this->pageConfigMock->expects($this->once())
165+
->method('getContentType')
166+
->willReturn('content_type_value');
167+
169168
$this->pageConfigMock
170169
->expects($this->once())
171170
->method('getMetadata')

0 commit comments

Comments
 (0)