MetadataLoader.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\RequestInterface;
  8. use Magento\Framework\App\ScopeFallbackResolverInterface;
  9. use Magento\Theme\Api\DesignConfigRepositoryInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. class MetadataLoader
  12. {
  13. /**
  14. * @var RequestInterface
  15. */
  16. protected $request;
  17. /**
  18. * @var ScopeFallbackResolverInterface
  19. */
  20. protected $scopeFallbackResolver;
  21. /**
  22. * @var DesignConfigRepositoryInterface
  23. */
  24. protected $designConfigRepository;
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. protected $storeManager;
  29. /**
  30. * @param RequestInterface $request
  31. * @param ScopeFallbackResolverInterface $scopeFallbackResolver
  32. * @param DesignConfigRepositoryInterface $designConfigRepository
  33. * @param StoreManagerInterface $storeManager
  34. */
  35. public function __construct(
  36. RequestInterface $request,
  37. ScopeFallbackResolverInterface $scopeFallbackResolver,
  38. DesignConfigRepositoryInterface $designConfigRepository,
  39. StoreManagerInterface $storeManager
  40. ) {
  41. $this->request = $request;
  42. $this->scopeFallbackResolver = $scopeFallbackResolver;
  43. $this->designConfigRepository = $designConfigRepository;
  44. $this->storeManager = $storeManager;
  45. }
  46. /**
  47. * Retrieve configuration metadata
  48. *
  49. * @return array
  50. */
  51. public function getData()
  52. {
  53. $scope = $this->request->getParam('scope');
  54. $scopeId = $this->request->getParam('scope_id');
  55. $data = [];
  56. if ($scope) {
  57. $showFallbackReset = false;
  58. list($fallbackScope, $fallbackScopeId) = $this->scopeFallbackResolver->getFallbackScope($scope, $scopeId);
  59. if ($fallbackScope && !$this->storeManager->isSingleStoreMode()) {
  60. $scope = $fallbackScope;
  61. $scopeId = $fallbackScopeId;
  62. $showFallbackReset = true;
  63. }
  64. $designConfig = $this->designConfigRepository->getByScope($scope, $scopeId);
  65. $fieldsData = $designConfig->getExtensionAttributes()->getDesignConfigData();
  66. foreach ($fieldsData as $fieldData) {
  67. $element = &$data;
  68. foreach (explode('/', $fieldData->getFieldConfig()['fieldset']) as $fieldset) {
  69. if (!isset($element[$fieldset]['children'])) {
  70. $element[$fieldset]['children'] = [];
  71. }
  72. $element = &$element[$fieldset]['children'];
  73. }
  74. $fieldName = $fieldData->getFieldConfig()['field'];
  75. $element[$fieldName]['arguments']['data']['config']['default'] = $fieldData->getValue();
  76. $element[$fieldName]['arguments']['data']['config']['showFallbackReset'] = $showFallbackReset;
  77. }
  78. }
  79. return $data;
  80. }
  81. }