Issue #2811749 by naveenvalecha, andypost, harsha012, xjm: Move ActionAddForm & ActionEditForm to src/Form
parent
fab8d1e741
commit
7c845e1307
|
@ -5,6 +5,8 @@
|
|||
* This is the Actions module for executing stored actions.
|
||||
*/
|
||||
|
||||
use Drupal\action\Form\ActionAddForm;
|
||||
use Drupal\action\Form\ActionEditForm;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
|
||||
/**
|
||||
|
@ -41,8 +43,8 @@ function action_help($route_name, RouteMatchInterface $route_match) {
|
|||
function action_entity_type_build(array &$entity_types) {
|
||||
/** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
|
||||
$entity_types['action']
|
||||
->setFormClass('add', 'Drupal\action\ActionAddForm')
|
||||
->setFormClass('edit', 'Drupal\action\ActionEditForm')
|
||||
->setFormClass('add', ActionAddForm::class)
|
||||
->setFormClass('edit', ActionEditForm::class)
|
||||
->setFormClass('delete', 'Drupal\action\Form\ActionDeleteForm')
|
||||
->setListBuilderClass('Drupal\action\ActionListBuilder')
|
||||
->setLinkTemplate('delete-form', '/admin/config/system/actions/configure/{action}/delete')
|
||||
|
|
|
@ -2,30 +2,20 @@
|
|||
|
||||
namespace Drupal\action;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\action\Form\ActionAddForm as ActionAddFormCurrent;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\ActionAddForm is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use ' . __NAMESPACE__ . '\Form\ActionAddForm. See https://www.drupal.org/node/3033540', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Provides a form for action add forms.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use
|
||||
* \Drupal\action\Form\ActionEditForm instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3033540
|
||||
*/
|
||||
class ActionAddForm extends ActionFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param string $action_id
|
||||
* The action ID.
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, $action_id = NULL) {
|
||||
$this->entity->setPlugin($action_id);
|
||||
|
||||
// Derive the label and type from the action definition.
|
||||
$definition = $this->entity->getPluginDefinition();
|
||||
$this->entity->set('label', $definition['label']);
|
||||
$this->entity->set('type', $definition['type']);
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
class ActionAddForm extends ActionAddFormCurrent {
|
||||
|
||||
}
|
||||
|
|
|
@ -2,11 +2,20 @@
|
|||
|
||||
namespace Drupal\action;
|
||||
|
||||
use Drupal\action\Form\ActionEditForm as ActionEditFormCurrent;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\ActionEditForm is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use ' . __NAMESPACE__ . '\Form\ActionEditForm. See https://www.drupal.org/node/3033540', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Provides a form for action edit forms.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use
|
||||
* \Drupal\action\Form\ActionEditForm instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3033540
|
||||
*/
|
||||
class ActionEditForm extends ActionFormBase {
|
||||
class ActionEditForm extends ActionEditFormCurrent {
|
||||
|
||||
}
|
||||
|
|
|
@ -2,152 +2,18 @@
|
|||
|
||||
namespace Drupal\action;
|
||||
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\action\Form\ActionFormBase as ActionFormBaseCurrent;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\ActionFormBase is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use ' . __NAMESPACE__ . '\Form\ActionFormBase. See https://www.drupal.org/node/3033540', E_USER_DEPRECATED);
|
||||
|
||||
/**
|
||||
* Provides a base form for action forms.
|
||||
*
|
||||
* @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use
|
||||
* \Drupal\action\Form\ActionFormBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/3033540
|
||||
*/
|
||||
abstract class ActionFormBase extends EntityForm {
|
||||
|
||||
/**
|
||||
* The action storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* The action entity.
|
||||
*
|
||||
* @var \Drupal\system\ActionConfigEntityInterface
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* Constructs a new action form.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
|
||||
* The action storage.
|
||||
*/
|
||||
public function __construct(EntityStorageInterface $storage) {
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('action')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Label'),
|
||||
'#default_value' => $this->entity->label(),
|
||||
'#maxlength' => '255',
|
||||
'#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
|
||||
];
|
||||
|
||||
$form['id'] = [
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $this->entity->id(),
|
||||
'#disabled' => !$this->entity->isNew(),
|
||||
'#maxlength' => 64,
|
||||
'#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
|
||||
'#machine_name' => [
|
||||
'exists' => [$this, 'exists'],
|
||||
],
|
||||
];
|
||||
$form['plugin'] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $this->entity->get('plugin'),
|
||||
];
|
||||
$form['type'] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $this->entity->getType(),
|
||||
];
|
||||
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$form += $plugin->buildConfigurationForm($form, $form_state);
|
||||
}
|
||||
|
||||
return parent::form($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the action already exists.
|
||||
*
|
||||
* @param string $id
|
||||
* The action ID.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the action exists, FALSE otherwise.
|
||||
*/
|
||||
public function exists($id) {
|
||||
$action = $this->storage->load($id);
|
||||
return !empty($action);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function actions(array $form, FormStateInterface $form_state) {
|
||||
$actions = parent::actions($form, $form_state);
|
||||
unset($actions['delete']);
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::validateForm($form, $form_state);
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$plugin->validateConfigurationForm($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitForm($form, $form_state);
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$plugin->submitConfigurationForm($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$this->entity->save();
|
||||
$this->messenger()->addStatus($this->t('The action has been successfully saved.'));
|
||||
|
||||
$form_state->setRedirect('entity.action.collection');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the action plugin while ensuring it implements configuration form.
|
||||
*
|
||||
* @return \Drupal\Core\Action\ActionInterface|\Drupal\Core\Plugin\PluginFormInterface|null
|
||||
* The action plugin, or NULL if it does not implement configuration forms.
|
||||
*/
|
||||
protected function getPlugin() {
|
||||
if ($this->entity->getPlugin() instanceof PluginFormInterface) {
|
||||
return $this->entity->getPlugin();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
abstract class ActionFormBase extends ActionFormBaseCurrent {
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\action\Form;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Provides a form for action add forms.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ActionAddForm extends ActionFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param string $action_id
|
||||
* The action ID.
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, $action_id = NULL) {
|
||||
$this->entity->setPlugin($action_id);
|
||||
|
||||
// Derive the label and type from the action definition.
|
||||
$definition = $this->entity->getPluginDefinition();
|
||||
$this->entity->set('label', $definition['label']);
|
||||
$this->entity->set('type', $definition['type']);
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\action\Form;
|
||||
|
||||
/**
|
||||
* Provides a form for action edit forms.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class ActionEditForm extends ActionFormBase {
|
||||
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\action\Form;
|
||||
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\PluginFormInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides a base form for action forms.
|
||||
*/
|
||||
abstract class ActionFormBase extends EntityForm {
|
||||
|
||||
/**
|
||||
* The action storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* The action entity.
|
||||
*
|
||||
* @var \Drupal\system\ActionConfigEntityInterface
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* Constructs a new action form.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
|
||||
* The action storage.
|
||||
*/
|
||||
public function __construct(EntityStorageInterface $storage) {
|
||||
$this->storage = $storage;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static(
|
||||
$container->get('entity.manager')->getStorage('action')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form['label'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Label'),
|
||||
'#default_value' => $this->entity->label(),
|
||||
'#maxlength' => '255',
|
||||
'#description' => $this->t('A unique label for this advanced action. This label will be displayed in the interface of modules that integrate with actions.'),
|
||||
];
|
||||
|
||||
$form['id'] = [
|
||||
'#type' => 'machine_name',
|
||||
'#default_value' => $this->entity->id(),
|
||||
'#disabled' => !$this->entity->isNew(),
|
||||
'#maxlength' => 64,
|
||||
'#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
|
||||
'#machine_name' => [
|
||||
'exists' => [$this, 'exists'],
|
||||
],
|
||||
];
|
||||
$form['plugin'] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $this->entity->get('plugin'),
|
||||
];
|
||||
$form['type'] = [
|
||||
'#type' => 'value',
|
||||
'#value' => $this->entity->getType(),
|
||||
];
|
||||
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$form += $plugin->buildConfigurationForm($form, $form_state);
|
||||
}
|
||||
|
||||
return parent::form($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the action already exists.
|
||||
*
|
||||
* @param string $id
|
||||
* The action ID.
|
||||
*
|
||||
* @return bool
|
||||
* TRUE if the action exists, FALSE otherwise.
|
||||
*/
|
||||
public function exists($id) {
|
||||
$action = $this->storage->load($id);
|
||||
return !empty($action);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function actions(array $form, FormStateInterface $form_state) {
|
||||
$actions = parent::actions($form, $form_state);
|
||||
unset($actions['delete']);
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::validateForm($form, $form_state);
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$plugin->validateConfigurationForm($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::submitForm($form, $form_state);
|
||||
if ($plugin = $this->getPlugin()) {
|
||||
$plugin->submitConfigurationForm($form, $form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save(array $form, FormStateInterface $form_state) {
|
||||
$this->entity->save();
|
||||
$this->messenger()->addStatus($this->t('The action has been successfully saved.'));
|
||||
|
||||
$form_state->setRedirect('entity.action.collection');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the action plugin while ensuring it implements configuration form.
|
||||
*
|
||||
* @return \Drupal\Core\Action\ActionInterface|\Drupal\Core\Plugin\PluginFormInterface|null
|
||||
* The action plugin, or NULL if it does not implement configuration forms.
|
||||
*/
|
||||
protected function getPlugin() {
|
||||
if ($this->entity->getPlugin() instanceof PluginFormInterface) {
|
||||
return $this->entity->getPlugin();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue