Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Update.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  private $_cache;
18 
22  private $layoutUpdateCache;
23 
29  public function __construct(
30  \Magento\Framework\Model\ResourceModel\Db\Context $context,
31  \Magento\Framework\Cache\FrontendInterface $cache,
32  $connectionName = null
33  ) {
34  parent::__construct($context, $connectionName);
35  $this->_cache = $cache;
36  }
37 
43  protected function _construct()
44  {
45  $this->_init('layout_update', 'layout_update_id');
46  }
47 
56  public function fetchUpdatesByHandle(
57  $handle,
58  \Magento\Framework\View\Design\ThemeInterface $theme,
59  \Magento\Framework\App\ScopeInterface $store
60  ) {
61  $bind = ['theme_id' => $theme->getId(), 'store_id' => $store->getId()];
62  $cacheKey = implode('-', $bind);
63  if (!isset($this->layoutUpdateCache[$cacheKey])) {
64  $this->layoutUpdateCache[$cacheKey] = [];
65  foreach ($this->getConnection()->fetchAll($this->_getFetchUpdatesByHandleSelect(), $bind) as $layout) {
66  if (!isset($this->layoutUpdateCache[$cacheKey][$layout['handle']])) {
67  $this->layoutUpdateCache[$cacheKey][$layout['handle']] = '';
68  }
69  $this->layoutUpdateCache[$cacheKey][$layout['handle']] .= $layout['xml'];
70  }
71  }
72  return isset($this->layoutUpdateCache[$cacheKey][$handle]) ? $this->layoutUpdateCache[$cacheKey][$handle] : '';
73  }
74 
81  protected function _getFetchUpdatesByHandleSelect($loadAllUpdates = false)
82  {
83  //@todo Why it also loads layout updates for store_id=0, isn't it Admin Store View?
84  //If 0 means 'all stores' why it then refers by foreign key to Admin in `store` and not to something named
85  // 'All Stores'?
86 
87  $select = $this->getConnection()->select()->from(
88  ['layout_update' => $this->getMainTable()],
89  ['xml', 'handle']
90  )->join(
91  ['link' => $this->getTable('layout_link')],
92  'link.layout_update_id=layout_update.layout_update_id',
93  ''
94  )->where(
95  'link.store_id IN (0, :store_id)'
96  )->where(
97  'link.theme_id = :theme_id'
98  )->order(
99  'layout_update.sort_order ' . \Magento\Framework\DB\Select::SQL_ASC
100  );
101 
102  if (!$loadAllUpdates) {
103  $select->where('link.is_temporary = 0');
104  }
105 
106  return $select;
107  }
108 
115  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
116  {
117  $data = $object->getData();
118  if (isset($data['store_id']) && isset($data['theme_id'])) {
119  $this->getConnection()->insertOnDuplicate(
120  $this->getTable('layout_link'),
121  [
122  'store_id' => $data['store_id'],
123  'theme_id' => $data['theme_id'],
124  'layout_update_id' => $object->getId(),
125  'is_temporary' => (int)$object->getIsTemporary()
126  ]
127  );
128  }
129  $this->_cache->clean();
130  return parent::_afterSave($object);
131  }
132 }
_getFetchUpdatesByHandleSelect($loadAllUpdates=false)
Definition: Update.php:81
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Update.php:115
$theme
fetchUpdatesByHandle( $handle, \Magento\Framework\View\Design\ThemeInterface $theme, \Magento\Framework\App\ScopeInterface $store)
Definition: Update.php:56
$handle
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Cache\FrontendInterface $cache, $connectionName=null)
Definition: Update.php:29