1. Регистрируем роут:
# modulename.routing.yml
entity.node.my_action:
path: '/node/{node}/my-action'
defaults:
_form: '\Drupal\modulename\Form\MyActionForm'
_title: 'My action'
requirements:
_permission: 'administer site configuration'
options:
_admin_route: TRUE
2. Создаём контроллер или форму:
<?php
// src/Form/MyAction.php
namespace Drupal\modulename\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
class MyActionForm extends FormBase {
/**
* {@inheritDoc}
*/
public function getFormId(): string {
return 'my_action_form';
}
/**
* {@inheritDoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, NodeInterface $node = NULL): array {
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Confirm'),
];
return $form;
}
/**
* {@inheritDoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$node = $form_state->getBuildInfo()['args'][0]; /** @var NodeInterface $node */
...
}
}
3. Добавляем действие в ссылки:
# modulename.links.contextual.yml
entity.node.my_action:
title: 'My action'
route_name: 'entity.node.my_action'
group: 'node'
weight: 100
Написанное актуально для
Drupal 8+
Добавить комментарий