View source
<?php
namespace Drupal\backup_migrate\Form;
use Drupal\backup_migrate\Drupal\Config\DrupalConfigHelper;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
class WrapperEntityForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $this->entity
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $this->entity
->id(),
'#machine_name' => [],
'#disabled' => !$this->entity
->isNew(),
];
if (!$this->entity
->get('type')) {
$form['type'] = [
'#type' => 'radios',
'#title' => $this
->t('Type'),
];
foreach ($this->entity
->getPluginManager()
->getDefinitions() as $type) {
if (empty($type['locked'])) {
$form['type']['#options'][$type['id']] = $type['title'];
$form['type'][$type['id']]['#description'] = $type['description'];
}
}
}
else {
$type = $this->entity
->getPlugin()
->getPluginDefinition();
$form['type'] = [
'#type' => 'value',
'#value' => $type['id'],
'#markup' => $this
->t("Type: @type", [
'@type' => $type['title'],
]),
];
if ($bam_plugin = $this->entity
->getObject()) {
$form['config'] = DrupalConfigHelper::buildPluginForm($bam_plugin, 'initialize', [
'config',
]);
}
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
if ($this->entity
->isNew()) {
$actions['submit']['#value'] = $this
->t('Save and edit');
}
return $actions;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = $entity
->save();
switch ($status) {
case SAVED_NEW:
\Drupal::messenger()
->addMessage($this
->t('Created %label.', [
'%label' => $entity
->label(),
]));
$form_state
->setRedirectUrl($entity
->toUrl('edit-form'));
break;
default:
\Drupal::messenger()
->addMessage($this
->t('Saved %label.', [
'%label' => $entity
->label(),
]));
$form_state
->setRedirectUrl($entity
->toUrl('collection'));
break;
}
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
foreach ($values as $key => $value) {
$entity
->set($key, $value);
}
}
}