Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Theme\Model\ResourceModel\Design\Config\Scope\CollectionFactory;
16 
17 class Config implements ActionInterface
18 {
22  protected $collectionFactory;
23 
28 
32  protected $structureFactory;
33 
37  protected $saveHandler;
38 
42  protected $filterable = [];
43 
47  protected $searchable = [];
48 
52  protected $fieldsetPool;
53 
57  protected $handlerPool;
58 
69  public function __construct(
74  CollectionFactory $collectionFactory,
75  $data = []
76  ) {
77  $this->structureFactory = $structureFactory;
78  $this->saveHandlerFactory = $saveHandlerFactory;
79  $this->fieldsetPool = $fieldsetPool;
80  $this->handlerPool = $handlerPool;
81  $this->collectionFactory = $collectionFactory;
82  $this->data = $data;
83  }
84 
91  protected function execute(array $ids = [])
92  {
94  $collection = $this->collectionFactory->create();
95  $this->prepareFields();
96  if (!count($ids)) {
97  $this->getSaveHandler()->cleanIndex([]);
98  }
99  $this->getSaveHandler()->deleteIndex([], new \ArrayObject($ids));
100  $this->getSaveHandler()->saveIndex([], $collection);
101  }
102 
108  public function executeFull()
109  {
110  $this->execute();
111  }
112 
119  public function executeList(array $ids)
120  {
121  $this->execute($ids);
122  }
123 
130  public function executeRow($id)
131  {
132  $this->execute([$id]);
133  }
134 
140  protected function getSaveHandler()
141  {
142  if ($this->saveHandler === null) {
143  $this->saveHandler = $this->saveHandlerFactory->create(
144  $this->data['saveHandler'],
145  [
146  'indexStructure' => $this->getStructureInstance(),
147  'data' => $this->data,
148  ]
149  );
150  }
151  return $this->saveHandler;
152  }
153 
159  protected function prepareFields()
160  {
161  foreach ($this->data['fieldsets'] as $fieldsetName => $fieldset) {
162  $this->data['fieldsets'][$fieldsetName]['source'] = $this->collectionFactory->create();
163  if (isset($fieldset['provider'])) {
164  $fieldsetObject = $this->fieldsetPool->get($fieldset['provider']);
165  $this->data['fieldsets'][$fieldsetName] =
166  $fieldsetObject->addDynamicData($this->data['fieldsets'][$fieldsetName]);
167  }
168  foreach ($this->data['fieldsets'][$fieldsetName]['fields'] as $fieldName => $field) {
169  $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['origin'] =
170  $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['origin']
171  ?: $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'];
172  if ($fieldsetName != 0) {
173  $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'] =
174  $this->data['fieldsets'][$fieldsetName]['name'] . '_'
175  . $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['name'];
176  }
177  $this->saveFieldByType($field);
178  $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['handler'] =
179  $this->handlerPool->get($field['handler']);
180  $this->data['fieldsets'][$fieldsetName]['fields'][$fieldName]['dataType'] =
181  isset($field['dataType']) ? $field['dataType'] : 'varchar';
182  }
183  }
184  }
185 
192  protected function saveFieldByType($field)
193  {
194  switch ($field['type']) {
195  case 'filterable':
196  $this->filterable[] = $field;
197  break;
198  case 'searchable':
199  $this->searchable[] = $field;
200  break;
201  }
202  }
203 
209  protected function getStructureInstance()
210  {
211  if (!$this->data['structure']) {
212  return null;
213  }
214  return $this->structureFactory->create($this->data['structure']);
215  }
216 }
__construct(StructureFactory $structureFactory, SaveHandlerFactory $saveHandlerFactory, FieldsetPool $fieldsetPool, HandlerPool $handlerPool, CollectionFactory $collectionFactory, $data=[])
Definition: Config.php:69
$id
Definition: fieldset.phtml:14