Make library item submit callback.
array $form: The element form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state object.
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();
}