Attribute.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Catalog\Api\Data\EavAttributeInterface;
  8. use Magento\Framework\Api\AttributeValueFactory;
  9. use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
  10. /**
  11. * Customer attribute model
  12. *
  13. * @method int getSortOrder()
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Attribute extends \Magento\Eav\Model\Attribute
  17. {
  18. /**
  19. * Name of the module
  20. */
  21. const MODULE_NAME = 'Magento_Customer';
  22. /**
  23. * Prefix of model events names
  24. *
  25. * @var string
  26. */
  27. protected $_eventPrefix = 'customer_entity_attribute';
  28. /**
  29. * Prefix of model events object
  30. *
  31. * @var string
  32. */
  33. protected $_eventObject = 'attribute';
  34. /**
  35. * @var \Magento\Framework\Indexer\IndexerRegistry
  36. */
  37. protected $indexerRegistry;
  38. /**
  39. * @var \Magento\Customer\Model\Metadata\AttributeMetadataCache
  40. */
  41. private $attributeMetadataCache;
  42. /**
  43. * Constructor
  44. *
  45. * @param \Magento\Framework\Model\Context $context
  46. * @param \Magento\Framework\Registry $registry
  47. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  48. * @param AttributeValueFactory $customAttributeFactory
  49. * @param \Magento\Eav\Model\Config $eavConfig
  50. * @param \Magento\Eav\Model\Entity\TypeFactory $eavTypeFactory
  51. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  52. * @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper
  53. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  54. * @param \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionDataFactory
  55. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  56. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  57. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  58. * @param \Magento\Catalog\Model\Product\ReservedAttributeList $reservedAttributeList
  59. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  60. * @param DateTimeFormatterInterface $dateTimeFormatter
  61. * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
  62. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  63. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  64. * @param array|null $data
  65. * @param \Magento\Customer\Model\Metadata\AttributeMetadataCache|null $attributeMetadataCache
  66. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  67. */
  68. public function __construct(
  69. \Magento\Framework\Model\Context $context,
  70. \Magento\Framework\Registry $registry,
  71. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  72. AttributeValueFactory $customAttributeFactory,
  73. \Magento\Eav\Model\Config $eavConfig,
  74. \Magento\Eav\Model\Entity\TypeFactory $eavTypeFactory,
  75. \Magento\Store\Model\StoreManagerInterface $storeManager,
  76. \Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
  77. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  78. \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionDataFactory,
  79. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  80. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  81. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  82. \Magento\Catalog\Model\Product\ReservedAttributeList $reservedAttributeList,
  83. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  84. DateTimeFormatterInterface $dateTimeFormatter,
  85. \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
  86. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  87. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  88. array $data = [],
  89. \Magento\Customer\Model\Metadata\AttributeMetadataCache $attributeMetadataCache = null
  90. ) {
  91. $this->indexerRegistry = $indexerRegistry;
  92. $this->attributeMetadataCache = $attributeMetadataCache ?: \Magento\Framework\App\ObjectManager::getInstance()
  93. ->get(\Magento\Customer\Model\Metadata\AttributeMetadataCache::class);
  94. parent::__construct(
  95. $context,
  96. $registry,
  97. $extensionFactory,
  98. $customAttributeFactory,
  99. $eavConfig,
  100. $eavTypeFactory,
  101. $storeManager,
  102. $resourceHelper,
  103. $universalFactory,
  104. $optionDataFactory,
  105. $dataObjectProcessor,
  106. $dataObjectHelper,
  107. $localeDate,
  108. $reservedAttributeList,
  109. $localeResolver,
  110. $dateTimeFormatter,
  111. $resource,
  112. $resourceCollection,
  113. $data
  114. );
  115. }
  116. /**
  117. * Init resource model
  118. *
  119. * @return void
  120. */
  121. protected function _construct()
  122. {
  123. $this->_init(\Magento\Customer\Model\ResourceModel\Attribute::class);
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. public function afterSave()
  129. {
  130. if ($this->isObjectNew() && (bool)$this->getData(EavAttributeInterface::IS_USED_IN_GRID)) {
  131. $this->_getResource()->addCommitCallback([$this, 'invalidate']);
  132. } elseif (!$this->isObjectNew() && $this->dataHasChangedFor(EavAttributeInterface::IS_USED_IN_GRID)) {
  133. $this->_getResource()->addCommitCallback([$this, 'invalidate']);
  134. }
  135. $this->attributeMetadataCache->clean();
  136. return parent::afterSave();
  137. }
  138. /**
  139. * @inheritdoc
  140. */
  141. public function afterDelete()
  142. {
  143. $this->attributeMetadataCache->clean();
  144. return parent::afterDelete();
  145. }
  146. /**
  147. * Init indexing process after customer delete
  148. *
  149. * @return \Magento\Framework\Model\AbstractModel
  150. */
  151. public function afterDeleteCommit()
  152. {
  153. if ($this->getData(EavAttributeInterface::IS_USED_IN_GRID) == true) {
  154. $this->invalidate();
  155. }
  156. return parent::afterDeleteCommit();
  157. }
  158. /**
  159. * Init indexing process after customer save
  160. *
  161. * @return void
  162. */
  163. public function invalidate()
  164. {
  165. /** @var \Magento\Framework\Indexer\IndexerInterface $indexer */
  166. $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
  167. $indexer->invalidate();
  168. }
  169. /**
  170. * Check whether attribute is searchable in admin grid and it is allowed
  171. *
  172. * @return bool
  173. */
  174. public function canBeSearchableInGrid()
  175. {
  176. return $this->getData('is_searchable_in_grid') && in_array($this->getFrontendInput(), ['text', 'textarea']);
  177. }
  178. /**
  179. * Check whether attribute is filterable in admin grid and it is allowed
  180. *
  181. * @return bool
  182. */
  183. public function canBeFilterableInGrid()
  184. {
  185. return $this->getData('is_filterable_in_grid')
  186. && in_array($this->getFrontendInput(), ['text', 'date', 'select', 'boolean']);
  187. }
  188. /**
  189. * @inheritdoc
  190. */
  191. public function __sleep()
  192. {
  193. $this->unsetData('entity_type');
  194. return array_diff(
  195. parent::__sleep(),
  196. ['indexerRegistry', '_website']
  197. );
  198. }
  199. /**
  200. * @inheritdoc
  201. */
  202. public function __wakeup()
  203. {
  204. parent::__wakeup();
  205. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  206. $this->indexerRegistry = $objectManager->get(\Magento\Framework\Indexer\IndexerRegistry::class);
  207. }
  208. }