You are here

protected function YamlFormManagedFileBase::formatItems in YAML Form 8

Format a managed files as array of strings.

Parameters

array $element: An element.

array|mixed $value: A value.

array $options: An array of options.

Return value

array Managed files as array of strings.

2 calls to YamlFormManagedFileBase::formatItems()
YamlFormManagedFileBase::formatHtml in src/Plugin/YamlFormElement/YamlFormManagedFileBase.php
Format an element's value as HTML.
YamlFormManagedFileBase::formatText in src/Plugin/YamlFormElement/YamlFormManagedFileBase.php
Format an element's value as plain text.

File

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

Class

YamlFormManagedFileBase
Provides a base class form 'managed_file' elements.

Namespace

Drupal\yamlform\Plugin\YamlFormElement

Code

protected function formatItems(array &$element, $value, array $options) {
  $fids = is_array($value) ? $value : [
    $value,
  ];
  $files = File::loadMultiple($fids);
  $format = $this
    ->getFormat($element);
  $items = [];
  foreach ($files as $fid => $file) {
    switch ($format) {
      case 'link':
        $items[$fid] = [
          '#theme' => 'file_link',
          '#file' => $file,
        ];
        break;
      case 'id':
        $items[$fid] = $file
          ->id();
        break;
      case 'url':
      case 'value':
      case 'raw':
        $items[$fid] = file_create_url($file
          ->getFileUri());
        break;
      default:
        $theme = str_replace('yamlform_', 'yamlform_element_', $this
          ->getPluginId());
        if (strpos($theme, 'yamlform_') !== 0) {
          $theme = 'yamlform_element_' . $theme;
        }
        $items[$fid] = [
          '#theme' => $theme,
          '#element' => $element,
          '#value' => $value,
          '#options' => $options,
          '#file' => $file,
        ];
        break;
    }
  }
  return $items;
}