Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackendModelFactory.php
Go to the documentation of this file.
1 <?php
7 
12 use Magento\Theme\Model\ResourceModel\Design\Config\CollectionFactory;
13 
15 {
19  protected $storedData = [];
20 
24  protected $metadata = [];
25 
29  protected $metadataProvider;
30 
34  protected $collectionFactory;
35 
39  protected $backendTypes = [];
40 
46  public function __construct(
49  CollectionFactory $collectionFactory
50  ) {
51  $this->metadataProvider = $metadataProvider;
52  $this->collectionFactory = $collectionFactory;
53  parent::__construct($objectManager);
54  }
55 
59  public function create(array $data = [])
60  {
61  $backendModelData = array_replace_recursive(
62  $this->getStoredData($data['scope'], $data['scopeId'], $data['config']['path']),
63  [
64  'path' => $data['config']['path'],
65  'scope' => $data['scope'],
66  'scope_id' => $data['scopeId'],
67  'field_config' => $data['config'],
68  ]
69  );
70 
71  $backendType = isset($data['config']['backend_model'])
72  ? $data['config']['backend_model']
74 
76  $backendModel = $this->getNewBackendModel($backendType, $backendModelData);
77  $backendModel->setValue($data['value']);
78 
79  return $backendModel;
80  }
81 
89  protected function getNewBackendModel($backendType, array $data = [])
90  {
91  return $this->_objectManager->create($backendType, ['data' => $data]);
92  }
93 
101  public function createByPath($path, array $data = [])
102  {
103  return $this->getNewBackendModel($this->getBackendTypeByPath($path), $data);
104  }
105 
112  protected function getBackendTypeByPath($path)
113  {
114  if (!isset($this->backendTypes[$path])) {
115  $metadata = $this->metadataProvider->get();
116  $index = array_search($path, array_column($metadata, 'path'));
117  $backendType = $this->_instanceName;
118  if ($index !== false && isset(array_values($metadata)[$index]['backend_model'])) {
119  $backendType = array_values($metadata)[$index]['backend_model'];
120  }
121  $this->backendTypes[$path] = $backendType;
122  }
123  return $this->backendTypes[$path];
124  }
125 
134  protected function getStoredData($scope, $scopeId, $path)
135  {
136  $storedData = $this->getScopeData($scope, $scopeId);
137  $dataKey = array_search($path, array_column($storedData, 'path'));
138  return $dataKey !== false ? $storedData[$dataKey] : [];
139  }
140 
148  protected function getScopeData($scope, $scopeId)
149  {
150  if (!isset($this->storedData[$scope][$scopeId])) {
151  $collection = $this->collectionFactory->create();
152  $collection->addPathsFilter($this->getMetadata());
153  $collection->addFieldToFilter('scope', $scope);
154  $collection->addScopeIdFilter($scopeId);
155  $this->storedData[$scope][$scopeId] = $collection->getData();
156  }
157  return $this->storedData[$scope][$scopeId];
158  }
159 
165  protected function getMetadata()
166  {
167  if (!$this->metadata) {
168  $this->metadata = $this->metadataProvider->get();
169  array_walk($this->metadata, function (&$value) {
170  $value = $value['path'];
171  });
172  }
173  return $this->metadata;
174  }
175 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16
__construct(ObjectManagerInterface $objectManager, MetadataProvider $metadataProvider, CollectionFactory $collectionFactory)
$index
Definition: list.phtml:44