Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function buildView(BlockView $view, BlockInterface $block, array $options)
 {
     // merge the passed variables with the existing ones
     if (!empty($options['vars'])) {
         $view->vars = array_replace($view->vars, $options['vars']);
     }
     // add the view to itself vars to allow get it using 'block' variable in a rendered, for example TWIG
     $view->vars['block'] = $view;
     // replace attributes if specified ('attr' variable always exists in a view because it is added by FormView)
     if (isset($options['attr'])) {
         $view->vars['attr'] = $options['attr'];
     }
     // add label text and attributes if specified
     if (isset($options['label'])) {
         $view->vars['label'] = $options['label'];
         if (isset($options['label_attr'])) {
             $view->vars['label_attr'] = $options['label_attr'];
         }
     }
     // add the translation domain
     $view->vars['translation_domain'] = $this->getTranslationDomain($view, $options);
     // add core variables to the block view, like id, block type and variables required for rendering engine
     $id = $block->getId();
     $name = $block->getTypeName();
     // the block prefix must contain only letters, numbers and underscores (_)
     // due to limitations of block names in TWIG
     $uniqueBlockPrefix = '_' . preg_replace('/[^a-z0-9_]+/i', '_', $id);
     $blockPrefixes = $block->getTypeHelper()->getTypeNames($name);
     $blockPrefixes[] = $uniqueBlockPrefix;
     $view->vars['id'] = $id;
     $view->vars['block_type'] = $name;
     $view->vars['unique_block_prefix'] = $uniqueBlockPrefix;
     $view->vars['block_prefixes'] = $blockPrefixes;
     $view->vars['cache_key'] = sprintf('_%s_%s', $id, $name);
 }