DefaultValue.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Ui\Component\Form\Field;
  8. use Magento\Framework\View\Element\UiComponentFactory;
  9. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. /** Field class has dynamic default value based on System Configuration path */
  12. class DefaultValue extends \Magento\Ui\Component\Form\Field
  13. {
  14. /**
  15. * @var ScopeConfigInterface
  16. */
  17. private $scopeConfig;
  18. /**
  19. * @var string
  20. */
  21. private $path;
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface $storeManager
  24. */
  25. private $storeManager;
  26. /**
  27. * DefaultValue constructor.
  28. * @param ContextInterface $context
  29. * @param UiComponentFactory $uiComponentFactory
  30. * @param ScopeConfigInterface $scopeConfig
  31. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  32. * @param string $path
  33. * @param array $components
  34. * @param array $data
  35. */
  36. public function __construct(
  37. ContextInterface $context,
  38. UiComponentFactory $uiComponentFactory,
  39. ScopeConfigInterface $scopeConfig,
  40. \Magento\Store\Model\StoreManagerInterface $storeManager,
  41. string $path = '',
  42. array $components = [],
  43. array $data = []
  44. ) {
  45. parent::__construct($context, $uiComponentFactory, $components, $data);
  46. $this->scopeConfig = $scopeConfig;
  47. $this->storeManager = $storeManager;
  48. $this->path = $path;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function prepare()
  54. {
  55. parent::prepare();
  56. $store = $this->storeManager->getStore();
  57. $this->_data['config']['default'] = $this->scopeConfig->getValue(
  58. $this->path,
  59. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  60. $store
  61. );
  62. }
  63. }