You are here

public static function InteractiveUpload::process in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 src/Element/InteractiveUpload.php \Drupal\lightning_media\Element\InteractiveUpload::process()
  2. 8.2 src/Element/InteractiveUpload.php \Drupal\lightning_media\Element\InteractiveUpload::process()
  3. 8.3 src/Element/InteractiveUpload.php \Drupal\lightning_media\Element\InteractiveUpload::process()

Processes the element.

Parameters

array $element: The unprocessed element.

FormStateInterface $form_state: The current form state.

Return value

array The processed element.

1 call to InteractiveUpload::process()
AjaxUpload::process in src/Element/AjaxUpload.php
Processes the element.
1 method overrides InteractiveUpload::process()
AjaxUpload::process in src/Element/AjaxUpload.php
Processes the element.

File

src/Element/InteractiveUpload.php, line 45

Class

InteractiveUpload
A form element for uploading or deleting files interactively.

Namespace

Drupal\lightning_media\Element

Code

public static function process(array $element, FormStateInterface $form_state) {
  $element['fid'] = [
    '#type' => 'hidden',
  ];
  $element['upload'] = $element['remove'] = [
    '#type' => 'submit',
    '#is_button' => TRUE,
    '#limit_validation_errors' => [
      $element['#parents'],
    ],
    '#weight' => 100,
  ];
  $element['upload']['#value'] = t('Upload');
  $element['upload']['#submit'][] = [
    static::class,
    'upload',
  ];
  $element['remove']['#value'] = t('Remove');
  $element['remove']['#submit'][] = [
    static::class,
    'remove',
  ];
  $key = array_merge($element['#parents'], [
    'fid',
  ]);

  // Don't use $form_state->hasValue(), because it will return TRUE if the
  // value exists and is falsy. Valid file IDs will always be truthy.
  $fid = $form_state
    ->getValue($key);
  if ($fid) {
    $element['fid']['#value'] = $fid;
    $element['file'] = [
      '#theme' => 'file_link',
      '#file' => File::load($fid),
    ];
    $element['upload']['#access'] = FALSE;
  }
  else {
    $element['file'] = [
      '#type' => 'upload',
      '#title' => $element['#title'],
      '#upload_location' => $element['#upload_location'],
      '#upload_validators' => $element['#upload_validators'],
    ];
    $element['remove']['#access'] = FALSE;
  }
  return $element;
}