I have magento2 admin custom module with image upload functionality. I want to upload image from admin. what code should apply for display image field in from, upload image, also image display in edit action.
Thanks
File Path : app\code\[Vendor]\[Module]\Block\Adminhtml\Emp\Edit\Tab\Main.php
/**
* Prepare form
*
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry('emp_post');
$isElementDisabled = false;
/** @var \Magento\Framework\Data\Form $form */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('page_');
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Employee Information')]);
if ($model->getId()) {
$fieldset->addField('customer_id', 'hidden', ['name' => 'customer_id']);
}
$fieldset->addField(
'firstname',
'text',
[
'name' => 'firstname',
'label' => __('First Name'),
'title' => __('First Name'),
'required' => true,
'disabled' => $isElementDisabled,
'value' =>'abc'
]
);
$fieldset->addField(
'lastname',
'text',
[
'name' => 'lastname',
'label' => __('Last Name'),
'title' => __('Last Name'),
'required' => true,
'disabled' => $isElementDisabled,
'value' =>'abc'
]
);
$fieldset->addField(
'email',
'text',
[
'name' => 'email',
'label' => __('Email Address'),
'title' => __('Email Address'),
'required' => true,
'disabled' => $isElementDisabled,
'value' =>'abc'
]
);
$fieldset->addField(
'image',
'image',
array(
'name' => 'image',
'label' => __('Image'),
'title' => __('Image')
)
);
$fieldset->addField(
'telephone',
'text',
[
'name' => 'telephone',
'label' => __('Telephone'),
'title' => __('Telephone'),
'required' => true,
'disabled' => $isElementDisabled,
'value' =>'abc'
]
);
$dateFormat = $this->_localeDate->getDateFormat(
\IntlDateFormatter::SHORT
);
$fieldset->addField(
'dob',
'date',
[
'name' => 'dob',
'label' => __('Date of birth'),
'date_format' => $dateFormat,
'disabled' => $isElementDisabled,
'class' => 'validate-date validate-date-range date-range-custom_theme-from'
]
);
$fieldset->addField(
'is_active',
'select',
[
'label' => __('Status'),
'title' => __('Status'),
'name' => 'is_active',
'required' => true,
'options' => $this->_status->getOptionArray(),
'disabled' => $isElementDisabled
]
);
if (!$model->getId()) {
$model->setData('is_active', $isElementDisabled ? '0' : '1');
}
if($model->getData('image')){
$model->setData('image','learning/images'.$model->getData('image'));
}
$form->setValues($model->getData());
$this->setForm($form);
return parent::_prepareForm();
}