You are here

public function YamlFormManagedFileBase::prepare in YAML Form 8

Prepare an element to be rendered within a form.

Parameters

array $element: An element.

\Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission: A form submission.

Overrides YamlFormElementBase::prepare

File

src/Plugin/YamlFormElement/YamlFormManagedFileBase.php, line 98

Class

YamlFormManagedFileBase
Provides a base class form 'managed_file' elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

public function prepare(array &$element, YamlFormSubmissionInterface $yamlform_submission) {
  parent::prepare($element, $yamlform_submission);

  // Check if the URI scheme exists and can be used the upload location.
  $scheme_options = self::getVisibleStreamWrappers();
  $uri_scheme = $this
    ->getUriScheme($element);
  if (!isset($scheme_options[$uri_scheme])) {
    $element['#access'] = FALSE;
    $this
      ->displayDisabledWarning($element);
  }
  else {
    $element['#upload_location'] = $this
      ->getUploadLocation($element, $yamlform_submission
      ->getYamlForm());
  }
  $element['#upload_validators']['file_validate_size'] = [
    $this
      ->getMaxFileSize($element),
  ];
  $element['#upload_validators']['file_validate_extensions'] = [
    $this
      ->getFileExtensions($element),
  ];

  // Use custom validation callback so that File entities can be converted
  // into file ids (akk fids).
  $element['#element_validate'][] = [
    get_class($this),
    'validate',
  ];

  // Add file upload help to the element.
  $element['help'] = [
    '#theme' => 'file_upload_help',
    '#description' => '',
    '#upload_validators' => $element['#upload_validators'],
    '#cardinality' => empty($element['#multiple']) ? 1 : -1,
    '#prefix' => '<div class="description">',
    '#suffix' => '</div>',
  ];
}