We use the following pattern in f.e. WorkflowDefaultWidget:

    // Step 1: use the Element.
    $element['#default_value'] = $transition;
    $element += WorkflowTransitionElement::transitionElement($element, $form_state, $form);

    // Step 2: use the Form, in order to get extra/attached fields.
    $entity_form_builder = \Drupal::getContainer()->get('entity.form_builder');
    $workflow_form = $entity_form_builder->getForm($transition, 'add');

Where the last line also calls TransitionElement.

This is not fine, since this bad performance, and the 'default' form_id is Always overridden.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johnv created an issue. See original summary.

  • johnv committed 266887f on 8.x-1.x
    Issue #2926094: Add WorkflowManager::getTransitionForm()
    
johnv’s picture

Status: Active » Postponed

I see no way to avoid calling the Element first, then WorkflowTransitionForm. The latter has too many subelements, it is impossible to determine which ones to keep or to discard.

  • johnv committed 2693151 on 8.x-1.x
    Issue #2926094 by johnv: Rename WorkflowManager::getTransitionForm to...

  • johnv committed 59d95b0 on 8.x-1.x
    Issue #2926094: correction in call to function
    
robertom’s picture

This is not fine, since this bad performance, and the 'default' form_id is Always overridden.

I do not know if this is the root cause of my problem, but I need to remove the WorkflowTransitionForm::getFormId() method to not have the problem I will describe below.

I have a view block with a list of transitions and the links to edit them...

links are in this format:
/workflow_transition/13/edit?destination=/group/7/projects/1
/workflow_transition/17/edit?destination=/group/7/projects/1
/workflow_transition/18/edit?destination=/group/7/projects/1
and open the workflow_transition form in a modal

when I try to save (from the modal) instead to save the form and go to destination, I get redirected to the page with the form edit (outside the modal) where the data have not been modified. Saving from this last page the data are correctly modified.

If I remove the WorkflowTransitionForm::getFormId() method, everything works as I expect (the form in the modal saves the data) and sends me to the destination page indicated in the url.

attached the patch that I use for remove the method, but should not be considered as a patch for resolving this issue

johnv’s picture

Version: 8.x-1.0-rc1 » 8.x-1.1
Status: Postponed » Needs work

OK,
perhaps, we can do the latter part conditionally, after executing

$form_id = parent::getFormId();
johnv’s picture

Category: Task » Bug report

Changed to Bug , as per #6

johnv’s picture

Version: 8.x-1.1 » 8.x-1.7
Status: Needs work » Active

in #2899025-57: Attached field type 'file' not working on WorkflowTransition, I removed the unique ID.
Please test dev version.

I guess this solves your problem, If not please reopen.

johnv’s picture

johnv’s picture

Title: Avoid calling the WorkflowTransitionElement twice on a form » WorkflowTransitionElement is called twice on a form

  • johnv committed 4213b0ce on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement is called twice on a form
    

  • johnv committed 20f51585 on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement is called twice on a form
    

  • johnv committed 902dd685 on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement is called twice on a form
    

  • johnv committed df731853 on 8.x-1.x
    Issue #2926094: reorder WorkflowTransitionElement
    

  • johnv committed 1f77307b on 8.x-1.x
    Issue #2926094: reorder WorkflowTransitionElement
    

  • johnv committed a093a8e1 on 8.x-1.x
    Issue #2926094: reorder WorkflowTransitionElement
    

  • johnv committed a52f7512 on 8.x-1.x
    Issue #2926094: factor out  WorkflowTransitionTimestamp
    

  • johnv committed ad73361b on 8.x-1.x
    Issue #2926094: Move to_sid and comment fields to widget
    

  • johnv committed 6c294a09 on 8.x-1.x
    Issue #2926094: Remove redundant _validate_buttons()
    

  • johnv committed 1ebc07e6 on 8.x-1.x
    Issue #2926094: Remove outdated entity cloning in WorkflowTransitionForm
    

  • johnv committed f982825d on 8.x-1.x
    Issue #2926094: Remove redundant code & Factor out updateEntity()
    

  • johnv committed b5217fab on 8.x-1.x
    Issue #2926094 by robertom, johnv: WorkflowTransitionElement is called...

  • johnv committed c69a218c on 8.x-1.x
    Issue #2926094 by robertom, johnv: WorkflowTransitionElement - move code...

  • johnv committed 1d736019 on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement - unify updateEntity() and...

  • johnv committed dd07ab73 on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement is called twice  -...
johnv’s picture

Status: Active » Fixed

We now have 3 possible options:
Option 1: call transitionElement() directly.

$element['#default_value'] = $transition;
 $element = WorkflowTransitionElement::transitionElement($element, $form_state, $form);
 

Option 2: call WorkflowTransitionForm() directly.

// @todo Use Form/Widget, instead of transitionElement().
$element = WorkflowTransitionForm::createInstance($entity, $field_name, [], $transition);
$element = WorkflowTransitionForm::trimWorkflowTransitionForm($element, $transition);
$element['#parents'] = [];
// Remove langcode to avoid error upon submit.
// InvalidArgumentException: The configuration property langcode.0 doesn't exist.
unset($element['langcode']);

Option 3: call WorkflowDefaultWidget via NodeFormDisplay.

    $element = WorkflowDefaultWidget::createInstance($transition);
// Fetch the element from the widget.
$element = $element[$field_name]['widget'][0];

  • johnv committed fc09a0cd on 8.x-1.x
    Issue #2926094: WorkflowTransitionElement is called twice on a form -...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.