0

I have made a custom action which have a configuration form:

class MyAction extends ConfigurableActionBase

Thus, I have the opportunity to create a form that allows the user to set some options. This form is created inside

public function buildConfigurationForm(array $form, FormStateInterface $form_state)

I need to get the list of the selected items that will be processed at this stage in order for me to exclude some option in the configuration form. In my use case, I need to exclude some option depending of the bundle of the selected entities (by my VBO view)

2
+50

Provided you use the Views Bulk Operations contrib module, you can use code similar to the following, inside the buildConfigurationForm() method. Most importantly, you need to extend ViewsBulkOperationsActionBase instead of ConfigurableActionBase.

class YourAction extends ViewsBulkOperationsActionBase

...
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    ...
    $selectedItems = array_map(function ($listItem) {
      return reset($listItem);
    }, $this->context['list']);
    ...
  }
...

This will give you an array whose values are the entity ids of the selected items in your view. The keys of the array are the bulkFormKey for each selected entity.

See ViewsBulkOperationsFormTrait::calculateEntityBulkFormKey() and ViewsBulkOperationsFormTrait::getListItem() for more information about bulkFormKeys.

Hope this helps, good luck!

P.S.: And for anyone else landing here, the related VBO's documentation page is over at https://www.drupal.org/docs/8/modules/views-bulk-operations-vbo/configuring-an-action

  • This looks like it is exactly what I was looking for!!! I will try this code this evening and will give you the bounty if it is working. – Baud Nov 19 '18 at 9:01
  • Good to know, cheers! – Stefanos Petrakis Nov 20 '18 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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