function paragraphs_library_make_library_item_submit

Make library item submit callback.

Parameters

array $form: The element form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

1 string reference to 'paragraphs_library_make_library_item_submit'

File

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

Code

function paragraphs_library_make_library_item_submit(array $form, FormStateInterface $form_state) {
  $submit = ParagraphsWidget::getSubmitElementInfo($form, $form_state, ParagraphsWidget::ACTION_POSITION_ACTIONS);

  // Replacing element in the array.
  $library_item = LibraryItem::createFromParagraph($submit['widget_state']['paragraphs'][$submit['delta']]['entity']);
  $library_item
    ->save();

  // Replace this paragraph with a library reference one.
  $paragraph_item = [
    'entity' => Paragraph::create([
      'type' => 'from_library',
      'field_reusable_paragraph' => $library_item,
    ]),
    '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();
}