|
| 1 | +--- |
| 2 | +title: How to Extend the Workflow Transition Modal |
| 3 | +--- |
| 4 | + |
| 5 | +# How to Extend the Workflow Transition Modal |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +This example adds a custom multi-select reviewers field to the |
| 10 | +workflow transition modal and persists the selected reviewers as |
| 11 | +part of the workflow note via a Symfony Workflow event subscriber. |
| 12 | + |
| 13 | +Before reaching for a UI extension, check whether your use case is |
| 14 | +already covered by the |
| 15 | +[`additionalFields` option in the workflow configuration](https://docs.pimcore.com/platform/Pimcore/Workflow_Management/Workflow_Tutorial/). |
| 16 | +Static select boxes, single-user pickers, checkboxes, date pickers |
| 17 | +and similar inputs can be declared directly in the workflow YAML |
| 18 | +config — no plugin code required. Use the slot mechanism described |
| 19 | +below when you need behavior the YAML config cannot express: |
| 20 | +filtered option lists, options that depend on the current element, |
| 21 | +asynchronously fetched data, or multi-select widgets. |
| 22 | + |
| 23 | +## Extension points |
| 24 | + |
| 25 | +The workflow transition modal exposes three slot positions and one |
| 26 | +overridable modal component: |
| 27 | + |
| 28 | +| Name | Type | Purpose | |
| 29 | +|---|---|---| |
| 30 | +| `element.editor.workflow.modal.slots.top` | Slot | Content above the form (banners, summaries). Rendered outside the form. | |
| 31 | +| `element.editor.workflow.modal.slots.center` | Slot | Form fields rendered between the YAML-declared additional fields and the notes textarea. Use this for custom `<Form.Item>` inputs. | |
| 32 | +| `element.editor.workflow.modal.slots.bottom` | Slot | Content below the form (footnotes, links). Rendered outside the form. | |
| 33 | +| `element.editor.workflow.modal` | Single | The complete modal component. Override only if you need full control over modal chrome and submission. | |
| 34 | + |
| 35 | +Slot components rendered into `…slots.center` sit inside the same |
| 36 | +antd `<Form>` as the rest of the modal. Any `<Form.Item name="…">` |
| 37 | +inside the slot component automatically participates in the form |
| 38 | +context, and its value flows into the workflow submission payload |
| 39 | +under `additional.<name>` without any additional wiring. |
| 40 | + |
| 41 | +## Files Overview |
| 42 | + |
| 43 | +| Layer | File | Purpose | |
| 44 | +|---|---|---| |
| 45 | +| Backend | [workflow.yaml] | Example workflow definition | |
| 46 | +| Backend | [WorkflowReviewersSubscriber.php] | Reads `additional.reviewers` from the transition context and appends it to the workflow note | |
| 47 | +| Backend | [services.yaml] | Service registration | |
| 48 | +| UI | [reviewers-field.tsx] | React `<Form.Item>` rendered into the modal's center slot | |
| 49 | +| UI | [workflow-modal-extension-module.tsx] | Registers the slot component | |
| 50 | +| UI | [index.ts] | Plugin entry point | |
| 51 | +| UI | [plugins.ts] | Plugin export | |
| 52 | + |
| 53 | +[workflow.yaml]: https://github.com/pimcore/studio-example-bundle/blob/main/config/pimcore/workflow.yaml |
| 54 | +[WorkflowReviewersSubscriber.php]: https://github.com/pimcore/studio-example-bundle/blob/main/src/EventSubscriber/WorkflowReviewersSubscriber.php |
| 55 | +[services.yaml]: https://github.com/pimcore/studio-example-bundle/blob/main/config/services.yaml |
| 56 | +[reviewers-field.tsx]: https://github.com/pimcore/studio-example-bundle/blob/main/assets/js/src/examples/workflow-modal-extension/components/reviewers-field.tsx |
| 57 | +[workflow-modal-extension-module.tsx]: https://github.com/pimcore/studio-example-bundle/blob/main/assets/js/src/examples/workflow-modal-extension/modules/workflow-modal-extension-module.tsx |
| 58 | +[index.ts]: https://github.com/pimcore/studio-example-bundle/blob/main/assets/js/src/examples/workflow-modal-extension/index.ts |
| 59 | +[plugins.ts]: https://github.com/pimcore/studio-example-bundle/blob/main/assets/js/src/plugins.ts |
| 60 | + |
| 61 | +## Details |
| 62 | + |
| 63 | +### Registering a slot component |
| 64 | + |
| 65 | +A plugin registers a React component into one of the workflow modal |
| 66 | +slots via the `ComponentRegistry`: |
| 67 | + |
| 68 | +```typescript |
| 69 | +componentRegistry.registerToSlot( |
| 70 | + componentConfig.element.editor.workflow.modal.slots.center.name, |
| 71 | + { |
| 72 | + name: 'reviewers', |
| 73 | + priority: 100, |
| 74 | + component: ReviewersField |
| 75 | + } |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Filtering by workflow and element context |
| 80 | + |
| 81 | +A slot component renders for every workflow modal open by default. |
| 82 | +Use the existing context hooks to filter for the workflow and |
| 83 | +element you care about, and return `null` otherwise: |
| 84 | + |
| 85 | +```tsx |
| 86 | +import { useWorkflow, useElementContext } from '@pimcore/studio-ui-bundle/modules/element' |
| 87 | + |
| 88 | +export const ReviewersField = (): React.JSX.Element | null => { |
| 89 | + const { triggeredWorkflowAction } = useWorkflow() |
| 90 | + const { elementType } = useElementContext() |
| 91 | + |
| 92 | + if (triggeredWorkflowAction?.workflowId !== 'simple_approval') return null |
| 93 | + if (triggeredWorkflowAction?.transitionId !== 'request_review') return null |
| 94 | + if (elementType !== 'data-object') return null |
| 95 | + |
| 96 | + return ( |
| 97 | + <Form.Item |
| 98 | + label="Reviewers" |
| 99 | + name="reviewers" |
| 100 | + rules={ [{ required: true, message: 'Please select at least one reviewer.' }] } |
| 101 | + > |
| 102 | + <Select |
| 103 | + mode="multiple" |
| 104 | + options={ reviewerOptions } |
| 105 | + /> |
| 106 | + </Form.Item> |
| 107 | + ) |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Filter *before* fetching data so plugins do not burn API requests |
| 112 | +on transitions they do not handle. |
| 113 | + |
| 114 | +### How submitted values reach the backend |
| 115 | + |
| 116 | +The modal collects every form value and posts them as part of the |
| 117 | +workflow action submission: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "workflowOptions": { |
| 122 | + "notes": "Please review the latest changes.", |
| 123 | + "additional": { |
| 124 | + "reviewers": [1, 7, 12] |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +The `workflowOptions` array is passed verbatim to |
| 131 | +`Pimcore\Workflow\Manager::applyWithAdditionalData()`, which in turn |
| 132 | +calls `Workflow::apply($subject, $transition, $additionalData)`. |
| 133 | +That third argument becomes the Symfony Workflow transition context |
| 134 | +and is available on every workflow event: |
| 135 | + |
| 136 | +```php |
| 137 | +public function onTransition(TransitionEvent $event): void |
| 138 | +{ |
| 139 | + $context = $event->getContext(); |
| 140 | + $reviewers = $context['additional']['reviewers'] ?? null; |
| 141 | + // …persist, notify, log, etc. |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +Note the nesting: slot-injected values live at |
| 146 | +`$context['additional']['<name>']`, not `$context['<name>']`. |
| 147 | + |
| 148 | +### Full modal override |
| 149 | + |
| 150 | +When the slot mechanism is not enough — for example, if you need to |
| 151 | +replace the modal chrome, change the submission flow, or render a |
| 152 | +wizard — override the modal component itself: |
| 153 | + |
| 154 | +```typescript |
| 155 | +componentRegistry.override({ |
| 156 | + name: componentConfig.element.editor.workflow.modal.component.name, |
| 157 | + component: CustomWorkflowModal |
| 158 | +}) |
| 159 | +``` |
| 160 | + |
| 161 | +The custom modal can reuse the existing |
| 162 | +`useWorkflow`, `useSubmitWorkflow`, `useWorkflowFieldRenderer` and |
| 163 | +`useDateConverter` hooks so the standard fields and submission |
| 164 | +logic do not need to be re-implemented. |
0 commit comments