You are here

public static function Attach::element in FileField Sources 8

Theme the output of the attach element.

File

src/Plugin/FilefieldSource/Attach.php, line 178

Class

Attach
A FileField source plugin to allow use of files within a server directory.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function element($variables) {
  $element = $variables['element'];
  if (isset($element['attach_message'])) {
    $output = $element['attach_message']['#markup'];
  }
  elseif (isset($element['filename'])) {

    // Get rendered options.
    $options = form_select_options($element['filename']);
    $option_output = '';
    foreach ($options as $key => $value) {
      $option_output .= '<option value="' . $value["value"] . '">' . $value["label"] . '</option>';
    }

    // Get rendered select.
    $size = !empty($element['filename']['#size']) ? ' size="' . $element['filename']['#size'] . '"' : '';
    $element['filename']['#attributes']['class'][] = 'form-select';
    $multiple = !empty($element['#multiple']);
    $output = '<select name="' . $element['filename']['#name'] . '' . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple="multiple" ' : '') . new Attribute($element['filename']['#attributes']) . ' id="' . $element['filename']['#id'] . '" ' . $size . '>' . $option_output . '</select>';
  }
  $output .= \Drupal::service('renderer')
    ->render($element['attach']);
  $element['#children'] = $output;
  $element['#theme_wrappers'] = [
    'form_element',
  ];
  return '<div class="filefield-source filefield-source-attach clear-block">' . \Drupal::service('renderer')
    ->render($element) . '</div>';
}