public static function ParagraphsWidget::paragraphsConvertSubmit

The submit callback to complete the paragraphs conversion.

File

paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php, line 1049

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

public static function paragraphsConvertSubmit(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $conversion_manager = \Drupal::service('plugin.manager.paragraphs.conversion');

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -3));
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];

  // Replacing element in the array.
  $widget_state = WidgetBase::getWidgetState($parents, $field_name, $form_state);
  $delta = $button['#delta'];

  /** @var \Drupal\paragraphs\ParagraphInterface $original_paragraph */
  $original_paragraph = $widget_state['paragraphs'][$delta]['entity']
    ->getUntranslated();

  // Clean the user input of the original paragraph to avoid issues with
  // default values. Keep the original weight only as it is used for ordering.
  $user_input = NestedArray::getValue($form_state
    ->getUserInput(), array_slice($button['#parents'], 0, -3));
  $conversion_plugin_id = $user_input[$delta]['conversion_plugins']['action'];
  $settings = [];
  if (isset($user_input[$delta]['conversion_plugins']['form'][$conversion_plugin_id])) {
    $settings = $user_input[$delta]['conversion_plugins']['form'][$conversion_plugin_id];
  }
  $user_input[$delta] = [
    '_weight' => $user_input[$delta]['_weight'] ?? 0,
  ];
  NestedArray::setValue($form_state
    ->getUserInput(), array_slice($button['#parents'], 0, -3), $user_input);

  // Create an instance of the translation plugin to use.
  $plugin = $conversion_plugin_definitions = \Drupal::service('plugin.manager.paragraphs.conversion')
    ->createInstance($conversion_plugin_id);
  $new_paragraphs_count = 0;
  $converted_paragraphs = [];

  // Make sure that the default language is the first one in the list.
  $languages = array_merge([
    $original_paragraph
      ->language()
      ->getId(),
  ], array_keys($original_paragraph
    ->getTranslationLanguages(FALSE)));

  // Loop over all languages of the original paragraph.
  foreach ($languages as $langcode) {

    // Get the correct translation and convert with those values.
    $translation_paragraph = $original_paragraph
      ->getTranslation($langcode);
    $paragraphs_values = $plugin
      ->convert($settings, $translation_paragraph, $converted_paragraphs);

    // If there are no converted paragraphs, skip this translation.
    if (!$paragraphs_values) {
      continue;
    }

    // Set the count for new paragraphs based on the default translation.
    if ($original_paragraph
      ->isDefaultTranslation()) {
      $new_paragraphs_count = count($paragraphs_values) - 1;
    }

    // Loop over all returned paragraphs.
    foreach ($paragraphs_values as $key => $paragraph_values) {

      // Create a Paragraph from the default translation.
      if ($translation_paragraph
        ->isDefaultTranslation()) {
        if (is_array($paragraph_values)) {

          /** @var \Drupal\Paragraphs\ParagraphInterface $converted_paragraph */
          $converted_paragraph = Paragraph::create($paragraph_values);
          $converted_paragraphs[$key] = $converted_paragraph;

          // Apply default paragraph values.
          $conversion_manager
            ->applyDefaultValues($original_paragraph, $converted_paragraph);
        }
        else {
          $converted_paragraphs[$key] = $paragraph_values;
        }
      }
      else {
        $converted_paragraph = $converted_paragraphs[$key];
        if (!$converted_paragraph
          ->hasTranslation($langcode)) {

          // Add the translation to the default translation paragraph.
          $conversion_manager
            ->addTranslation($converted_paragraph, $langcode, $paragraph_values);
        }
      }
    }
  }
  $first = TRUE;
  $display = $widget_state['paragraphs'][$delta]['display'];
  $widget_state['items_count'] += $new_paragraphs_count;
  $widget_state['real_item_count'] += $new_paragraphs_count;

  // Loop over all converted paragraphs and place them where they should.
  foreach ($converted_paragraphs as $converted_paragraph) {

    // Allow modules to alter the converted paragraphs.
    \Drupal::moduleHandler()
      ->alter('paragraphs_conversion', $original_paragraph, $converted_paragraph);

    // Replace original paragraph item with a converted one.
    $paragraph_item = [
      'entity' => $converted_paragraph,
      'display' => $display,
      'mode' => 'edit',
    ];
    if ($first) {

      // The first returned paragraph should replace the one where the
      // conversion is triggered.
      $widget_state['paragraphs'][$delta] = $paragraph_item;
      $first = FALSE;
    }
    else {

      // All other paragraphs should be added at the end with the delta
      // recalculated to follow the first one.
      $widget_state['paragraphs'][] = $paragraph_item;
      $field_path = array_slice($button['#parents'], 0, -3);
      static::prepareDeltaPositionConvert($widget_state, $form_state, $field_path, $delta);
    }
    $delta++;
  }
  WidgetBase::setWidgetState($parents, $field_name, $form_state, $widget_state);
  $form_state
    ->setRebuild();
}