Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Head.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class Head implements Layout\GeneratorInterface
13 {
17  const SRC_TYPE_RESOURCE = 'resource';
18  const SRC_TYPE_CONTROLLER = 'controller';
19  const SRC_TYPE_URL = 'url';
25  const TYPE = 'head';
26 
30  const VIRTUAL_CONTENT_TYPE_LINK = 'link';
31 
35  protected $remoteAssetTypes = [
36  self::SRC_TYPE_CONTROLLER => self::SRC_TYPE_CONTROLLER,
37  self::SRC_TYPE_URL => self::SRC_TYPE_URL,
38  ];
39 
43  protected $assetProperties = [
44  'ie_condition',
45  'order'
46  ];
47 
52  'src',
53  'src_type',
54  'content_type',
55  ];
56 
60  protected $pageConfig;
61 
65  private $url;
66 
73  public function __construct(
74  \Magento\Framework\View\Page\Config $pageConfig,
75  \Magento\Framework\UrlInterface $url = null
76  ) {
77  $this->pageConfig = $pageConfig;
78  $this->url = $url ?: ObjectManager::getInstance()->get(\Magento\Framework\UrlInterface::class);
79  }
80 
86  public function getType()
87  {
88  return self::TYPE;
89  }
90 
98  public function process(Layout\Reader\Context $readerContext, Layout\Generator\Context $generatorContext)
99  {
100  $structure = $readerContext->getPageConfigStructure();
101  $structure->processRemoveAssets();
102  $structure->processRemoveElementAttributes();
103 
104  $this->processAssets($structure);
105  $this->processTitle($structure);
106  $this->processMetadata($structure);
107  $this->processElementAttributes($structure);
108  return $this;
109  }
110 
117  protected function processAssets(Structure $pageStructure)
118  {
119  foreach ($pageStructure->getAssets() as $name => $data) {
120  if (isset($data['src_type']) && in_array($data['src_type'], $this->remoteAssetTypes)) {
121  if ($data['src_type'] === self::SRC_TYPE_CONTROLLER) {
122  $data['src'] = $this->url->getUrl($data['src']);
123  }
124 
125  $this->pageConfig->addRemotePageAsset(
126  $data['src'],
127  isset($data['content_type']) ? $data['content_type'] : self::VIRTUAL_CONTENT_TYPE_LINK,
128  $this->getAssetProperties($data),
129  $name
130  );
131  } else {
132  $this->pageConfig->addPageAsset($name, $this->getAssetProperties($data));
133  }
134  }
135  return $this;
136  }
137 
144  protected function getAssetProperties(array $data = [])
145  {
146  $properties = [];
147  $attributes = [];
148  foreach ($data as $name => $value) {
149  if (in_array($name, $this->assetProperties)) {
151  } elseif (!in_array($name, $this->serviceAssetProperties)) {
153  }
154  }
155  $properties['attributes'] = $attributes;
156  return $properties;
157  }
158 
165  protected function processTitle(Structure $pageStructure)
166  {
167  if ($pageStructure->getTitle()) {
168  $this->pageConfig->getTitle()->set($pageStructure->getTitle());
169  }
170  return $this;
171  }
172 
179  protected function processMetadata(Structure $pageStructure)
180  {
181  foreach ($pageStructure->getMetadata() as $name => $content) {
182  $this->pageConfig->setMetadata($name, $content);
183  }
184  return $this;
185  }
186 
193  protected function processElementAttributes(Structure $pageStructure)
194  {
195  foreach ($pageStructure->getElementAttributes() as $element => $attributes) {
196  foreach ($attributes as $name => $value) {
197  $this->pageConfig->setElementAttribute($element, $name, $value);
198  }
199  }
200  return $this;
201  }
202 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
processAssets(Structure $pageStructure)
Definition: Head.php:117
processMetadata(Structure $pageStructure)
Definition: Head.php:179
__construct(\Magento\Framework\View\Page\Config $pageConfig, \Magento\Framework\UrlInterface $url=null)
Definition: Head.php:73
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
processTitle(Structure $pageStructure)
Definition: Head.php:165
$properties
Definition: categories.php:26
processElementAttributes(Structure $pageStructure)
Definition: Head.php:193
process(Layout\Reader\Context $readerContext, Layout\Generator\Context $generatorContext)
Definition: Head.php:98
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12