Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeDataFactory.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Eav\Model;
8 
15 {
16  const OUTPUT_FORMAT_JSON = 'json';
17  const OUTPUT_FORMAT_TEXT = 'text';
18  const OUTPUT_FORMAT_HTML = 'html';
19  const OUTPUT_FORMAT_PDF = 'pdf';
20  const OUTPUT_FORMAT_ONELINE = 'oneline';
21  const OUTPUT_FORMAT_ARRAY = 'array';
22 
23  // available only for multiply attributes
24 
25  // available only for multiply attributes
26  protected $_dataModels = [];
27 
31  protected $_objectManager;
32 
36  protected $string;
37 
43  public function __construct(
45  \Magento\Framework\Stdlib\StringUtils $string
46  ) {
47  $this->_objectManager = $objectManager;
48  $this->string = $string;
49  }
50 
59  public function create(\Magento\Eav\Model\Attribute $attribute, \Magento\Framework\Model\AbstractModel $entity)
60  {
61  /* @var $dataModel \Magento\Eav\Model\Attribute\Data\AbstractData */
62  $dataModelClass = $attribute->getDataModel();
63  if (!empty($dataModelClass)) {
64  if (empty($this->_dataModels[$dataModelClass])) {
65  $dataModel = $this->_objectManager->create($dataModelClass);
66  $this->_dataModels[$dataModelClass] = $dataModel;
67  } else {
68  $dataModel = $this->_dataModels[$dataModelClass];
69  }
70  } else {
71  if (empty($this->_dataModels[$attribute->getFrontendInput()])) {
72  $dataModelClass = sprintf(
73  'Magento\Eav\Model\Attribute\Data\%s',
74  $this->string->upperCaseWords($attribute->getFrontendInput())
75  );
76  $dataModel = $this->_objectManager->create($dataModelClass);
77  $this->_dataModels[$attribute->getFrontendInput()] = $dataModel;
78  } else {
79  $dataModel = $this->_dataModels[$attribute->getFrontendInput()];
80  }
81  }
82 
83  $dataModel->setAttribute($attribute);
84  $dataModel->setEntity($entity);
85 
86  return $dataModel;
87  }
88 }
$objectManager
Definition: bootstrap.php:17
create(\Magento\Eav\Model\Attribute $attribute, \Magento\Framework\Model\AbstractModel $entity)
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\Stdlib\StringUtils $string)
$entity
Definition: element.phtml:22