Usecustom.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Config backend model for "Use Custom Admin URL" option
  8. */
  9. namespace Magento\Config\Model\Config\Backend\Admin;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Usecustom extends \Magento\Framework\App\Config\Value
  15. {
  16. /**
  17. * Writer of configuration storage
  18. *
  19. * @var \Magento\Framework\App\Config\Storage\WriterInterface
  20. */
  21. protected $_configWriter;
  22. /**
  23. * @param \Magento\Framework\Model\Context $context
  24. * @param \Magento\Framework\Registry $registry
  25. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  26. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  27. * @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
  28. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  29. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  30. * @param array $data
  31. */
  32. public function __construct(
  33. \Magento\Framework\Model\Context $context,
  34. \Magento\Framework\Registry $registry,
  35. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  36. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  37. \Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
  38. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  39. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  40. array $data = []
  41. ) {
  42. $this->_configWriter = $configWriter;
  43. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  44. }
  45. /**
  46. * Validate custom url
  47. *
  48. * @return $this
  49. * @throws \Magento\Framework\Exception\LocalizedException
  50. */
  51. public function beforeSave()
  52. {
  53. $value = $this->getValue();
  54. if ($value == 1) {
  55. $customUrl = $this->getData('groups/url/fields/custom/value');
  56. if (empty($customUrl)) {
  57. throw new \Magento\Framework\Exception\LocalizedException(__('Please specify the admin custom URL.'));
  58. }
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Delete custom admin url from configuration if "Use Custom Admin Url" option disabled
  64. *
  65. * @return $this
  66. */
  67. public function afterSave()
  68. {
  69. $value = $this->getValue();
  70. if (!$value) {
  71. $this->_configWriter->delete(
  72. Custom::XML_PATH_SECURE_BASE_URL,
  73. Custom::CONFIG_SCOPE,
  74. Custom::CONFIG_SCOPE_ID
  75. );
  76. $this->_configWriter->delete(
  77. Custom::XML_PATH_UNSECURE_BASE_URL,
  78. Custom::CONFIG_SCOPE,
  79. Custom::CONFIG_SCOPE_ID
  80. );
  81. }
  82. return parent::afterSave();
  83. }
  84. }