Element.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Form;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * Eav Form Element Model
  10. *
  11. * @api
  12. * @method int getTypeId()
  13. * @method \Magento\Eav\Model\Form\Element setTypeId(int $value)
  14. * @method int getFieldsetId()
  15. * @method \Magento\Eav\Model\Form\Element setFieldsetId(int $value)
  16. * @method int getAttributeId()
  17. * @method \Magento\Eav\Model\Form\Element setAttributeId(int $value)
  18. * @method int getSortOrder()
  19. * @method \Magento\Eav\Model\Form\Element setSortOrder(int $value)
  20. * @since 100.0.2
  21. */
  22. class Element extends \Magento\Framework\Model\AbstractModel
  23. {
  24. /**
  25. * Prefix of model events names
  26. *
  27. * @var string
  28. */
  29. protected $_eventPrefix = 'eav_form_element';
  30. /**
  31. * @var \Magento\Eav\Model\Config
  32. */
  33. protected $_eavConfig;
  34. /**
  35. * @param \Magento\Framework\Model\Context $context
  36. * @param \Magento\Framework\Registry $registry
  37. * @param \Magento\Eav\Model\Config $eavConfig
  38. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  39. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  40. * @param array $data
  41. * @codeCoverageIgnore
  42. */
  43. public function __construct(
  44. \Magento\Framework\Model\Context $context,
  45. \Magento\Framework\Registry $registry,
  46. \Magento\Eav\Model\Config $eavConfig,
  47. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  48. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  49. array $data = []
  50. ) {
  51. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  52. $this->_eavConfig = $eavConfig;
  53. }
  54. /**
  55. * Initialize resource model
  56. *
  57. * @return void
  58. * @codeCoverageIgnore
  59. */
  60. protected function _construct()
  61. {
  62. $this->_init(\Magento\Eav\Model\ResourceModel\Form\Element::class);
  63. }
  64. /**
  65. * Validate data before save data
  66. *
  67. * @throws \Magento\Framework\Exception\LocalizedException
  68. * @return $this
  69. */
  70. public function beforeSave()
  71. {
  72. if (!$this->getTypeId()) {
  73. throw new LocalizedException(__('The form type is invalid. Reset the type and try again.'));
  74. }
  75. if (!$this->getAttributeId()) {
  76. throw new LocalizedException(__('The EAV attribute is invalid. Verify the attribute and try again.'));
  77. }
  78. return parent::beforeSave();
  79. }
  80. /**
  81. * Retrieve EAV Attribute instance
  82. *
  83. * @return \Magento\Eav\Model\Entity\Attribute
  84. */
  85. public function getAttribute()
  86. {
  87. if (!$this->hasData('attribute')) {
  88. $attribute = $this->_eavConfig->getAttribute($this->getEntityTypeId(), $this->getAttributeId());
  89. $this->setData('attribute', $attribute);
  90. }
  91. return $this->_getData('attribute');
  92. }
  93. }