Data.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Helper;
  7. /**
  8. * Eav data helper
  9. */
  10. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  11. {
  12. /**
  13. * XML path to input types validator data in config
  14. *
  15. * @var string
  16. */
  17. const XML_PATH_VALIDATOR_DATA_INPUT_TYPES = 'general/validator_data/input_types';
  18. /**
  19. * @var array
  20. */
  21. protected $_attributesLockedFields = [];
  22. /**
  23. * @var array
  24. */
  25. protected $_entityTypeFrontendClasses = [];
  26. /**
  27. * @var \Magento\Eav\Model\Entity\Attribute\Config
  28. */
  29. protected $_attributeConfig;
  30. /**
  31. * @var \Magento\Eav\Model\Config
  32. */
  33. protected $_eavConfig;
  34. /**
  35. * @param \Magento\Framework\App\Helper\Context $context
  36. * @param \Magento\Eav\Model\Entity\Attribute\Config $attributeConfig
  37. * @param \Magento\Eav\Model\Config $eavConfig
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. \Magento\Framework\App\Helper\Context $context,
  42. \Magento\Eav\Model\Entity\Attribute\Config $attributeConfig,
  43. \Magento\Eav\Model\Config $eavConfig
  44. ) {
  45. $this->_attributeConfig = $attributeConfig;
  46. $this->_eavConfig = $eavConfig;
  47. parent::__construct($context);
  48. }
  49. /**
  50. * Return default frontend classes value label array
  51. *
  52. * @return array
  53. */
  54. protected function _getDefaultFrontendClasses()
  55. {
  56. return [
  57. ['value' => '', 'label' => __('None')],
  58. ['value' => 'validate-number', 'label' => __('Decimal Number')],
  59. ['value' => 'validate-digits', 'label' => __('Integer Number')],
  60. ['value' => 'validate-email', 'label' => __('Email')],
  61. ['value' => 'validate-url', 'label' => __('URL')],
  62. ['value' => 'validate-alpha', 'label' => __('Letters')],
  63. ['value' => 'validate-alphanum', 'label' => __('Letters (a-z, A-Z) or Numbers (0-9)')]
  64. ];
  65. }
  66. /**
  67. * Return merged default and entity type frontend classes value label array
  68. *
  69. * @param string $entityTypeCode
  70. * @return array
  71. */
  72. public function getFrontendClasses($entityTypeCode)
  73. {
  74. $_defaultClasses = $this->_getDefaultFrontendClasses();
  75. if (isset($this->_entityTypeFrontendClasses[$entityTypeCode])) {
  76. return array_merge($_defaultClasses, $this->_entityTypeFrontendClasses[$entityTypeCode]);
  77. }
  78. return $_defaultClasses;
  79. }
  80. /**
  81. * Retrieve attributes locked fields to edit
  82. *
  83. * @param string $entityTypeCode
  84. * @return array
  85. */
  86. public function getAttributeLockedFields($entityTypeCode)
  87. {
  88. if (!$entityTypeCode) {
  89. return [];
  90. }
  91. if (isset($this->_attributesLockedFields[$entityTypeCode])) {
  92. return $this->_attributesLockedFields[$entityTypeCode];
  93. }
  94. $attributesLockedFields = $this->_attributeConfig->getEntityAttributesLockedFields($entityTypeCode);
  95. if (count($attributesLockedFields)) {
  96. $this->_attributesLockedFields[$entityTypeCode] = $attributesLockedFields;
  97. return $this->_attributesLockedFields[$entityTypeCode];
  98. }
  99. return [];
  100. }
  101. /**
  102. * Get input types validator data
  103. *
  104. * @return array
  105. */
  106. public function getInputTypesValidatorData()
  107. {
  108. return $this->scopeConfig->getValue(
  109. self::XML_PATH_VALIDATOR_DATA_INPUT_TYPES,
  110. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  111. );
  112. }
  113. /**
  114. * Retrieve attribute metadata.
  115. *
  116. * @param string $entityTypeCode
  117. * @param string $attributeCode
  118. * @return array <pre>[
  119. * 'entity_type_id' => $entityTypeId,
  120. * 'attribute_id' => $attributeId,
  121. * 'attribute_table' => $attributeTable
  122. * 'backend_type' => $backendType
  123. * ]</pre>
  124. */
  125. public function getAttributeMetadata($entityTypeCode, $attributeCode)
  126. {
  127. $attribute = $this->_eavConfig->getAttribute($entityTypeCode, $attributeCode);
  128. return [
  129. 'entity_type_id' => $attribute->getEntityTypeId(),
  130. 'attribute_id' => $attribute->getAttributeId(),
  131. 'attribute_table' => $attribute->getBackend()->getTable(),
  132. 'backend_type' => $attribute->getBackendType()
  133. ];
  134. }
  135. }