Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
8 
12 
29 class Config
30 {
34  const ELEMENT_TYPE_BODY = 'body';
35  const ELEMENT_TYPE_HTML = 'html';
36  const ELEMENT_TYPE_HEAD = 'head';
39  const META_DESCRIPTION = 'description';
40  const META_CONTENT_TYPE = 'content_type';
41  const META_MEDIA_TYPE = 'media_type';
42  const META_CHARSET = 'charset';
43  const META_TITLE = 'title';
44  const META_KEYWORDS = 'keywords';
45  const META_ROBOTS = 'robots';
46  const META_X_UI_COMPATIBLE = 'x_ua_compatible';
47 
51  const BODY_ATTRIBUTE_CLASS = 'class';
52 
56  const HTML_ATTRIBUTE_LANG = 'lang';
57 
63  protected $allowedTypes = [
64  self::ELEMENT_TYPE_BODY,
65  self::ELEMENT_TYPE_HTML,
66  self::ELEMENT_TYPE_HEAD,
67  ];
68 
72  protected $title;
73 
79  protected $assetRepo;
80 
84  protected $pageAssets;
85 
89  protected $elements = [];
90 
94  protected $pageLayout;
95 
99  protected $scopeConfig;
100 
104  protected $favicon;
105 
109  protected $localeResolver;
110 
114  protected $builder;
115 
119  protected $includes;
120 
124  protected $metadata = [
125  'charset' => null,
126  'media_type' => null,
127  'content_type' => null,
128  'description' => null,
129  'keywords' => null,
130  'robots' => null,
131  'title' => null,
132  ];
133 
137  private $areaResolver;
138 
142  private $isIncludesAvailable;
143 
151  private function getAreaResolver()
152  {
153  if ($this->areaResolver === null) {
155  ->get(\Magento\Framework\App\State::class);
156  }
157  return $this->areaResolver;
158  }
159 
169  public function __construct(
170  View\Asset\Repository $assetRepo,
171  View\Asset\GroupedCollection $pageAssets,
172  App\Config\ScopeConfigInterface $scopeConfig,
173  View\Page\FaviconInterface $favicon,
174  Title $title,
175  \Magento\Framework\Locale\ResolverInterface $localeResolver,
176  $isIncludesAvailable = true
177  ) {
178  $this->assetRepo = $assetRepo;
179  $this->pageAssets = $pageAssets;
180  $this->scopeConfig = $scopeConfig;
181  $this->favicon = $favicon;
182  $this->title = $title;
183  $this->localeResolver = $localeResolver;
184  $this->isIncludesAvailable = $isIncludesAvailable;
185  $this->setElementAttribute(
186  self::ELEMENT_TYPE_HTML,
187  self::HTML_ATTRIBUTE_LANG,
188  strstr($this->localeResolver->getLocale(), '_', true)
189  );
190  }
191 
198  public function setBuilder(View\Layout\BuilderInterface $builder)
199  {
200  $this->builder = $builder;
201  return $this;
202  }
203 
209  protected function build()
210  {
211  if (!empty($this->builder)) {
212  $this->builder->build();
213  }
214  }
215 
223  public function publicBuild()
224  {
225  $this->build();
226  }
227 
233  public function getTitle()
234  {
235  $this->build();
236  return $this->title;
237  }
238 
246  public function setMetadata($name, $content)
247  {
248  $this->build();
249  $this->metadata[$name] = htmlspecialchars($content);
250  }
251 
257  public function getMetadata()
258  {
259  $this->build();
260  return $this->metadata;
261  }
262 
269  public function setContentType($contentType)
270  {
271  $this->setMetadata(self::META_CONTENT_TYPE, $contentType);
272  }
273 
279  public function getContentType()
280  {
281  $this->build();
282  if (strtolower($this->metadata[self::META_CONTENT_TYPE]) === 'auto') {
283  $this->metadata[self::META_CONTENT_TYPE] = $this->getMediaType() . '; charset=' . $this->getCharset();
284  }
285  return $this->metadata[self::META_CONTENT_TYPE];
286  }
287 
294  public function setMediaType($mediaType)
295  {
296  $this->setMetadata(self::META_MEDIA_TYPE, $mediaType);
297  }
298 
304  public function getMediaType()
305  {
306  $this->build();
307  if (empty($this->metadata[self::META_MEDIA_TYPE])) {
308  $this->metadata[self::META_MEDIA_TYPE] = $this->scopeConfig->getValue(
309  'design/head/default_media_type',
310  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
311  );
312  }
313  return $this->metadata[self::META_MEDIA_TYPE];
314  }
315 
322  public function setCharset($charset)
323  {
324  $this->setMetadata(self::META_CHARSET, $charset);
325  }
326 
332  public function getCharset()
333  {
334  $this->build();
335  if (empty($this->metadata[self::META_CHARSET])) {
336  $this->metadata[self::META_CHARSET] = $this->scopeConfig->getValue(
337  'design/head/default_charset',
338  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
339  );
340  }
341  return $this->metadata[self::META_CHARSET];
342  }
343 
350  public function setDescription($description)
351  {
352  $this->setMetadata(self::META_DESCRIPTION, $description);
353  }
354 
360  public function getDescription()
361  {
362  $this->build();
363  if (empty($this->metadata[self::META_DESCRIPTION])) {
364  $this->metadata[self::META_DESCRIPTION] = $this->scopeConfig->getValue(
365  'design/head/default_description',
366  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
367  );
368  }
369  return $this->metadata[self::META_DESCRIPTION];
370  }
371 
378  public function setMetaTitle($title)
379  {
380  $this->setMetadata(self::META_TITLE, $title);
381  }
382 
389  public function getMetaTitle()
390  {
391  $this->build();
392  if (empty($this->metadata[self::META_TITLE])) {
393  return '';
394  }
395 
396  return $this->metadata[self::META_TITLE];
397  }
398 
405  public function setKeywords($keywords)
406  {
407  $this->setMetadata(self::META_KEYWORDS, $keywords);
408  }
409 
415  public function getKeywords()
416  {
417  $this->build();
418  if (empty($this->metadata[self::META_KEYWORDS])) {
419  $this->metadata[self::META_KEYWORDS] = $this->scopeConfig->getValue(
420  'design/head/default_keywords',
421  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
422  );
423  }
424  return $this->metadata[self::META_KEYWORDS];
425  }
426 
433  public function setRobots($robots)
434  {
435  $this->setMetadata(self::META_ROBOTS, $robots);
436  }
437 
444  public function getRobots()
445  {
446  if ($this->getAreaResolver()->getAreaCode() !== Area::AREA_FRONTEND) {
447  return 'NOINDEX,NOFOLLOW';
448  }
449  $this->build();
450  if (empty($this->metadata[self::META_ROBOTS])) {
451  $this->metadata[self::META_ROBOTS] = $this->scopeConfig->getValue(
452  'design/search_engine_robots/default_robots',
453  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
454  );
455  }
456  return $this->metadata[self::META_ROBOTS];
457  }
458 
464  public function getAssetCollection()
465  {
466  $this->build();
467  return $this->pageAssets;
468  }
469 
478  public function addPageAsset($file, array $properties = [], $name = null)
479  {
480  $asset = $this->assetRepo->createAsset($file);
481  $name = $name ?: $file;
482  $this->pageAssets->add($name, $asset, $properties);
483 
484  return $this;
485  }
486 
496  public function addRemotePageAsset($url, $contentType, array $properties = [], $name = null)
497  {
498  $remoteAsset = $this->assetRepo->createRemoteAsset($url, $contentType);
499  $name = $name ?: $url;
500  $this->pageAssets->add($name, $remoteAsset, $properties);
501 
502  return $this;
503  }
504 
512  public function addRss($title, $href)
513  {
514  $remoteAsset = $this->assetRepo->createRemoteAsset((string)$href, 'unknown');
515  $this->pageAssets->add(
516  "link/{$href}",
517  $remoteAsset,
518  ['attributes' => 'rel="alternate" type="application/rss+xml" title="' . $title . '"']
519  );
520 
521  return $this;
522  }
523 
530  public function addBodyClass($className)
531  {
532  $className = preg_replace('#[^a-z0-9-_]+#', '-', strtolower($className));
533  $bodyClasses = $this->getElementAttribute(self::ELEMENT_TYPE_BODY, self::BODY_ATTRIBUTE_CLASS);
534  $bodyClasses = $bodyClasses ? explode(' ', $bodyClasses) : [];
535  $bodyClasses[] = $className;
536  $bodyClasses = array_unique($bodyClasses);
537  $this->setElementAttribute(
538  self::ELEMENT_TYPE_BODY,
539  self::BODY_ATTRIBUTE_CLASS,
540  implode(' ', $bodyClasses)
541  );
542  return $this;
543  }
544 
554  public function setElementAttribute($elementType, $attribute, $value)
555  {
556  $this->build();
557  if (array_search($elementType, $this->allowedTypes) === false) {
558  throw new \Magento\Framework\Exception\LocalizedException(
559  new \Magento\Framework\Phrase('%1 isn\'t allowed', [$elementType])
560  );
561  }
562  $this->elements[$elementType][$attribute] = $value;
563  return $this;
564  }
565 
573  public function getElementAttribute($elementType, $attribute)
574  {
575  $this->build();
576  return $this->elements[$elementType][$attribute] ?? null;
577  }
578 
585  public function getElementAttributes($elementType)
586  {
587  $this->build();
588  return $this->elements[$elementType] ?? [];
589  }
590 
597  public function setPageLayout($handle)
598  {
599  $this->pageLayout = $handle;
600  return $this;
601  }
602 
608  public function getPageLayout()
609  {
610  return $this->pageLayout;
611  }
612 
618  public function getFaviconFile()
619  {
620  return $this->favicon->getFaviconFile();
621  }
622 
628  public function getDefaultFavicon()
629  {
630  return $this->favicon->getDefaultFavicon();
631  }
632 
638  public function getIncludes()
639  {
640  if (empty($this->includes) && $this->isIncludesAvailable) {
641  $this->includes = $this->scopeConfig->getValue(
642  'design/head/includes',
643  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
644  );
645  }
646  return $this->includes;
647  }
648 }
$title
Definition: default.phtml:14
addPageAsset($file, array $properties=[], $name=null)
Definition: Config.php:478
getElementAttributes($elementType)
Definition: Config.php:585
setElementAttribute($elementType, $attribute, $value)
Definition: Config.php:554
$value
Definition: gender.phtml:16
setBuilder(View\Layout\BuilderInterface $builder)
Definition: Config.php:198
addRemotePageAsset($url, $contentType, array $properties=[], $name=null)
Definition: Config.php:496
getElementAttribute($elementType, $attribute)
Definition: Config.php:573
__construct(View\Asset\Repository $assetRepo, View\Asset\GroupedCollection $pageAssets, App\Config\ScopeConfigInterface $scopeConfig, View\Page\FaviconInterface $favicon, Title $title, \Magento\Framework\Locale\ResolverInterface $localeResolver, $isIncludesAvailable=true)
Definition: Config.php:169
$properties
Definition: categories.php:26
$handle
setMetadata($name, $content)
Definition: Config.php:246
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14