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;
  }
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;
  }
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 ; }
Processes the element.

@param array $element
The unprocessed element.
@param \Drupal\Core\Form\FormStateInterface $form_state
The current form state.

@return array
The processed element.
Processes the element .