AttributeLoader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Entity;
  7. use Magento\Eav\Model\Config;
  8. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  9. use Magento\Framework\DataObject;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\ObjectManagerInterface;
  12. /**
  13. * Attribute loader
  14. */
  15. class AttributeLoader implements AttributeLoaderInterface
  16. {
  17. /**
  18. * Default attributes
  19. *
  20. * @var array
  21. */
  22. private $defaultAttributes = [];
  23. /**
  24. * @var ObjectManagerInterface
  25. */
  26. protected $objectManager;
  27. /**
  28. * @var Config
  29. */
  30. private $config;
  31. /**
  32. * Constructor
  33. *
  34. * @param Config $config
  35. * @param ObjectManagerInterface $objectManager
  36. */
  37. public function __construct(
  38. Config $config,
  39. ObjectManagerInterface $objectManager
  40. ) {
  41. $this->config = $config;
  42. $this->objectManager = $objectManager;
  43. }
  44. /**
  45. * Retrieve configuration for all attributes
  46. *
  47. * @param AbstractEntity $resource
  48. * @param DataObject|null $object
  49. * @return AbstractEntity
  50. * @throws LocalizedException
  51. */
  52. public function loadAllAttributes(AbstractEntity $resource, DataObject $object = null)
  53. {
  54. $attributes = $this->config->getEntityAttributes($resource->getEntityType(), $object);
  55. $attributeCodes = array_keys($attributes);
  56. /**
  57. * Check and init default attributes
  58. */
  59. $defaultAttributesCodes = array_diff($resource->getDefaultAttributes(), $attributeCodes);
  60. $resource->unsetAttributes();
  61. foreach ($defaultAttributesCodes as $attributeCode) {
  62. $resource->addAttribute($this->_getDefaultAttribute($resource, $attributeCode), $object);
  63. }
  64. foreach ($attributes as $attributeCode => $attribute) {
  65. $resource->addAttribute($attribute, $object);
  66. }
  67. return $resource;
  68. }
  69. /**
  70. * Return default static virtual attribute that doesn't exists in EAV attributes
  71. *
  72. * @param AbstractEntity $resource
  73. * @param string $attributeCode
  74. * @return Attribute
  75. */
  76. protected function _getDefaultAttribute(AbstractEntity $resource, $attributeCode)
  77. {
  78. $entityTypeId = $resource->getEntityType()->getId();
  79. if (!isset($this->defaultAttributes[$entityTypeId][$attributeCode])) {
  80. $attribute = $this->objectManager->create($resource->getEntityType()->getAttributeModel())
  81. ->setAttributeCode($attributeCode)
  82. ->setBackendType(AbstractAttribute::TYPE_STATIC)
  83. ->setIsGlobal(1)
  84. ->setEntityType($resource->getEntityType())
  85. ->setEntityTypeId($resource->getEntityType()->getId());
  86. $this->defaultAttributes[$entityTypeId][$attributeCode] = $attribute;
  87. }
  88. return $this->defaultAttributes[$entityTypeId][$attributeCode];
  89. }
  90. }