You are here

public function Upload::getForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Widget/Upload.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\Upload::getForm()

Returns widget form.

Parameters

array $original_form: Entire form bult up to this point. Form elements for widget should generally not be added directly to it but returned from funciton as a separated unit.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

array $additional_widget_parameters: Additional parameters that we want to pass to the widget.

Return value

array Form structure.

Overrides WidgetBase::getForm

File

src/Plugin/EntityBrowser/Widget/Upload.php, line 99

Class

Upload
Adds an upload field browser's widget.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\Widget

Code

public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
  $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
  $field_cardinality = $form_state
    ->get([
    'entity_browser',
    'validators',
    'cardinality',
    'cardinality',
  ]);
  $upload_validators = $form_state
    ->has([
    'entity_browser',
    'widget_context',
    'upload_validators',
  ]) ? $form_state
    ->get([
    'entity_browser',
    'widget_context',
    'upload_validators',
  ]) : [];
  $form['upload'] = [
    '#type' => 'managed_file',
    '#title' => $this
      ->t('Choose a file'),
    '#title_display' => 'invisible',
    '#upload_location' => $this->token
      ->replace($this->configuration['upload_location']),
    // Multiple uploads will only be accepted if the source field allows
    // more than one value.
    '#multiple' => $field_cardinality != 1 && $this->configuration['multiple'],
    '#upload_validators' => array_merge([
      'file_validate_extensions' => [
        $this->configuration['extensions'],
      ],
    ], $upload_validators),
  ];
  return $form;
}