Commit af88a197 authored by mxh's avatar mxh
Browse files

Issue #3266721 by mxh: Embedded entity forms lose submitted form values when...

Issue #3266721 by mxh: Embedded entity forms lose submitted form values when other form sections are being edited
parent 17e43edf
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -64,6 +64,9 @@ On every task mode, you can create a task that always operates on a certain
subject. A subject is always a concrete data type, like an article, blog post
or a "Tags" taxonomy term.

when adding a new task, it is not enabled immediately. To enable the task,
select the newly created task in the Flow configuration and choose "Enable".

## 4.1 The default form display mode

Some configurations of tasks and subjects, for example for merging content
+10 −6
Original line number Diff line number Diff line
@@ -115,10 +115,14 @@ flow.task.settings.relate:
    method:
      type: string
      label: 'Reference method'
    reverse_field_name:
    reverse:
      type: mapping
      label: 'Reverse reference settings'
      mapping:
        field_name:
          type: string
          label: 'Reverse reference field machine name'
    reverse_method:
        method:
          type: string
          label: 'Reverse reference method'
    target:
+2 −1
Original line number Diff line number Diff line
@@ -385,7 +385,8 @@ class Load extends FlowSubjectBase implements PluginFormInterface {
    while ($element = &NestedArray::getValue($form, $button_parents)) {
      foreach (Element::children($element) as $child) {
        if (isset($element[$child]['#value'])) {
          $form_state->setValueForElement($element[$child], $element[$child]['#value']);
          $value = $element[$child]['#value'] === '_none' ? NULL : $element[$child]['#value'];
          $form_state->setValueForElement($element[$child], $value);
        }
      }
      if (isset($element['#subject']) && $element['#subject'] === $this) {
+24 −50
Original line number Diff line number Diff line
@@ -211,25 +211,24 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
      $weight += 10;
      $field_name_options = ['_none' => $this->t('- None -')] + $field_name_options;
      $reverse_wrapper_id = Html::getUniqueId('flow-reverse-reference');
      $form['reverse_prefix'] = [
        '#type' => 'markup',
        '#markup' => '<div id="' . $reverse_wrapper_id . '">',
      $form['reverse'] = [
        '#type' => 'container',
        '#attributes' => ['id' => $reverse_wrapper_id],
        '#weight' => $weight++,
      ];
      $weight += 10;
      $form['reverse_field_name'] = [
      $form['reverse']['field_name'] = [
        '#type' => 'select',
        '#title' => $this->t('Reverse reference field'),
        '#description' => $this->t('Optionally you may define the reverse relationship, by selecting the field of the target that will hold the reference back to the @source.', [
          '@source' => $source_label,
        ]),
        '#options' => $field_name_options,
        '#default_value' => $this->settings['reverse_field_name'] ?? '_none',
        '#default_value' => $this->settings['reverse']['field_name'] ?? '_none',
        '#required' => FALSE,
        '#empty_value' => '_none',
        '#weight' => $weight++,
        '#weight' => 10,
        '#ajax' => [
          'callback' => [$this, 'setReverseAjax'],
          'callback' => [$this, 'selectAjax'],
          'wrapper' => $reverse_wrapper_id,
        ],
        '#submit' => [[$this, 'selectSubmitAjax']],
@@ -237,24 +236,18 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
        '#limit_validation_errors' => [],
      ];
      $weight += 10;
      if (!empty($this->settings['reverse_field_name'])) {
        $form['reverse_method'] = [
      if (!empty($this->settings['reverse']['field_name'])) {
        $form['reverse']['method'] = [
          '#type' => 'select',
          '#title' => $this->t('Reverse reference method'),
          '#description' => $this->t('Select how the reverse relationship will be defined, using the above selected reverse reference field.'),
          '#options' => $reference_method_options,
          '#default_value' => $this->settings['reverse_method'] ?? '_none',
          '#default_value' => $this->settings['reverse']['method'] ?? '_none',
          '#required' => TRUE,
          '#empty_value' => '_none',
          '#weight' => $weight++,
          '#weight' => 20,
        ];
      }
      $weight += 10;
      $form['reverse_suffix'] = [
        '#type' => 'markup',
        '#markup' => '</div>',
        '#weight' => $weight++,
      ];
    }

    $weight += 10;
@@ -297,7 +290,7 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
      '#empty_value' => '_none',
      '#weight' => 10,
      '#ajax' => [
        'callback' => [$this, 'setTargetAjax'],
        'callback' => [$this, 'selectAjax'],
        'wrapper' => $target_wrapper_id,
      ],
      '#submit' => [[$this, 'selectSubmitAjax']],
@@ -331,28 +324,6 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
    return $form;
  }

  /**
   * Ajax callback for setting up the reverse reference.
   *
   * @param array &$form
   *   The current form build array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The according form state.
   *
   * @return array
   *   The part of the form that got refreshed via Ajax.
   */
  public function setReverseAjax(array &$form, FormStateInterface $form_state): array {
    $button = &$form_state->getTriggeringElement();
    $element = &NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
    return array_intersect_key($element, [
      'reverse_prefix' => TRUE,
      'reverse_field_name' => TRUE,
      'reverse_method' => TRUE,
      'reverse_suffix' => TRUE,
    ]);
  }

  /**
   * Ajax submit callback for select widgets.
   *
@@ -367,7 +338,8 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
    while ($element = &NestedArray::getValue($form, $button_parents)) {
      foreach (Element::children($element) as $child) {
        if (isset($element[$child]['#value'])) {
          $form_state->setValueForElement($element[$child], $element[$child]['#value']);
          $value = $element[$child]['#value'] === '_none' ? NULL : $element[$child]['#value'];
          $form_state->setValueForElement($element[$child], $value);
        }
      }
      if (isset($element['#task']) && $element['#task'] === $this) {
@@ -376,7 +348,10 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
      array_pop($button_parents);
    }
    $subform_state = SubformState::createForSubform($element, $form, $form_state);
    $this->submitConfigurationForm($element, $subform_state);
    $this->settings['field_name'] = $subform_state->getValue(['field_name']);
    $this->settings['method'] = $subform_state->getValue(['method']);
    $this->settings['reverse'] = $subform_state->getValue(['reverse']);
    $this->settings['target']['id'] = $subform_state->getValue(['target', 'id']);
    $form_state->setRebuild();
  }

@@ -391,7 +366,7 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
   * @return array
   *   The part of the form that got refreshed via Ajax.
   */
  public function setTargetAjax(array &$form, FormStateInterface $form_state): array {
  public function selectAjax(array &$form, FormStateInterface $form_state): array {
    $button = &$form_state->getTriggeringElement();
    $element = &NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
    return $element;
@@ -414,8 +389,7 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->settings['field_name'] = $form_state->getValue(['field_name']);
    $this->settings['method'] = $form_state->getValue(['method']);
    $this->settings['reverse_field_name'] = $form_state->getValue(['reverse_field_name']);
    $this->settings['reverse_method'] = $form_state->getValue(['reverse_method']);
    $this->settings['reverse'] = $form_state->getValue(['reverse']);
    $this->settings['target']['id'] = $form_state->getValue(['target', 'id']);

    $this->target = NULL;
@@ -450,8 +424,8 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
      return;
    }
    $method_settings = explode(':', $this->settings['method']);
    $reverse_field_name = $this->settings['reverse_field_name'] ?? NULL;
    $reverse_method_settings = !empty($this->settings['reverse_method']) ? explode(':', $this->settings['reverse_method']) : NULL;
    $reverse_field_name = $this->settings['reverse']['field_name'] ?? NULL;
    $reverse_method_settings = !empty($this->settings['reverse']['method']) ? explode(':', $this->settings['reverse']['method']) : NULL;

    if (empty($this->flags)) {
      $this->flags = [
@@ -650,8 +624,8 @@ class Relate extends FlowTaskBase implements PluginFormInterface {
    if (isset($settings['field_name'])) {
      $target_configuration['settings']['target_for']['field'] = $settings['field_name'];
    }
    if (isset($settings['reverse_field_name'])) {
      $target_configuration['settings']['target_for']['reverse_field'] = $settings['reverse_field_name'];
    if (isset($settings['reverse']['field_name'])) {
      $target_configuration['settings']['target_for']['reverse_field'] = $settings['reverse']['field_name'];
    }
    $this->setTargetSubject($this->subjectManager->createInstance($settings['target']['id'], $target_configuration));
  }