0

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

3
  • Does this answer your question? custom action plugin with ConfigurationForm Nov 19, 2023 at 1:57
  • In this case, the form would not be the form to configure the action, but the node create form, which would be the output of the action.
    – apaderno
    Nov 19, 2023 at 9:47
  • All the classes implemented by a module are in a src directory. Some classes are required to be placed in a specific sub-directory of the src directory.
    – apaderno
    Nov 19, 2023 at 11:36

1 Answer 1

1

In above code this "system.action.test_action.yml" file missing : add this file inside this folder: module/config/optional, only then custom action 'll start appear under action select list

langcode: en
status: true
dependencies:
 module:
   - user
id: test_action
label: 'Test action'
type: user
plugin: test_action
configuration: {  }

After this newly added "Test action" start appear in select list of Action field on people listing page(/admin/people)

Note: Make sure uninstall then install module so that "system.action.test_action.yml" config installed

After adding this file "system.action.test_action.yml" at proper folder structure, only then action 'll start appear.

ActionTest.php need to place here: my_module/src/Plugin/Action/ActionTest.php instead of here my_module/Plugin/Action/ActionTest.php

Here in this file name convention : system.action.test_action.yml , test_action denotes machine name of custom action plugin

If you want to configure action from UI along with custom action with the ConfigurationForm

  1. install "Actions" module in the core

  2. Then visit to the actions overview page: /admin/config/system/actions

  3. Here you 'll see "Create an advanced action" field, select the "Test action"(newly added custom action) from drop down & then click on create button

  4. Then form 'll appear , fill the value in the field & click on save

  5. Now the "Newly added custom action" 'll appear in available action list

Note : In this approach you don't need to create "system.action.test_action.yml" file

I have noticed this buildConfigurationForm() not defined, so need to remove this line:
$form = parent::buildConfigurationForm($form, $form_state);

2
  • Hi! Thanks for your answer. But I did the same as you said, I deleted the code, I also added the folders to the src, I added the file to the module/config/optional folder but when I click on it, my custom form does not appear. Nov 20, 2023 at 13:49
  • I suggested two approaches but if you want to see your custom form then you need to follow steps mentioned above in points form (1-5) points, you can revert the changes that you done as it is part of another approach
    – mini
    Nov 20, 2023 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.