You are here

public function BlockUploadForm::submitForm in Block Upload 8

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

File

src/Form/BlockUploadForm.php, line 120

Class

BlockUploadForm
Configure book settings for this site.

Namespace

Drupal\block_upload\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $nid = $values['block_upload_nid'];
  if (isset($values['block_upload_file']['fids']['0'])) {
    $file = $values['block_upload_file']['fids']['0'];
  }
  elseif (isset($values['block_upload_file']['0'])) {
    $file = $values['block_upload_file']['0'];
  }
  else {
    $file = '';
  }
  $buid = $values['buid'];
  $field_name = explode('.', \Drupal::state()
    ->get('block_upload_' . $buid . '_field') ?: '')[1];
  $node = Node::load($nid);
  if (isset($values['remove_files'])) {
    if (array_filter($values['remove_files'])) {
      BlockUploadBuild::blockUploadDeleteFiles($node, $field_name, $values);
    }
  }
  if (!empty($values['block_upload_file']['fids']) || !empty($values['block_upload_file'])) {
    $new_file['target_id'] = $file;
    if (isset($values['block_upload_' . $buid . '_alt'])) {
      $alt = Html::escape($values['block_upload_' . $buid . '_alt']);
      $new_file['alt'] = $alt;
    }
    if (isset($values['block_upload_' . $buid . '_title'])) {
      $title = Html::escape($values['block_upload_' . $buid . '_title']);
      $new_file['title'] = $title;
    }
    if (isset($values['block_upload_' . $buid . '_desc'])) {
      $desc = Html::escape($values['block_upload_' . $buid . '_desc']);
      $new_file['description'] = $desc;
    }
    $node
      ->get($field_name)
      ->appendItem($new_file);
    $this
      ->messenger()
      ->addMessage(t('File was successfully uploaded!'));
  }
  $node
    ->save();
}