Attribute.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel\Eav;
  7. use Magento\Catalog\Model\Attribute\LockValidatorInterface;
  8. use Magento\Framework\Api\AttributeValueFactory;
  9. use Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface;
  10. /**
  11. * Catalog attribute model
  12. *
  13. * @api
  14. * @method \Magento\Catalog\Model\ResourceModel\Eav\Attribute getFrontendInputRenderer()
  15. * @method string setFrontendInputRenderer(string $value)
  16. * @method int setIsGlobal(int $value)
  17. * @method int getSearchWeight()
  18. * @method int setSearchWeight(int $value)
  19. * @method bool getIsUsedForPriceRules()
  20. * @method int setIsUsedForPriceRules(int $value)
  21. *
  22. * @author Magento Core Team <core@magentocommerce.com>
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  25. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  26. * @since 100.0.2
  27. */
  28. class Attribute extends \Magento\Eav\Model\Entity\Attribute implements
  29. \Magento\Catalog\Api\Data\ProductAttributeInterface,
  30. \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface
  31. {
  32. const MODULE_NAME = 'Magento_Catalog';
  33. const ENTITY = 'catalog_eav_attribute';
  34. const KEY_IS_GLOBAL = 'is_global';
  35. /**
  36. * @var LockValidatorInterface
  37. */
  38. protected $attrLockValidator;
  39. /**
  40. * Event object name
  41. *
  42. * @var string
  43. */
  44. protected $_eventObject = 'attribute';
  45. /**
  46. * Array with labels
  47. *
  48. * @var array
  49. */
  50. protected static $_labels = null;
  51. /**
  52. * Event prefix
  53. *
  54. * @var string
  55. */
  56. protected $_eventPrefix = 'catalog_entity_attribute';
  57. /**
  58. * @var \Magento\Catalog\Model\Indexer\Product\Flat\Processor
  59. */
  60. protected $_productFlatIndexerProcessor;
  61. /**
  62. * @var \Magento\Catalog\Helper\Product\Flat\Indexer
  63. */
  64. protected $_productFlatIndexerHelper;
  65. /**
  66. * @var \Magento\Catalog\Model\Indexer\Product\Eav\Processor
  67. */
  68. protected $_indexerEavProcessor;
  69. /**
  70. * @var \Magento\Eav\Api\Data\AttributeExtensionFactory
  71. */
  72. private $eavAttributeFactory;
  73. /**
  74. * @param \Magento\Framework\Model\Context $context
  75. * @param \Magento\Framework\Registry $registry
  76. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  77. * @param AttributeValueFactory $customAttributeFactory
  78. * @param \Magento\Eav\Model\Config $eavConfig
  79. * @param \Magento\Eav\Model\Entity\TypeFactory $eavTypeFactory
  80. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  81. * @param \Magento\Eav\Model\ResourceModel\Helper $resourceHelper
  82. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  83. * @param \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionDataFactory
  84. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  85. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  86. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
  87. * @param \Magento\Catalog\Model\Product\ReservedAttributeList $reservedAttributeList
  88. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  89. * @param DateTimeFormatterInterface $dateTimeFormatter
  90. * @param \Magento\Catalog\Model\Indexer\Product\Flat\Processor $productFlatIndexerProcessor
  91. * @param \Magento\Catalog\Model\Indexer\Product\Eav\Processor $indexerEavProcessor
  92. * @param \Magento\Catalog\Helper\Product\Flat\Indexer $productFlatIndexerHelper
  93. * @param LockValidatorInterface $lockValidator
  94. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  95. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  96. * @param array $data
  97. * @param \Magento\Eav\Api\Data\AttributeExtensionFactory|null $eavAttributeFactory
  98. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  99. */
  100. public function __construct(
  101. \Magento\Framework\Model\Context $context,
  102. \Magento\Framework\Registry $registry,
  103. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  104. AttributeValueFactory $customAttributeFactory,
  105. \Magento\Eav\Model\Config $eavConfig,
  106. \Magento\Eav\Model\Entity\TypeFactory $eavTypeFactory,
  107. \Magento\Store\Model\StoreManagerInterface $storeManager,
  108. \Magento\Eav\Model\ResourceModel\Helper $resourceHelper,
  109. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  110. \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionDataFactory,
  111. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  112. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  113. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
  114. \Magento\Catalog\Model\Product\ReservedAttributeList $reservedAttributeList,
  115. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  116. DateTimeFormatterInterface $dateTimeFormatter,
  117. \Magento\Catalog\Model\Indexer\Product\Flat\Processor $productFlatIndexerProcessor,
  118. \Magento\Catalog\Model\Indexer\Product\Eav\Processor $indexerEavProcessor,
  119. \Magento\Catalog\Helper\Product\Flat\Indexer $productFlatIndexerHelper,
  120. LockValidatorInterface $lockValidator,
  121. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  122. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  123. array $data = [],
  124. \Magento\Eav\Api\Data\AttributeExtensionFactory $eavAttributeFactory = null
  125. ) {
  126. $this->_indexerEavProcessor = $indexerEavProcessor;
  127. $this->_productFlatIndexerProcessor = $productFlatIndexerProcessor;
  128. $this->_productFlatIndexerHelper = $productFlatIndexerHelper;
  129. $this->attrLockValidator = $lockValidator;
  130. $this->eavAttributeFactory = $eavAttributeFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
  131. ->get(\Magento\Eav\Api\Data\AttributeExtensionFactory::class);
  132. parent::__construct(
  133. $context,
  134. $registry,
  135. $extensionFactory,
  136. $customAttributeFactory,
  137. $eavConfig,
  138. $eavTypeFactory,
  139. $storeManager,
  140. $resourceHelper,
  141. $universalFactory,
  142. $optionDataFactory,
  143. $dataObjectProcessor,
  144. $dataObjectHelper,
  145. $localeDate,
  146. $reservedAttributeList,
  147. $localeResolver,
  148. $dateTimeFormatter,
  149. $resource,
  150. $resourceCollection,
  151. $data
  152. );
  153. }
  154. /**
  155. * Init model
  156. *
  157. * @return void
  158. */
  159. protected function _construct()
  160. {
  161. $this->_init(\Magento\Catalog\Model\ResourceModel\Attribute::class);
  162. }
  163. /**
  164. * Processing object before save data
  165. *
  166. * @return \Magento\Framework\Model\AbstractModel
  167. * @throws \Magento\Framework\Exception\LocalizedException
  168. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  169. */
  170. public function beforeSave()
  171. {
  172. $this->setData('modulePrefix', self::MODULE_NAME);
  173. if (isset($this->_origData[self::KEY_IS_GLOBAL])) {
  174. if (!isset($this->_data[self::KEY_IS_GLOBAL])) {
  175. $this->_data[self::KEY_IS_GLOBAL] = self::SCOPE_GLOBAL;
  176. }
  177. if ($this->_data[self::KEY_IS_GLOBAL] != $this->_origData[self::KEY_IS_GLOBAL]) {
  178. try {
  179. $this->attrLockValidator->validate($this);
  180. } catch (\Magento\Framework\Exception\LocalizedException $exception) {
  181. throw new \Magento\Framework\Exception\LocalizedException(
  182. __('Do not change the scope. %1', $exception->getMessage())
  183. );
  184. }
  185. }
  186. }
  187. if ($this->getFrontendInput() == 'price') {
  188. if (!$this->getBackendModel()) {
  189. $this->setBackendModel(\Magento\Catalog\Model\Product\Attribute\Backend\Price::class);
  190. }
  191. }
  192. if ($this->getFrontendInput() == 'textarea') {
  193. if ($this->getIsWysiwygEnabled()) {
  194. $this->setIsHtmlAllowedOnFront(1);
  195. }
  196. }
  197. if (!$this->getIsSearchable()) {
  198. $this->setIsVisibleInAdvancedSearch(false);
  199. }
  200. return parent::beforeSave();
  201. }
  202. /**
  203. * Processing object after save data
  204. *
  205. * @return \Magento\Framework\Model\AbstractModel
  206. */
  207. public function afterSave()
  208. {
  209. /**
  210. * Fix saving attribute in admin
  211. */
  212. $this->_eavConfig->clear();
  213. if ($this->_isOriginalEnabledInFlat() != $this->_isEnabledInFlat()) {
  214. $this->_productFlatIndexerProcessor->markIndexerAsInvalid();
  215. }
  216. if ($this->_isOriginalIndexable() !== $this->isIndexable()
  217. || ($this->isIndexable() && $this->dataHasChangedFor(self::KEY_IS_GLOBAL))
  218. ) {
  219. $this->_indexerEavProcessor->markIndexerAsInvalid();
  220. }
  221. $this->_source = null;
  222. return parent::afterSave();
  223. }
  224. /**
  225. * Is attribute enabled for flat indexing
  226. *
  227. * @return bool
  228. * @since 103.0.0
  229. */
  230. public function isEnabledInFlat()
  231. {
  232. return $this->_isEnabledInFlat();
  233. }
  234. /**
  235. * Is attribute enabled for flat indexing
  236. *
  237. * @return bool
  238. */
  239. protected function _isEnabledInFlat()
  240. {
  241. return $this->_getData('backend_type') == 'static'
  242. || $this->_productFlatIndexerHelper->isAddFilterableAttributes()
  243. && $this->_getData('is_filterable') > 0
  244. || $this->_getData('used_in_product_listing') == 1
  245. || $this->_getData('used_for_sort_by') == 1;
  246. }
  247. /**
  248. * Is original attribute enabled for flat indexing
  249. *
  250. * @return bool
  251. */
  252. protected function _isOriginalEnabledInFlat()
  253. {
  254. return $this->getOrigData('backend_type') == 'static'
  255. || $this->_productFlatIndexerHelper->isAddFilterableAttributes()
  256. && $this->getOrigData('is_filterable') > 0
  257. || $this->getOrigData('used_in_product_listing') == 1
  258. || $this->getOrigData('used_for_sort_by') == 1;
  259. }
  260. /**
  261. * Register indexing event before delete catalog eav attribute
  262. *
  263. * @return $this
  264. * @throws \Magento\Framework\Exception\LocalizedException
  265. */
  266. public function beforeDelete()
  267. {
  268. $this->attrLockValidator->validate($this);
  269. return parent::beforeDelete();
  270. }
  271. /**
  272. * Init indexing process after catalog eav attribute delete commit
  273. *
  274. * @return $this
  275. */
  276. public function afterDeleteCommit()
  277. {
  278. parent::afterDeleteCommit();
  279. if ($this->_isOriginalEnabledInFlat()) {
  280. $this->_productFlatIndexerProcessor->markIndexerAsInvalid();
  281. }
  282. if ($this->_isOriginalIndexable()) {
  283. $this->_indexerEavProcessor->markIndexerAsInvalid();
  284. }
  285. return $this;
  286. }
  287. /**
  288. * Return is attribute global
  289. *
  290. * @return integer
  291. */
  292. public function getIsGlobal()
  293. {
  294. return $this->_getData(self::KEY_IS_GLOBAL);
  295. }
  296. /**
  297. * Retrieve attribute is global scope flag
  298. *
  299. * @return bool
  300. */
  301. public function isScopeGlobal()
  302. {
  303. return $this->getIsGlobal() == self::SCOPE_GLOBAL;
  304. }
  305. /**
  306. * Retrieve attribute is website scope website
  307. *
  308. * @return bool
  309. */
  310. public function isScopeWebsite()
  311. {
  312. return $this->getIsGlobal() == self::SCOPE_WEBSITE;
  313. }
  314. /**
  315. * Retrieve attribute is store scope flag
  316. *
  317. * @return bool
  318. */
  319. public function isScopeStore()
  320. {
  321. return !$this->isScopeGlobal() && !$this->isScopeWebsite();
  322. }
  323. /**
  324. * Retrieve store id
  325. *
  326. * @return int
  327. */
  328. public function getStoreId()
  329. {
  330. $dataObject = $this->getDataObject();
  331. if ($dataObject) {
  332. return $dataObject->getStoreId();
  333. }
  334. return $this->_getData('store_id');
  335. }
  336. /**
  337. * Retrieve apply to products array
  338. *
  339. * Return empty array if applied to all products
  340. *
  341. * @return string[]
  342. */
  343. public function getApplyTo()
  344. {
  345. $applyTo = $this->_getData(self::APPLY_TO) ?: [];
  346. if (!is_array($applyTo)) {
  347. $applyTo = explode(',', $applyTo);
  348. }
  349. return $applyTo;
  350. }
  351. /**
  352. * Retrieve source model
  353. *
  354. * @return \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
  355. */
  356. public function getSourceModel()
  357. {
  358. $model = $this->_getData('source_model');
  359. if (empty($model)) {
  360. if ($this->getBackendType() == 'int' && $this->getFrontendInput() == 'select') {
  361. return $this->_getDefaultSourceModel();
  362. }
  363. }
  364. return $model;
  365. }
  366. /**
  367. * Whether allowed for rule condition
  368. *
  369. * @return bool
  370. */
  371. public function isAllowedForRuleCondition()
  372. {
  373. $allowedInputTypes = [
  374. 'boolean',
  375. 'date',
  376. 'datetime',
  377. 'multiselect',
  378. 'price',
  379. 'select',
  380. 'text',
  381. 'textarea',
  382. 'weight',
  383. ];
  384. return $this->getIsVisible() && in_array($this->getFrontendInput(), $allowedInputTypes);
  385. }
  386. /**
  387. * Get default attribute source model
  388. *
  389. * @return string
  390. */
  391. public function _getDefaultSourceModel()
  392. {
  393. return \Magento\Eav\Model\Entity\Attribute\Source\Table::class;
  394. }
  395. /**
  396. * Check is an attribute used in EAV index
  397. *
  398. * @return bool
  399. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  400. */
  401. public function isIndexable()
  402. {
  403. // exclude price attribute
  404. if ($this->getAttributeCode() == 'price') {
  405. return false;
  406. }
  407. if ($this->getAttributeCode() == 'visibility') {
  408. return true;
  409. }
  410. if (!$this->getIsFilterableInSearch() && !$this->getIsVisibleInAdvancedSearch() && !$this->getIsFilterable()) {
  411. return false;
  412. }
  413. $backendType = $this->getBackendType();
  414. $frontendInput = $this->getFrontendInput();
  415. if ($backendType == 'int' && $frontendInput == 'select') {
  416. return true;
  417. } elseif ($backendType == 'varchar' && $frontendInput == 'multiselect') {
  418. return true;
  419. } elseif ($backendType == 'decimal') {
  420. return true;
  421. }
  422. return false;
  423. }
  424. /**
  425. * Is original attribute config indexable
  426. *
  427. * @return bool
  428. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  429. */
  430. protected function _isOriginalIndexable()
  431. {
  432. // exclude price attribute
  433. if ($this->getOrigData('attribute_code') == 'price') {
  434. return false;
  435. }
  436. if (!$this->getOrigData('is_filterable_in_search')
  437. && !$this->getOrigData('is_visible_in_advanced_search')
  438. && !$this->getOrigData('is_filterable')) {
  439. return false;
  440. }
  441. $backendType = $this->getOrigData('backend_type');
  442. $frontendInput = $this->getOrigData('frontend_input');
  443. if ($backendType == 'int' && ($frontendInput == 'select' || $frontendInput == 'boolean')) {
  444. return true;
  445. } elseif ($backendType == 'varchar' && $frontendInput == 'multiselect') {
  446. return true;
  447. } elseif ($backendType == 'decimal') {
  448. return true;
  449. }
  450. return false;
  451. }
  452. /**
  453. * Retrieve index type for indexable attribute
  454. *
  455. * @return string|false
  456. */
  457. public function getIndexType()
  458. {
  459. if (!$this->isIndexable()) {
  460. return false;
  461. }
  462. if ($this->getBackendType() == 'decimal') {
  463. return 'decimal';
  464. }
  465. return 'source';
  466. }
  467. /**
  468. * @inheritdoc
  469. * @codeCoverageIgnoreStart
  470. */
  471. public function getIsWysiwygEnabled()
  472. {
  473. return $this->_getData(self::IS_WYSIWYG_ENABLED);
  474. }
  475. /**
  476. * @inheritdoc
  477. */
  478. public function getIsHtmlAllowedOnFront()
  479. {
  480. return $this->_getData(self::IS_HTML_ALLOWED_ON_FRONT);
  481. }
  482. /**
  483. * @inheritdoc
  484. */
  485. public function getUsedForSortBy()
  486. {
  487. return $this->_getData(self::USED_FOR_SORT_BY);
  488. }
  489. /**
  490. * @inheritdoc
  491. */
  492. public function getIsFilterable()
  493. {
  494. return $this->_getData(self::IS_FILTERABLE);
  495. }
  496. /**
  497. * @inheritdoc
  498. */
  499. public function getIsFilterableInSearch()
  500. {
  501. return $this->_getData(self::IS_FILTERABLE_IN_SEARCH);
  502. }
  503. /**
  504. * @inheritdoc
  505. */
  506. public function getIsUsedInGrid()
  507. {
  508. return (bool)$this->_getData(self::IS_USED_IN_GRID);
  509. }
  510. /**
  511. * @inheritdoc
  512. */
  513. public function getIsVisibleInGrid()
  514. {
  515. return (bool)$this->_getData(self::IS_VISIBLE_IN_GRID);
  516. }
  517. /**
  518. * @inheritdoc
  519. */
  520. public function getIsFilterableInGrid()
  521. {
  522. return (bool)$this->_getData(self::IS_FILTERABLE_IN_GRID);
  523. }
  524. /**
  525. * @inheritdoc
  526. */
  527. public function getPosition()
  528. {
  529. return $this->_getData(self::POSITION);
  530. }
  531. /**
  532. * @inheritdoc
  533. */
  534. public function getIsSearchable()
  535. {
  536. return $this->_getData(self::IS_SEARCHABLE);
  537. }
  538. /**
  539. * @inheritdoc
  540. */
  541. public function getIsVisibleInAdvancedSearch()
  542. {
  543. return $this->_getData(self::IS_VISIBLE_IN_ADVANCED_SEARCH);
  544. }
  545. /**
  546. * @inheritdoc
  547. */
  548. public function getIsComparable()
  549. {
  550. return $this->_getData(self::IS_COMPARABLE);
  551. }
  552. /**
  553. * @inheritdoc
  554. */
  555. public function getIsUsedForPromoRules()
  556. {
  557. return $this->_getData(self::IS_USED_FOR_PROMO_RULES);
  558. }
  559. /**
  560. * @inheritdoc
  561. */
  562. public function getIsVisibleOnFront()
  563. {
  564. return $this->_getData(self::IS_VISIBLE_ON_FRONT);
  565. }
  566. /**
  567. * @inheritdoc
  568. */
  569. public function getUsedInProductListing()
  570. {
  571. return $this->_getData(self::USED_IN_PRODUCT_LISTING);
  572. }
  573. /**
  574. * @inheritdoc
  575. */
  576. public function getIsVisible()
  577. {
  578. return $this->_getData(self::IS_VISIBLE);
  579. }
  580. //@codeCoverageIgnoreEnd
  581. /**
  582. * @inheritdoc
  583. */
  584. public function getScope()
  585. {
  586. if ($this->isScopeGlobal()) {
  587. return self::SCOPE_GLOBAL_TEXT;
  588. } elseif ($this->isScopeWebsite()) {
  589. return self::SCOPE_WEBSITE_TEXT;
  590. } else {
  591. return self::SCOPE_STORE_TEXT;
  592. }
  593. }
  594. /**
  595. * Set whether WYSIWYG is enabled flag
  596. *
  597. * @param bool $isWysiwygEnabled
  598. * @return $this
  599. */
  600. public function setIsWysiwygEnabled($isWysiwygEnabled)
  601. {
  602. return $this->setData(self::IS_WYSIWYG_ENABLED, $isWysiwygEnabled);
  603. }
  604. /**
  605. * Set whether the HTML tags are allowed on the frontend
  606. *
  607. * @param bool $isHtmlAllowedOnFront
  608. * @return $this
  609. */
  610. public function setIsHtmlAllowedOnFront($isHtmlAllowedOnFront)
  611. {
  612. return $this->setData(self::IS_HTML_ALLOWED_ON_FRONT, $isHtmlAllowedOnFront);
  613. }
  614. /**
  615. * Set whether it is used for sorting in product listing
  616. *
  617. * @param bool $usedForSortBy
  618. * @return $this
  619. */
  620. public function setUsedForSortBy($usedForSortBy)
  621. {
  622. return $this->setData(self::USED_FOR_SORT_BY, $usedForSortBy);
  623. }
  624. /**
  625. * Set whether it used in layered navigation
  626. *
  627. * @param bool $isFilterable
  628. * @return $this
  629. */
  630. public function setIsFilterable($isFilterable)
  631. {
  632. return $this->setData(self::IS_FILTERABLE, $isFilterable);
  633. }
  634. /**
  635. * Set whether it is used in search results layered navigation
  636. *
  637. * @param bool $isFilterableInSearch
  638. * @return $this
  639. */
  640. public function setIsFilterableInSearch($isFilterableInSearch)
  641. {
  642. return $this->setData(self::IS_FILTERABLE_IN_SEARCH, $isFilterableInSearch);
  643. }
  644. /**
  645. * Set position
  646. *
  647. * @param int $position
  648. * @return $this
  649. */
  650. public function setPosition($position)
  651. {
  652. return $this->setData(self::POSITION, $position);
  653. }
  654. /**
  655. * Set apply to value for the element
  656. *
  657. * @param string[]|string $applyTo
  658. * @return $this
  659. */
  660. public function setApplyTo($applyTo)
  661. {
  662. if (is_array($applyTo)) {
  663. $applyTo = implode(',', $applyTo);
  664. }
  665. return $this->setData(self::APPLY_TO, $applyTo);
  666. }
  667. /**
  668. * Whether the attribute can be used in Quick Search
  669. *
  670. * @param string $isSearchable
  671. * @return $this
  672. */
  673. public function setIsSearchable($isSearchable)
  674. {
  675. return $this->setData(self::IS_SEARCHABLE, $isSearchable);
  676. }
  677. /**
  678. * Set whether the attribute can be used in Advanced Search
  679. *
  680. * @param string $isVisibleInAdvancedSearch
  681. * @return $this
  682. */
  683. public function setIsVisibleInAdvancedSearch($isVisibleInAdvancedSearch)
  684. {
  685. return $this->setData(self::IS_VISIBLE_IN_ADVANCED_SEARCH, $isVisibleInAdvancedSearch);
  686. }
  687. /**
  688. * Set whether the attribute can be compared on the frontend
  689. *
  690. * @param string $isComparable
  691. * @return $this
  692. */
  693. public function setIsComparable($isComparable)
  694. {
  695. return $this->setData(self::IS_COMPARABLE, $isComparable);
  696. }
  697. /**
  698. * Set whether the attribute can be used for promo rules
  699. *
  700. * @param string $isUsedForPromoRules
  701. * @return $this
  702. */
  703. public function setIsUsedForPromoRules($isUsedForPromoRules)
  704. {
  705. return $this->setData(self::IS_USED_FOR_PROMO_RULES, $isUsedForPromoRules);
  706. }
  707. /**
  708. * Set whether the attribute is visible on the frontend
  709. *
  710. * @param string $isVisibleOnFront
  711. * @return $this
  712. */
  713. public function setIsVisibleOnFront($isVisibleOnFront)
  714. {
  715. return $this->setData(self::IS_VISIBLE_ON_FRONT, $isVisibleOnFront);
  716. }
  717. /**
  718. * Set whether the attribute can be used in product listing
  719. *
  720. * @param string $usedInProductListing
  721. * @return $this
  722. */
  723. public function setUsedInProductListing($usedInProductListing)
  724. {
  725. return $this->setData(self::USED_IN_PRODUCT_LISTING, $usedInProductListing);
  726. }
  727. /**
  728. * Set whether attribute is visible on frontend.
  729. *
  730. * @param bool $isVisible
  731. * @return $this
  732. */
  733. public function setIsVisible($isVisible)
  734. {
  735. return $this->setData(self::IS_VISIBLE, $isVisible);
  736. }
  737. /**
  738. * Set attribute scope
  739. *
  740. * @param string $scope
  741. * @return $this
  742. */
  743. public function setScope($scope)
  744. {
  745. if ($scope == self::SCOPE_GLOBAL_TEXT) {
  746. return $this->setData(self::KEY_IS_GLOBAL, self::SCOPE_GLOBAL);
  747. } elseif ($scope == self::SCOPE_WEBSITE_TEXT) {
  748. return $this->setData(self::KEY_IS_GLOBAL, self::SCOPE_WEBSITE);
  749. } elseif ($scope == self::SCOPE_STORE_TEXT) {
  750. return $this->setData(self::KEY_IS_GLOBAL, self::SCOPE_STORE);
  751. } else {
  752. //Ignore unrecognized scope
  753. return $this;
  754. }
  755. }
  756. /**
  757. * @inheritdoc
  758. */
  759. public function afterDelete()
  760. {
  761. $this->_eavConfig->clear();
  762. return parent::afterDelete();
  763. }
  764. /**
  765. * @inheritdoc
  766. * @since 100.0.9
  767. */
  768. public function __sleep()
  769. {
  770. $this->unsetData('entity_type');
  771. return array_diff(
  772. parent::__sleep(),
  773. ['_indexerEavProcessor', '_productFlatIndexerProcessor', '_productFlatIndexerHelper', 'attrLockValidator']
  774. );
  775. }
  776. /**
  777. * @inheritdoc
  778. * @since 100.0.9
  779. */
  780. public function __wakeup()
  781. {
  782. parent::__wakeup();
  783. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  784. $this->_indexerEavProcessor = $objectManager->get(\Magento\Catalog\Model\Indexer\Product\Flat\Processor::class);
  785. $this->_productFlatIndexerProcessor = $objectManager->get(
  786. \Magento\Catalog\Model\Indexer\Product\Eav\Processor::class
  787. );
  788. $this->_productFlatIndexerHelper = $objectManager->get(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
  789. $this->attrLockValidator = $objectManager->get(LockValidatorInterface::class);
  790. }
  791. /**
  792. * @inheritdoc
  793. * @since 102.0.0
  794. */
  795. public function setIsUsedInGrid($isUsedInGrid)
  796. {
  797. $this->setData(self::IS_USED_IN_GRID, $isUsedInGrid);
  798. return $this;
  799. }
  800. /**
  801. * @inheritdoc
  802. * @since 102.0.0
  803. */
  804. public function setIsVisibleInGrid($isVisibleInGrid)
  805. {
  806. $this->setData(self::IS_VISIBLE_IN_GRID, $isVisibleInGrid);
  807. return $this;
  808. }
  809. /**
  810. * @inheritdoc
  811. * @since 102.0.0
  812. */
  813. public function setIsFilterableInGrid($isFilterableInGrid)
  814. {
  815. $this->setData(self::IS_FILTERABLE_IN_GRID, $isFilterableInGrid);
  816. return $this;
  817. }
  818. }