Attribute.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * EAV attribute resource model (Using Forms)
  8. *
  9. * @method \Magento\Eav\Model\Attribute\Data\AbstractData|null getDataModel()
  10. * Get data model linked to attribute or null.
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. namespace Magento\Eav\Model;
  15. use Magento\Store\Model\Website;
  16. /**
  17. * @api
  18. * @since 100.0.2
  19. */
  20. class Attribute extends \Magento\Eav\Model\Entity\Attribute
  21. {
  22. /**
  23. * Name of the module
  24. * Override it
  25. */
  26. //const MODULE_NAME = 'Magento_Eav';
  27. /**
  28. * Name of the module
  29. * Override it
  30. */
  31. protected $_eventObject = 'attribute';
  32. /**
  33. * Active Website instance
  34. *
  35. * @var Website
  36. */
  37. protected $_website;
  38. /**
  39. * Set active website instance
  40. *
  41. * @param Website|int $website
  42. * @return $this
  43. * @codeCoverageIgnore
  44. */
  45. public function setWebsite($website)
  46. {
  47. $this->_website = $this->_storeManager->getWebsite($website);
  48. return $this;
  49. }
  50. /**
  51. * Return active website instance
  52. *
  53. * @return Website
  54. */
  55. public function getWebsite()
  56. {
  57. if ($this->_website === null) {
  58. $this->_website = $this->_storeManager->getWebsite();
  59. }
  60. return $this->_website;
  61. }
  62. /**
  63. * Processing object after save data
  64. *
  65. * @return $this
  66. */
  67. public function afterSave()
  68. {
  69. $this->_eavConfig->clear();
  70. return parent::afterSave();
  71. }
  72. /**
  73. * Return forms in which the attribute
  74. *
  75. * @return array
  76. */
  77. public function getUsedInForms()
  78. {
  79. $forms = $this->getData('used_in_forms');
  80. if ($forms === null) {
  81. $forms = $this->_getResource()->getUsedInForms($this);
  82. $this->setData('used_in_forms', $forms);
  83. }
  84. return $forms;
  85. }
  86. /**
  87. * Return validate rules
  88. *
  89. * @return array
  90. */
  91. public function getValidateRules()
  92. {
  93. $rules = $this->getData('validate_rules');
  94. if (is_array($rules)) {
  95. return $rules;
  96. } elseif (!empty($rules)) {
  97. return (array)$this->getSerializer()->unserialize($rules);
  98. }
  99. return [];
  100. }
  101. /**
  102. * Set validate rules
  103. *
  104. * @param array|string $rules
  105. * @return $this
  106. */
  107. public function setValidateRules($rules)
  108. {
  109. if (empty($rules)) {
  110. $rules = null;
  111. } elseif (is_array($rules)) {
  112. $rules = $this->getSerializer()->serialize($rules);
  113. }
  114. $this->setData('validate_rules', $rules);
  115. return $this;
  116. }
  117. /**
  118. * Return scope value by key
  119. *
  120. * @param string $key
  121. * @return mixed
  122. */
  123. protected function _getScopeValue($key)
  124. {
  125. $scopeKey = sprintf('scope_%s', $key);
  126. if ($this->getData($scopeKey) !== null) {
  127. return $this->getData($scopeKey);
  128. }
  129. return $this->getData($key);
  130. }
  131. /**
  132. * Return is attribute value required
  133. *
  134. * @return mixed
  135. * @codeCoverageIgnore
  136. */
  137. public function getIsRequired()
  138. {
  139. return $this->_getScopeValue('is_required');
  140. }
  141. /**
  142. * Return is visible attribute flag
  143. *
  144. * @return mixed
  145. * @codeCoverageIgnore
  146. */
  147. public function getIsVisible()
  148. {
  149. return $this->_getScopeValue('is_visible');
  150. }
  151. /**
  152. * Return default value for attribute
  153. *
  154. * @return mixed
  155. * @codeCoverageIgnore
  156. */
  157. public function getDefaultValue()
  158. {
  159. return $this->_getScopeValue('default_value');
  160. }
  161. /**
  162. * Return count of lines for multiply line attribute
  163. *
  164. * @return mixed
  165. * @codeCoverageIgnore
  166. */
  167. public function getMultilineCount()
  168. {
  169. return $this->_getScopeValue('multiline_count');
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function afterDelete()
  175. {
  176. $this->_eavConfig->clear();
  177. return parent::afterDelete();
  178. }
  179. }