Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Container.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class Container implements Layout\GeneratorInterface
11 {
15  const CONTAINER_OPT_HTML_TAG = 'htmlTag';
16  const CONTAINER_OPT_HTML_CLASS = 'htmlClass';
17  const CONTAINER_OPT_HTML_ID = 'htmlId';
18  const CONTAINER_OPT_LABEL = 'label';
21  const TYPE = 'container';
22 
26  protected $allowedTags = [
27  'aside',
28  'dd',
29  'div',
30  'dl',
31  'fieldset',
32  'main',
33  'nav',
34  'header',
35  'footer',
36  'ol',
37  'p',
38  'section',
39  'table',
40  'tfoot',
41  'ul',
42  ];
43 
49  public function getType()
50  {
51  return self::TYPE;
52  }
53 
61  public function process(Layout\Reader\Context $readerContext, Layout\Generator\Context $generatorContext)
62  {
63  $structure = $generatorContext->getStructure();
64  $scheduledStructure = $readerContext->getScheduledStructure();
65  foreach ($scheduledStructure->getElements() as $elementName => $element) {
66  list($type, $data) = $element;
67  if ($type === self::TYPE) {
68  $this->generateContainer($structure, $elementName, $data['attributes']);
69  $scheduledStructure->unsetElement($elementName);
70  }
71  }
72  return $this;
73  }
74 
83  public function generateContainer(
86  $options
87  ) {
88  unset($options['type']);
89 
90  $this->validateOptions($options);
91 
92  foreach ($options as $key => $value) {
93  $structure->setAttribute($elementName, $key, $value);
94  }
95  }
96 
102  protected function validateOptions($options)
103  {
105  && !in_array(
107  $this->allowedTags
108  )
109  ) {
110  throw new \Magento\Framework\Exception\LocalizedException(
111  new \Magento\Framework\Phrase(
112  'Html tag "%1" is forbidden for usage in containers. Consider to use one of the allowed: %2.',
113  [$options[Layout\Element::CONTAINER_OPT_HTML_TAG], implode(', ', $this->allowedTags)]
114  )
115  );
116  }
117 
119  && (
122  )
123  ) {
124  throw new \Magento\Framework\Exception\LocalizedException(
125  new \Magento\Framework\Phrase('HTML ID or class will not have effect, if HTML tag is not specified.')
126  );
127  }
128  }
129 }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
process(Layout\Reader\Context $readerContext, Layout\Generator\Context $generatorContext)
Definition: Container.php:61
generateContainer(Layout\Data\Structure $structure, $elementName, $options)
Definition: Container.php:83
$elementName
Definition: gallery.phtml:10
$element
Definition: element.phtml:12