Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Extend.php
Go to the documentation of this file.
1 <?php
13 
14 class Extend extends \Magento\Catalog\Block\Adminhtml\Form\Renderer\Fieldset\Element
15 {
19  private $template = 'Magento_Bundle::catalog/product/edit/tab/attributes/extend.phtml';
20 
21  const DYNAMIC = 0;
22 
23  const FIXED = 1;
24 
30  protected $_coreRegistry = null;
31 
35  private $formFactory;
36 
43  public function __construct(
44  \Magento\Backend\Block\Template\Context $context,
45  \Magento\Framework\Registry $registry,
46  \Magento\Framework\Data\FormFactory $formFactory,
47  array $data = []
48  ) {
49  $this->_coreRegistry = $registry;
50  parent::__construct($context, $data);
51  $this->formFactory = $formFactory;
52  }
53 
59  protected function _construct()
60  {
61  parent::_construct();
62  $this->setCanEditPrice(true);
63  $this->setCanReadPrice(true);
64  }
65 
71  public function getElementHtml()
72  {
73  $templateFile = $this->getTemplateFile($this->template);
74  return $this->fetchView($templateFile);
75  }
76 
82  public function getParentElementHtml()
83  {
84  return parent::getElementHtml();
85  }
86 
90  public function getOptions()
91  {
92  return [
93  [
94  'value' => '',
95  'label' => __('-- Select --')
96  ],
97  [
98  'value' => self::DYNAMIC,
99  'label' => __('Dynamic')
100  ],
101  [
102  'value' => self::FIXED,
103  'label' => __('Fixed')
104  ]
105  ];
106  }
107 
111  public function isDisabledField()
112  {
113  return $this->_getData('is_disabled_field')
114  || ($this->getProduct()->getId()
115  && $this->getAttribute()->getAttributeCode() === 'price'
116  )
117  || $this->getElement()->getReadonly();
118  }
119 
123  public function getProduct()
124  {
125  if (!$this->getData('product')) {
126  $this->setData('product', $this->_coreRegistry->registry('product'));
127  }
128  return $this->getData('product');
129  }
130 
137  {
138  $form = $this->formFactory->create();
139  return $form->addField(
141  'select',
142  [
143  'name' => "product[{$switchAttributeCode}]",
144  'values' => $this->getOptions(),
145  'class' => 'required-entry next-toinput',
146  'no_span' => true,
147  'disabled' => $this->isDisabledField(),
148  'value' => $this->getProduct()->getData($switchAttributeCode),
149  ]
150  );
151  }
152 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
setData($key, $value=null)
Definition: DataObject.php:72
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, array $data=[])
Definition: Extend.php:43
$switchAttributeCode
Definition: extend.phtml:15