Provide core Action plugins to set state_machine field values

Created on 15 March 2024, 8 months ago
Updated 21 March 2024, 8 months ago

Problem/Motivation

It would be very helpful if state_machine provided a core Action plugin to alter the values of state fields.

For example, I have a site with a state field on User entities, and I'd love to use the bulk operations at /admin/people to alter the state of different users. There are tons of other possible uses.

I asked about this in Slack, @rszrama confirmed it was needed, I proposed a solution, he approved and asked me to write it up.

Steps to reproduce

Proposed resolution

Define an Action plugin, probably extending Drupal\Core\Action\ConfigurableActionBase since we're going to want to select the target state.

Maybe the "contract" of this feature is:

  • You select as many entities as you want
  • You pick the workflow you want to bulk act on
  • You pick the value you want to set
  • The robot finds all entities that:
    • Have a state field using your workflow
    • Are in a state that allows transition to the state you selected

Anything you selected that didn't match gets printed in the results:

  • Updated X entities to state N
  • Y entities from your selection do not use $workflow
  • Z entities are not in a state that allows transition to N

It'd be nice to avoid the UI overwhelm of defining separate actions for each workflow. But if it's too much complication to let you pick a workflow, then get a set of choices for states, we could provide an action for each workflow (like core's actions for each role), each one providing its own, known, set of state options to pick from.

Worst case is we have a separate action for each workflow and state, so there's nothing to pick, except from an exploding list of choices in the action drop-down. 😬

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States dww

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @dww
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    I've committed the simplest solution - when we create action for specific entity type, workflow and action.



    I've not found default solution to add custom additional fields on the action selection form.

    Drupal\views\Plugin\views\field::BulkForm builds only select list widget for the options.

    To add custom conditional fields, I think we have to alter the bulk form and add the fields dynamically when our plugin is selected. Still researching if we can implement this in other way.

  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    I've added batch processing of the selected entities as there can be event subscribers to the state transitions events, so maybe it can take some time.
    I've added also the messages, but have changed some strings.
    Also I've added message for the entities with the empty state field. Maybe we can unite it with the count of the not allowed transition message?

    Regarding more complex action form when we can select workflow and state transition, maybe it can be only possible by creating custom views form similar to BulkForm and only for the state transition action. And maybe then only one of them can be used at the same time.

    Ideas?

  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    I've created bulk form by extending BulkForm from views and new non-configurable action plugin so it now possible to select workflow and transition on the views bulk action form.

    Testing steps:
    1. Enable Actions UI module.
    2. Goto /admin/config/system/actions and add new action(s).
    3. In the select list you should see ''Apply state transition to order', ''Apply selected state transition to order' if you have enabled commerce_order can see other options with name of an entity which has state fields.
    ''Apply state transition to ...' is not configurable action and will work only with new views bulk form plugin ''Bulk update (state_machine)''
    'Apply selected state transition to ...' is configurable action, so you can pre-select workflow and transition to apply. This action can work with default 'Bulk update' views form.
    4. Create or edit view and add 'Bulk update (state_machine)''. Save the view.
    5. Open page with the view, select entities and select action. For the ''Apply state transition to ..." the 'Workflow' and 'Transition' select lists should appear. Try to apply the action.

    Example of the action form on views:

    β†’

    I will review and improve current fixes. I think we need to try alter default views bulk form and exclude actions created from the new non-configurable plugin from form's setting form.

  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    Committed improvements in code and updated README.md

  • Merge request !23Resolve #3427978 "Provide core action" β†’ (Open) created by khiminrm
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 8 months ago
    PHPLint Failed
  • Status changed to Needs review 8 months ago
  • Pipeline finished with Success
    8 months ago
    Total: 150s
    #125158
  • πŸ‡ΊπŸ‡¦Ukraine khiminrm

    I've created MR.
    For testing on local there are two options:

    • Apply patch to the module by adding the patch from the MR's diff in composer.json.
    • Clone the module from the issue fork. Fetch the fork's branch and checkout to it on local. See commands above in the head of the issue.
Production build 0.71.5 2024