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:
GeneratorInterface

Public Member Functions

 __construct (\Magento\Framework\View\Element\BlockFactory $blockFactory, \Magento\Framework\Data\Argument\InterpreterInterface $argumentInterpreter, \Magento\Framework\Event\ManagerInterface $eventManager, \Psr\Log\LoggerInterface $logger, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\ScopeResolverInterface $scopeResolver, \Magento\Framework\View\Element\ExceptionHandlerBlockFactory $exceptionHandlerBlockFactory, State $appState, $defaultClass=Template::class)
 
 getType ()
 
 createBlock ($block, $name, array $arguments=[])
 
- Public Member Functions inherited from GeneratorInterface
 process (Reader\Context $readerContext, Generator\Context $generatorContext)
 

Data Fields

const TYPE = 'block'
 

Protected Member Functions

 handleRenderException (\Exception $cause)
 
 generateBlock (Layout\ScheduledStructure $scheduledStructure, Layout\Data\Structure $structure, $elementName)
 
 getBlockInstance ($block, array $arguments=[])
 
 generateAction ($block, $methodName, $actionArguments)
 
 evaluateArguments (array $arguments)
 

Protected Attributes

 $blockFactory
 
 $argumentInterpreter
 
 $eventManager
 
 $logger
 
 $scopeConfig
 
 $scopeResolver
 
 $appState
 
 $exceptionHandlerBlockFactory
 

Detailed Description

Class Block @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 17 of file Block.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\View\Element\BlockFactory  $blockFactory,
\Magento\Framework\Data\Argument\InterpreterInterface  $argumentInterpreter,
\Magento\Framework\Event\ManagerInterface  $eventManager,
\Psr\Log\LoggerInterface  $logger,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Framework\App\ScopeResolverInterface  $scopeResolver,
\Magento\Framework\View\Element\ExceptionHandlerBlockFactory  $exceptionHandlerBlockFactory,
State  $appState,
  $defaultClass = Template::class 
)
Parameters
\Magento\Framework\View\Element\BlockFactory$blockFactory
\Magento\Framework\Data\Argument\InterpreterInterface$argumentInterpreter
\Magento\Framework\Event\ManagerInterface$eventManager
\Psr\Log\LoggerInterface$logger
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\App\ScopeResolverInterface$scopeResolver
\Magento\Framework\View\Element\ExceptionHandlerBlockFactory$exceptionHandlerBlockFactory
State$appState
string$defaultClass

Definition at line 82 of file Block.php.

92  {
93  $this->blockFactory = $blockFactory;
94  $this->argumentInterpreter = $argumentInterpreter;
95  $this->eventManager = $eventManager;
96  $this->logger = $logger;
97  $this->scopeConfig = $scopeConfig;
98  $this->scopeResolver = $scopeResolver;
99  $this->exceptionHandlerBlockFactory = $exceptionHandlerBlockFactory;
100  $this->appState = $appState;
101  $this->defaultClass = $defaultClass;
102  }

Member Function Documentation

◆ createBlock()

createBlock (   $block,
  $name,
array  $arguments = [] 
)

Create block instance

Parameters
string | \Magento\Framework\View\Element\AbstractBlock$block
string$name
array$arguments
Returns
\Magento\Framework\View\Element\AbstractBlock

Definition at line 247 of file Block.php.

248  {
250  $block->setType(get_class($block));
251  $block->setNameInLayout($name);
252  $block->addData(isset($arguments['data']) ? $arguments['data'] : []);
253  return $block;
254  }
getBlockInstance($block, array $arguments=[])
Definition: Block.php:264
$block
Definition: block.php:8
$arguments
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ evaluateArguments()

evaluateArguments ( array  $arguments)
protected

Compute and return argument values

Parameters
array$arguments
Returns
array

Definition at line 309 of file Block.php.

310  {
311  $result = [];
312  foreach ($arguments as $argumentName => $argumentData) {
313  if (!isset($argumentData[Dom::TYPE_ATTRIBUTE])) {
314  $result[$argumentName] = $argumentData;
315  continue;
316  }
317  $result[$argumentName] = $this->argumentInterpreter->evaluate($argumentData);
318  }
319  return $result;
320  }
$arguments

◆ generateAction()

generateAction (   $block,
  $methodName,
  $actionArguments 
)
protected

Run action defined in layout update

Parameters
\Magento\Framework\View\Element\AbstractBlock$block
string$methodName
array$actionArguments
Returns
void

Definition at line 294 of file Block.php.

295  {
296  $profilerKey = 'BLOCK_ACTION:' . $block->getNameInLayout() . '>' . $methodName;
297  \Magento\Framework\Profiler::start($profilerKey);
298  $args = $this->evaluateArguments($actionArguments);
299  call_user_func_array([$block, $methodName], $args);
300  \Magento\Framework\Profiler::stop($profilerKey);
301  }
$block
Definition: block.php:8

◆ generateBlock()

generateBlock ( Layout\ScheduledStructure  $scheduledStructure,
Layout\Data\Structure  $structure,
  $elementName 
)
protected

Create block and set related data

Parameters
\Magento\Framework\View\Layout\ScheduledStructure$scheduledStructure
\Magento\Framework\View\Layout\Data\Structure$structure
string$elementName
Returns
\Magento\Framework\View\Element\AbstractBlock

Definition at line 208 of file Block.php.

212  {
213  list(, $data) = $scheduledStructure->getElement($elementName);
214  $attributes = $data['attributes'];
215 
216  if (!empty($attributes['group'])) {
217  $structure->addToParentGroup($elementName, $attributes['group']);
218  }
219  if (!empty($attributes['display'])) {
220  $structure->setAttribute($elementName, 'display', $attributes['display']);
221  }
222 
223  // create block
224  $className = isset($attributes['class']) && !empty($attributes['class']) ?
225  $attributes['class'] : $this->defaultClass;
227  'data' => $this->evaluateArguments($data['arguments'])
228  ]);
229  if (!empty($attributes['template'])) {
230  $block->setTemplate($attributes['template']);
231  }
232  if (!empty($attributes['ttl'])) {
233  $ttl = (int)$attributes['ttl'];
234  $block->setTtl($ttl);
235  }
236  return $block;
237  }
$block
Definition: block.php:8
$attributes
Definition: matrix.phtml:13
createBlock($block, $name, array $arguments=[])
Definition: Block.php:247
$elementName
Definition: gallery.phtml:10
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ getBlockInstance()

getBlockInstance (   $block,
array  $arguments = [] 
)
protected

Create block object instance based on block type

Parameters
string | \Magento\Framework\View\Element\AbstractBlock$block
array$arguments
Exceptions

Definition at line 264 of file Block.php.

265  {
266  $e = null;
267  if ($block && is_string($block)) {
268  try {
269  $block = $this->blockFactory->createBlock($block, $arguments);
270  } catch (\ReflectionException $e) {
271  $this->logger->critical($e->getMessage());
272  }
273  }
274  if (!$block instanceof \Magento\Framework\View\Element\AbstractBlock) {
275  throw new \Magento\Framework\Exception\LocalizedException(
276  new \Magento\Framework\Phrase(
277  'Invalid block type: %1',
278  [is_object($block) ? get_class($block) : (string) $block]
279  ),
280  $e
281  );
282  }
283  return $block;
284  }
$block
Definition: block.php:8
$arguments

◆ getType()

getType ( )

{Return type of generator

Returns
string
}

Returns
string

Implements GeneratorInterface.

Definition at line 109 of file Block.php.

110  {
111  return self::TYPE;
112  }

◆ handleRenderException()

handleRenderException ( \Exception  $cause)
protected

Handle exceptions during rendering process

Parameters
\Exception$cause
Exceptions

Definition at line 191 of file Block.php.

192  {
193  if ($this->appState->getMode() === State::MODE_DEVELOPER) {
194  throw $cause;
195  }
196  $message = ($cause instanceof LocalizedException) ? $cause->getLogMessage() : $cause->getMessage();
197  $this->logger->critical($message);
198  }
$message

Field Documentation

◆ $appState

$appState
protected

Definition at line 57 of file Block.php.

◆ $argumentInterpreter

$argumentInterpreter
protected

Definition at line 32 of file Block.php.

◆ $blockFactory

$blockFactory
protected

Definition at line 27 of file Block.php.

◆ $eventManager

$eventManager
protected

Definition at line 37 of file Block.php.

◆ $exceptionHandlerBlockFactory

$exceptionHandlerBlockFactory
protected

Definition at line 62 of file Block.php.

◆ $logger

$logger
protected

Definition at line 42 of file Block.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 47 of file Block.php.

◆ $scopeResolver

$scopeResolver
protected

Definition at line 52 of file Block.php.

◆ TYPE

const TYPE = 'block'

Type of generator

Definition at line 22 of file Block.php.


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