Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DesignAbstraction.php
Go to the documentation of this file.
1 <?php
7 
15 {
20 
24  protected $_themesFactory;
25 
29  protected $_appState;
30 
38  public function __construct(
39  \Magento\Framework\View\Element\Context $context,
40  \Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory,
41  \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $themesFactory,
42  \Magento\Framework\App\State $appState,
43  array $data = []
44  ) {
45  $this->_layoutProcessorFactory = $layoutProcessorFactory;
46  $this->_themesFactory = $themesFactory;
47  $this->_appState = $appState;
48  parent::__construct($context, $data);
49  }
50 
56  protected function _beforeToHtml()
57  {
58  if (!$this->getOptions()) {
59  $this->addOption('', __('-- Please Select --'));
60  $layoutUpdateParams = ['theme' => $this->_getThemeInstance($this->getTheme())];
61  $designAbstractions = $this->_appState->emulateAreaCode(
62  'frontend',
63  [$this->_getLayoutProcessor($layoutUpdateParams), 'getAllDesignAbstractions']
64  );
65  $this->_addDesignAbstractionOptions($designAbstractions);
66  }
67  return parent::_beforeToHtml();
68  }
69 
76  protected function _getThemeInstance($themeId)
77  {
79  $themeCollection = $this->_themesFactory->create();
80  return $themeCollection->getItemById($themeId);
81  }
82 
89  protected function _getLayoutProcessor(array $arguments)
90  {
91  return $this->_layoutProcessorFactory->create($arguments);
92  }
93 
100  protected function _addDesignAbstractionOptions(array $designAbstractions)
101  {
102  $label = [];
103  // Sort list of design abstractions by label
104  foreach ($designAbstractions as $key => $row) {
105  $label[$key] = $row['label'];
106  }
107  array_multisort($label, SORT_STRING, $designAbstractions);
108 
109  // Group the layout options
110  $customLayouts = [];
111  $pageLayouts = [];
113  $layoutProcessor = $this->_layoutProcessorFactory->create();
114  foreach ($designAbstractions as $pageTypeName => $pageTypeInfo) {
115  if ($layoutProcessor->isPageLayoutDesignAbstraction($pageTypeInfo)) {
116  $pageLayouts[] = ['value' => $pageTypeName, 'label' => $pageTypeInfo['label']];
117  } else {
118  $customLayouts[] = ['value' => $pageTypeName, 'label' => $pageTypeInfo['label']];
119  }
120  }
121  $params = [];
122  $this->addOption($customLayouts, __('Custom Layouts'), $params);
123  $this->addOption($pageLayouts, __('Page Layouts'), $params);
124  }
125 }
__()
Definition: __.php:13
__construct(\Magento\Framework\View\Element\Context $context, \Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory, \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $themesFactory, \Magento\Framework\App\State $appState, array $data=[])
addOption($value, $label, $params=[])
Definition: Select.php:50
$label
Definition: details.phtml:21
$arguments
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18