You are here

public function AddMediaFormBase::submitForm in Media Directories 2.x

Same name and namespace in other branches
  1. 8 modules/media_directories_ui/src/Form/AddMediaFormBase.php \Drupal\media_directories_ui\Form\AddMediaFormBase::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

1 method overrides AddMediaFormBase::submitForm()
MediaEditForm::submitForm in modules/media_directories_ui/src/Form/MediaEditForm.php
Form submission handler.

File

modules/media_directories_ui/src/Form/AddMediaFormBase.php, line 387

Class

AddMediaFormBase
Class AddMediaFormBase.

Namespace

Drupal\media_directories_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $added_media = $form_state
    ->get('media');

  // Support multi value fields.
  $tmp_tid_mids = [];
  $all_mids = [];
  foreach ($added_media as $delta => $media) {
    EntityFormDisplay::collectRenderDisplay($media, 'media_library')
      ->extractFormValues($media, $form['media'][$delta]['fields'], $form_state);

    // $this->prepareMediaEntityForSave($media);
    $media
      ->save();
    $tmp_tid_mids[isset($media
      ->get('directory')->target_id) ? $media
      ->get('directory')->target_id : MEDIA_DIRECTORY_ROOT][] = $media
      ->id();
    $all_mids[] = $media
      ->id();
  }
  $tid_holding_most_mids = -1;
  foreach ($tmp_tid_mids as $tid => $mids) {
    if (!isset($tmp_tid_mids[$tid_holding_most_mids]) || count($tmp_tid_mids[$tid_holding_most_mids]) < count($tmp_tid_mids[$tid])) {
      $tid_holding_most_mids = $tid;
    }
  }
  $cardinality = $this
    ->getCardinality($form_state);
  if ($form_state
    ->get('selection_mode') != 'keep') {
    $newly_added_media_ids = $tmp_tid_mids[$tid_holding_most_mids];
    if (count($newly_added_media_ids) < count($all_mids)) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('You uploaded medias to different folders. Only medias in the current folder (having the most uploaded media count) are selected.'));
    }
    if ($cardinality > -1 && $cardinality < count($newly_added_media_ids)) {
      $newly_added_media_ids = array_slice($newly_added_media_ids, 0, $cardinality);
      $this
        ->messenger()
        ->addStatus($this
        ->formatPlural($cardinality, 'As this field only accepts one media, only the first one uploaded is selected.', 'You uploaded more medias then allowed for the field, only the first @count are selected.'));
    }
    $form_state
      ->setValue('newly_added_media_ids', $newly_added_media_ids);
  }
  else {
    if ($cardinality > -1 && $cardinality < count($all_mids)) {
      $newly_added_media_ids = array_slice($all_mids, 0, $cardinality);
      $this
        ->messenger()
        ->addStatus($this
        ->formatPlural($cardinality, 'As this field only accepts one media, only the first one uploaded is selected.', 'You uploaded more medias then allowed for the field, only the first @count are selected.'));
    }
    $form_state
      ->setValue('newly_added_media_ids', $all_mids);
  }
  $form_state
    ->setValue('most_choosen_directory_tid', $tid_holding_most_mids);
}