commands-1.x-dev/src/Form/CommandTypeForm.php
src/Form/CommandTypeForm.php
<?php
namespace Drupal\commands\Form;
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commands\Entity\CommandType;
/**
* Form handler for command type forms.
*/
class CommandTypeForm extends BundleEntityFormBase {
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/**
* @var CommandType
*/
$entity_type = $this->entity;
// dsm($entity_type);
if ($this->operation == 'edit') {
$form['#title'] = $this->t('Edit %label command type', ['%label' => $entity_type->label()]);
}
if ($entity_type->isNew()) {
$plugin = \Drupal::routeMatch()->getParameter('plugin');
if (empty($plugin)) {
$this->redirect('entity.command_type.collection')->send();
return $form;
}
}
else {
$plugin = $entity_type->id();
}
$definition = \Drupal::service('plugin.manager.commands')->getDefinition($plugin);
$form['command_plugin'] = [
'#type' => 'value',
'#value' => $plugin,
];
$form['title'] = [
'#type' => 'item',
'#title' => $this->t('Command'),
'#markup' => $definition['label'],
'#description' => $definition['description'],
];
$form['label'] = [
'#title' => $this->t('Label'),
'#type' => 'textfield',
'#default_value' => $entity_type->label() ?: $definition['label'],
'#description' => $this->t('The text to display for this command.'),
'#required' => TRUE,
'#size' => 30,
];
$form['description'] = [
'#title' => $this->t('Description'),
'#type' => 'textarea',
'#default_value' => $entity_type->description() ?: $definition['description'],
'#description' => $this->t('Text to display to the user when choosing what command to run.'),
'#size' => 30,
];
$form['id'] = [
'#type' => 'value',
'#default_value' => $entity_type->id(),
];
return $this->protectBundleIdElement($form);
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this->t('Save command type');
$actions['delete']['#value'] = $this->t('Delete command type');
return $actions;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var CommandType $entity_type */
$entity_type = $this->entity;
$entity_type->set('id', $entity_type->commandPlugin()->getPluginId());
$entity_type->set('command_plugin', $entity_type->commandPlugin()->getPluginId());
if (empty($entity_type->get('label'))) {
$entity_type->set('label', $entity_type->commandPlugin()->label());
}
$status = $entity_type->save();
$t_args = ['%name' => $entity_type->label()];
if ($status == SAVED_UPDATED) {
$message = $this->t('The command type %name has been updated.', $t_args);
}
elseif ($status == SAVED_NEW) {
$message = $this->t('The command type %name has been added.', $t_args);
}
$this->messenger()->addStatus($message);
$form_state->setRedirectUrl($entity_type->toUrl('collection'));
}
}
