You are here

function paragraphs_library_library_item_unlink_submit in Paragraphs 8

Convert library item submit callback.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

1 string reference to 'paragraphs_library_library_item_unlink_submit'
paragraphs_library_paragraphs_widget_actions_alter in modules/paragraphs_library/paragraphs_library.module
Implements hook_paragraphs_widget_actions_alter().

File

modules/paragraphs_library/paragraphs_library.module, line 233
Main module file for the Paragraphs Library module.

Code

function paragraphs_library_library_item_unlink_submit(array $form, FormStateInterface $form_state) {
  $submit = ParagraphsWidget::getSubmitElementInfo($form, $form_state, ParagraphsWidget::ACTION_POSITION_ACTIONS);
  $submit['widget_state']['original_deltas'] = array_merge($submit['widget_state']['original_deltas'], [
    '1' => 1,
  ]);

  /** @var \Drupal\Paragraphs\ParagraphInterface $paragraph */
  $paragraph = $submit['widget_state']['paragraphs'][$submit['delta']]['entity'];
  $original_paragraph = NULL;
  if ($paragraph
    ->hasField('field_reusable_paragraph')) {

    /** @var \Drupal\paragraphs_library\Entity\LibraryItem $library_item */
    $library_item = $paragraph
      ->get('field_reusable_paragraph')->entity;
    if ($library_item) {
      $original_paragraph = $library_item
        ->get('paragraphs')->entity;
    }
  }

  // Do nothing in case we fail to get the original paragraph.
  if (!$original_paragraph) {
    return;
  }

  // Make a copy of the paragraph. Use the Replicate module, if it is enabled.

  /** @var \Drupal\Core\Entity\EntityInterface $original_paragraph */
  if (\Drupal::hasService('replicate.replicator')) {
    $duplicate_paragraph = \Drupal::getContainer()
      ->get('replicate.replicator')
      ->cloneEntity($original_paragraph);
  }
  else {
    $duplicate_paragraph = $original_paragraph
      ->createDuplicate();
  }
  $paragraph_item = [
    'entity' => $duplicate_paragraph,
    'display' => $submit['widget_state']['paragraphs'][$submit['delta']]['display'],
    'mode' => 'edit',
  ];
  $submit['widget_state']['paragraphs'][$submit['delta']] = $paragraph_item;
  WidgetBase::setWidgetState($submit['parents'], $submit['field_name'], $form_state, $submit['widget_state']);
  $form_state
    ->setRebuild();
}