You are here

public static function WidgetSubmit::doSubmit in Library 8

Submits the widget elements, saving and deleted entities where needed.

Parameters

array $form: The form.

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

File

src/WidgetSubmit.php, line 44

Class

WidgetSubmit
Performs widget submission.

Namespace

Drupal\library

Code

public static function doSubmit(array $form, FormStateInterface $form_state) {
  $callback = $form_state
    ->getFormObject();
  $entity = $callback
    ->getEntity();

  /** @var \Drupal\node\Entity\Node $entity */
  $fields = $entity
    ->getFieldDefinitions();

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */
  $libraryFields = [];
  foreach ($fields as $field) {
    if ($field
      ->getType() == 'library_item_field_type') {
      $libraryFields[] = $field;
    }
  }
  if (count($libraryFields) > 1) {
    \Drupal::messenger()
      ->addError(t('Only one library field per bundle supported.'));
    return;
  }
  if (isset($libraryFields[0])) {
    $submittedValues = $form_state
      ->getValue($libraryFields[0]
      ->getName());
    self::processLibraryItems($libraryFields[0], $submittedValues, $entity);
  }
}