Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Block.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
8 
10 
15 
22 {
26  protected $_filterProvider;
27 
33  protected static $_widgetUsageMap = [];
34 
40  protected $_blockFactory;
41 
45  private $block;
46 
53  public function __construct(
54  \Magento\Framework\View\Element\Template\Context $context,
55  \Magento\Cms\Model\Template\FilterProvider $filterProvider,
56  \Magento\Cms\Model\BlockFactory $blockFactory,
57  array $data = []
58  ) {
59  parent::__construct($context, $data);
60  $this->_filterProvider = $filterProvider;
61  $this->_blockFactory = $blockFactory;
62  }
63 
71  protected function _beforeToHtml()
72  {
73  parent::_beforeToHtml();
74  $blockId = $this->getData('block_id');
75  $blockHash = get_class($this) . $blockId;
76 
77  if (isset(self::$_widgetUsageMap[$blockHash])) {
78  return $this;
79  }
80  self::$_widgetUsageMap[$blockHash] = true;
81 
82  $block = $this->getBlock();
83 
84  if ($block && $block->isActive()) {
85  try {
86  $storeId = $this->_storeManager->getStore()->getId();
87  $this->setText(
88  $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent())
89  );
90  } catch (NoSuchEntityException $e) {
91  }
92  }
93  unset(self::$_widgetUsageMap[$blockHash]);
94  return $this;
95  }
96 
102  public function getIdentities()
103  {
104  $block = $this->getBlock();
105 
106  if ($block) {
107  return $block->getIdentities();
108  }
109 
110  return [];
111  }
112 
118  private function getBlock(): ?CmsBlock
119  {
120  if ($this->block) {
121  return $this->block;
122  }
123 
124  $blockId = $this->getData('block_id');
125 
126  if ($blockId) {
127  try {
128  $storeId = $this->_storeManager->getStore()->getId();
130  $block = $this->_blockFactory->create();
131  $block->setStoreId($storeId)->load($blockId);
132  $this->block = $block;
133 
134  return $block;
135  } catch (NoSuchEntityException $e) {
136  }
137  }
138 
139  return null;
140  }
141 }
getData($key='', $index=null)
Definition: DataObject.php:119
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Cms\Model\Template\FilterProvider $filterProvider, \Magento\Cms\Model\BlockFactory $blockFactory, array $data=[])
Definition: Block.php:53