Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Block Class Reference
Inheritance diagram for Block:
ReaderInterface

Public Member Functions

 __construct (Layout\ScheduledStructure\Helper $helper, Layout\Argument\Parser $argumentParser, Layout\ReaderPool $readerPool, InterpreterInterface $argumentInterpreter, Condition $conditionReader, $scopeType=null)
 
 getSupportedNodes ()
 
 interpret (Context $readerContext, Element $currentElement)
 
- Public Member Functions inherited from ReaderInterface
 interpret (Reader\Context $readerContext, Element $element)
 

Data Fields

const TYPE_BLOCK = 'block'
 
const TYPE_REFERENCE_BLOCK = 'referenceBlock'
 
const TYPE_ARGUMENTS = 'arguments'
 
const TYPE_ACTION = 'action'
 
const ATTRIBUTE_GROUP = 'group'
 
const ATTRIBUTE_CLASS = 'class'
 
const ATTRIBUTE_TEMPLATE = 'template'
 
const ATTRIBUTE_TTL = 'ttl'
 
const ATTRIBUTE_DISPLAY = 'display'
 
const ATTRIBUTE_ACL = 'aclResource'
 

Protected Member Functions

 scheduleBlock (ScheduledStructure $scheduledStructure, Element $currentElement)
 
 mergeBlockAttributes (array $elementData, Element $currentElement)
 
 scheduleReference (ScheduledStructure $scheduledStructure, Element $currentElement)
 
 updateScheduledData ($currentElement, array &$data)
 
 getAttributes (Element $blockElement)
 
 getArguments (Element $blockElement)
 
 parseArguments (Element $node)
 
 evaluateArguments (Element $blockElement, array &$data)
 

Protected Attributes

 $attributes
 
 $helper
 
 $argumentParser
 
 $readerPool
 
 $scopeType
 
 $argumentInterpreter
 

Detailed Description

Block structure reader

Definition at line 17 of file Block.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Layout\ScheduledStructure\Helper  $helper,
Layout\Argument\Parser  $argumentParser,
Layout\ReaderPool  $readerPool,
InterpreterInterface  $argumentInterpreter,
Condition  $conditionReader,
  $scopeType = null 
)

Constructor

Parameters
Layout\ScheduledStructure\Helper$helper
Layout\Argument\Parser$argumentParser
Layout\ReaderPool$readerPool
InterpreterInterface$argumentInterpreter
Condition$conditionReader
string | null$scopeType

Definition at line 99 of file Block.php.

106  {
107  $this->helper = $helper;
108  $this->argumentParser = $argumentParser;
109  $this->readerPool = $readerPool;
110  $this->scopeType = $scopeType;
111  $this->argumentInterpreter = $argumentInterpreter;
112  $this->conditionReader = $conditionReader;
113  }

Member Function Documentation

◆ evaluateArguments()

evaluateArguments ( Element  $blockElement,
array &  $data 
)
protected

Compute argument values

Parameters
Element$blockElement
array$data
Returns
void

Definition at line 363 of file Block.php.

364  {
365  $arguments = $this->getArguments($blockElement);
366  foreach ($arguments as $argumentName => $argumentData) {
367  if (isset($argumentData['updater'])) {
368  continue;
369  }
370  $result = $this->argumentInterpreter->evaluate($argumentData);
371  if (is_array($result)) {
372  $data['arguments'][$argumentName] = isset($data['arguments'][$argumentName])
373  ? array_replace_recursive($data['arguments'][$argumentName], $result)
374  : $result;
375  } else {
376  $data['arguments'][$argumentName] = $result;
377  }
378  }
379  }
getArguments(Element $blockElement)
Definition: Block.php:310
$arguments

◆ getArguments()

getArguments ( Element  $blockElement)
protected

Get block arguments

Parameters
Element$blockElement
Returns
array

Definition at line 310 of file Block.php.

311  {
312  $arguments = $this->getElementsByType($blockElement, self::TYPE_ARGUMENTS);
313  // We have only one declaration of <arguments> node in block or its reference
314  $argumentElement = reset($arguments);
315  return $argumentElement ? $this->parseArguments($argumentElement) : [];
316  }
$arguments

◆ getAttributes()

getAttributes ( Element  $blockElement)
protected

Get block attributes

Parameters
Element$blockElement
Returns
array

Definition at line 276 of file Block.php.

277  {
278  $attributes = [];
279  foreach ($this->attributes as $attributeName) {
280  $attributes[$attributeName] = (string)$blockElement->getAttribute($attributeName);
281  }
282  return $attributes;
283  }

◆ getSupportedNodes()

getSupportedNodes ( )

{Get nodes types that current reader is support

Returns
string[]
}

Returns
string[]

Implements ReaderInterface.

Definition at line 120 of file Block.php.

◆ interpret()

interpret ( Context  $readerContext,
Element  $currentElement 
)

{}

Parameters
Context$readerContext
Element$currentElement
Element$parentElement
Returns
$this

Definition at line 133 of file Block.php.

134  {
135  $scheduledStructure = $readerContext->getScheduledStructure();
136  switch ($currentElement->getName()) {
137  case self::TYPE_BLOCK:
138  $this->scheduleBlock($scheduledStructure, $currentElement);
139  break;
141  $this->scheduleReference($scheduledStructure, $currentElement);
142  break;
143  default:
144  break;
145  }
146  $this->readerPool->interpret($readerContext, $currentElement);
147  return $this;
148  }
scheduleBlock(ScheduledStructure $scheduledStructure, Element $currentElement)
Definition: Block.php:157
scheduleReference(ScheduledStructure $scheduledStructure, Element $currentElement)
Definition: Block.php:231

◆ mergeBlockAttributes()

mergeBlockAttributes ( array  $elementData,
Element  $currentElement 
)
protected

Merge Block attributes

Parameters
array$elementData
Element$currentElement
Returns
array

Definition at line 184 of file Block.php.

185  {
186  $currentElement = $this->replaceDeprecatedAclKey($currentElement);
187  if (isset($elementData['attributes'])) {
188  $elementData['attributes'] = $this->replaceDeprecatedAclKey($elementData['attributes']);
189  $keys = array_keys($elementData['attributes']);
190  foreach ($keys as $key) {
191  if (isset($currentElement[$key])) {
192  $elementData['attributes'][$key] = (string)$currentElement[$key];
193  }
194  }
195  } else {
196  $elementData['attributes'] = [
197  self::ATTRIBUTE_CLASS => (string)$currentElement[self::ATTRIBUTE_CLASS],
198  self::ATTRIBUTE_GROUP => (string)$currentElement[self::ATTRIBUTE_GROUP],
199  self::ATTRIBUTE_TEMPLATE => (string)$currentElement[self::ATTRIBUTE_TEMPLATE],
200  self::ATTRIBUTE_TTL => (string)$currentElement[self::ATTRIBUTE_TTL],
201  self::ATTRIBUTE_DISPLAY => (string)$currentElement[self::ATTRIBUTE_DISPLAY],
202  self::ATTRIBUTE_ACL => (string)$currentElement[self::ATTRIBUTE_ACL],
203  ];
204  }
205  return $elementData['attributes'];
206  }

◆ parseArguments()

parseArguments ( Element  $node)
protected

Parse argument nodes and return their array representation

Parameters
Element$node
Returns
array

Definition at line 343 of file Block.php.

344  {
345  $nodeDom = dom_import_simplexml($node);
346  $result = [];
347  foreach ($nodeDom->childNodes as $argumentNode) {
348  if ($argumentNode instanceof \DOMElement && $argumentNode->nodeName == 'argument') {
349  $argumentName = $argumentNode->getAttribute('name');
350  $result[$argumentName] = $this->argumentParser->parse($argumentNode);
351  }
352  }
353  return $result;
354  }

◆ scheduleBlock()

scheduleBlock ( ScheduledStructure  $scheduledStructure,
Element  $currentElement 
)
protected

Process block element their attributes and children

Parameters
ScheduledStructure$scheduledStructure
Element$currentElement
Returns
void

Definition at line 157 of file Block.php.

160  {
161  $elementName = $this->helper->scheduleStructure(
162  $scheduledStructure,
163  $currentElement,
164  $currentElement->getParent()
165  );
166  $data = $scheduledStructure->getStructureElementData($elementName, []);
167  $data['attributes'] = $this->mergeBlockAttributes($data, $currentElement);
168  $this->updateScheduledData($currentElement, $data);
169  $this->evaluateArguments($currentElement, $data);
170  $data['attributes'] = array_merge(
171  $data['attributes'],
172  ['visibilityConditions' => $this->conditionReader->parseConditions($currentElement)]
173  );
174  $scheduledStructure->setStructureElementData($elementName, $data);
175  }
evaluateArguments(Element $blockElement, array &$data)
Definition: Block.php:363
updateScheduledData($currentElement, array &$data)
Definition: Block.php:257
mergeBlockAttributes(array $elementData, Element $currentElement)
Definition: Block.php:184
$elementName
Definition: gallery.phtml:10

◆ scheduleReference()

scheduleReference ( ScheduledStructure  $scheduledStructure,
Element  $currentElement 
)
protected

Schedule reference data

Parameters
ScheduledStructure$scheduledStructure
Element$currentElement
Returns
void

Definition at line 231 of file Block.php.

234  {
235  $elementName = $currentElement->getAttribute('name');
236  $elementRemove = filter_var($currentElement->getAttribute('remove'), FILTER_VALIDATE_BOOLEAN);
237  if ($elementRemove) {
238  $scheduledStructure->setElementToRemoveList($elementName);
239  return;
240  } elseif ($currentElement->getAttribute('remove')) {
241  $scheduledStructure->unsetElementFromListToRemove($elementName);
242  }
243  $data = $scheduledStructure->getStructureElementData($elementName, []);
244  $data['attributes'] = $this->mergeBlockAttributes($data, $currentElement);
245  $this->updateScheduledData($currentElement, $data);
246  $this->evaluateArguments($currentElement, $data);
247  $scheduledStructure->setStructureElementData($elementName, $data);
248  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
evaluateArguments(Element $blockElement, array &$data)
Definition: Block.php:363
updateScheduledData($currentElement, array &$data)
Definition: Block.php:257
mergeBlockAttributes(array $elementData, Element $currentElement)
Definition: Block.php:184
$elementName
Definition: gallery.phtml:10

◆ updateScheduledData()

updateScheduledData (   $currentElement,
array &  $data 
)
protected

Update data for scheduled element

Parameters
Element$currentElement
array&$data
Returns
array

Definition at line 257 of file Block.php.

258  {
259  $actions = $this->getActions($currentElement);
260  $arguments = $this->getArguments($currentElement);
261  $data['actions'] = isset($data['actions'])
262  ? array_merge($data['actions'], $actions)
263  : $actions;
264  $data['arguments'] = isset($data['arguments'])
265  ? array_replace_recursive($data['arguments'], $arguments)
266  : $arguments;
267  return $data;
268  }
getArguments(Element $blockElement)
Definition: Block.php:310
$arguments

Field Documentation

◆ $argumentInterpreter

$argumentInterpreter
protected

Definition at line 76 of file Block.php.

◆ $argumentParser

$argumentParser
protected

Definition at line 61 of file Block.php.

◆ $attributes

$attributes
protected

◆ $helper

$helper
protected

Definition at line 56 of file Block.php.

◆ $readerPool

$readerPool
protected

Definition at line 66 of file Block.php.

◆ $scopeType

$scopeType
protected

Definition at line 71 of file Block.php.

◆ ATTRIBUTE_ACL

const ATTRIBUTE_ACL = 'aclResource'

Definition at line 41 of file Block.php.

◆ ATTRIBUTE_CLASS

const ATTRIBUTE_CLASS = 'class'

Definition at line 37 of file Block.php.

◆ ATTRIBUTE_DISPLAY

const ATTRIBUTE_DISPLAY = 'display'

Definition at line 40 of file Block.php.

◆ ATTRIBUTE_GROUP

const ATTRIBUTE_GROUP = 'group'

#- #+ Names of block attributes in layout

Definition at line 36 of file Block.php.

◆ ATTRIBUTE_TEMPLATE

const ATTRIBUTE_TEMPLATE = 'template'

Definition at line 38 of file Block.php.

◆ ATTRIBUTE_TTL

const ATTRIBUTE_TTL = 'ttl'

Definition at line 39 of file Block.php.

◆ TYPE_ACTION

const TYPE_ACTION = 'action'

Definition at line 30 of file Block.php.

◆ TYPE_ARGUMENTS

const TYPE_ARGUMENTS = 'arguments'

#- #+ Supported subtypes for blocks

Definition at line 29 of file Block.php.

◆ TYPE_BLOCK

const TYPE_BLOCK = 'block'

#+ Supported types

Definition at line 22 of file Block.php.

◆ TYPE_REFERENCE_BLOCK

const TYPE_REFERENCE_BLOCK = 'referenceBlock'

Definition at line 23 of file Block.php.


The documentation for this class was generated from the following file: