DataLoader.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Design\Config\DataProvider;
  7. use Magento\Framework\App\Request\DataPersistorInterface;
  8. use Magento\Framework\App\RequestInterface;
  9. use Magento\Theme\Api\DesignConfigRepositoryInterface;
  10. class DataLoader
  11. {
  12. /**
  13. * @var RequestInterface
  14. */
  15. protected $request;
  16. /**
  17. * @var DesignConfigRepositoryInterface
  18. */
  19. protected $designConfigRepository;
  20. /**
  21. * @var DataPersistorInterface
  22. */
  23. protected $dataPersistor;
  24. /**
  25. * @param RequestInterface $request
  26. * @param DesignConfigRepositoryInterface $designConfigRepository
  27. * @param DataPersistorInterface $dataPersistor
  28. */
  29. public function __construct(
  30. RequestInterface $request,
  31. DesignConfigRepositoryInterface $designConfigRepository,
  32. DataPersistorInterface $dataPersistor
  33. ) {
  34. $this->request = $request;
  35. $this->designConfigRepository = $designConfigRepository;
  36. $this->dataPersistor = $dataPersistor;
  37. }
  38. /**
  39. * Retrieve configuration data
  40. *
  41. * @return array
  42. */
  43. public function getData()
  44. {
  45. $scope = $this->request->getParam('scope');
  46. $scopeId = $this->request->getParam('scope_id');
  47. $data = $this->loadData($scope, $scopeId);
  48. $data[$scope]['scope'] = $scope;
  49. $data[$scope]['scope_id'] = $scopeId;
  50. return $data;
  51. }
  52. /**
  53. * Load data
  54. *
  55. * @param string $scope
  56. * @param string $scopeId
  57. * @return array
  58. */
  59. protected function loadData($scope, $scopeId)
  60. {
  61. $designConfig = $this->designConfigRepository->getByScope($scope, $scopeId);
  62. $fieldsData = $designConfig->getExtensionAttributes()->getDesignConfigData();
  63. $data = [];
  64. foreach ($fieldsData as $fieldData) {
  65. $data[$scope][$fieldData->getFieldConfig()['field']] = $fieldData->getValue();
  66. }
  67. $storedData = $this->dataPersistor->get('theme_design_config');
  68. if (isset($storedData['scope']) && isset($storedData['scope_id'])
  69. && $storedData['scope'] == $scope && $storedData['scope_id'] == $scopeId
  70. ) {
  71. $data[$scope] = $storedData;
  72. $this->dataPersistor->clear('theme_design_config');
  73. }
  74. return $data;
  75. }
  76. }