I am trying to make a custom user action where the selected users will have a node created manually, however I need to create a form to save the data that this node will have, such as: title, start date, end date.
I have this code in my my_module/Plugin/Action/ActionTest.php
<?php
namespace Drupal\my_module\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Create custom action
*
* @Action(
* id = "test_action",
* label = @Translation("Test action"),
* type = "user",
* )
*/
class ActionTest extends ViewsBulkOperationsActionBase implements PluginFormInterface {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
$nid = $entity->id();
$uid = \Drupal::currentUser()->id();
$payment = \Drupal::entityTypeManager()->getStorage('node')->create([
'type' => 'payments',
'title' => "Payment ${nid}",
'langcode' => 'es',
'field_payment_state' => 1,
'field_user' => $nid,
'field_last_plan_purchased' => 2156
]);
$payment->save();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['title'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#required' => TRUE,
);
return $form;
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return true;
}
}
But I can't get the selected action to go to the form, any idea on how I can achieve this?
I have the .schema.yml file like this
action.configuration.my_module:
type: test_action
label: 'Action custom'
and the configuration .yml file
langcode: es
status: true
dependencies:
module:
- user
id: test_action
label: 'Action custom'
type: user
plugin: test_action
configuration: { }
I'm using drupal 10.1.4 and php 8.1 I appreciate any answer you can give me