You are here

public static function Clipboard::process in FileField Sources 8

Process callback for file field source plugin.

Parameters

array $element: An associative array containing the properties and children of the generic input element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $complete_form: The complete form structure.

Return value

array The processed element.

Overrides FilefieldSourceInterface::process

File

src/Plugin/FilefieldSource/Clipboard.php, line 85

Class

Clipboard
A FileField source plugin to allow transfer of files through the clipboard.

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $element['filefield_clipboard'] = [
    '#weight' => 100.5,
    '#theme' => 'filefield_sources_element',
    '#source_id' => 'clipboard',
    // Required for proper theming.
    '#filefield_source' => TRUE,
    '#filefield_sources_hint_text' => t('Enter filename then paste.'),
    '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
  ];
  $element['filefield_clipboard']['capture'] = [
    '#type' => 'item',
    '#markup' => '<div class="filefield-source-clipboard-capture" contenteditable="true"><span class="hint">example_filename.png</span></div> <span class="hint">' . t('ctrl + v') . '</span>',
    '#description' => t('Enter a file name and paste an image from the clipboard. This feature only works in <a href="http://drupal.org/node/1775902">limited browsers</a>.'),
  ];
  $element['filefield_clipboard']['filename'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'filefield-source-clipboard-filename',
      ],
    ],
  ];
  $element['filefield_clipboard']['contents'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'class' => [
        'filefield-source-clipboard-contents',
      ],
    ],
  ];
  $class = '\\Drupal\\file\\Element\\ManagedFile';
  $ajax_settings = [
    'callback' => [
      $class,
      'uploadAjaxCallback',
    ],
    'options' => [
      'query' => [
        'element_parents' => implode('/', $element['#array_parents']),
      ],
    ],
    'wrapper' => $element['upload_button']['#ajax']['wrapper'],
    'effect' => 'fade',
    'progress' => [
      'type' => 'throbber',
      'message' => t('Transfering file...'),
    ],
  ];
  $element['filefield_clipboard']['upload'] = [
    '#name' => implode('_', $element['#parents']) . '_clipboard_upload_button',
    '#type' => 'submit',
    '#value' => t('Upload'),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
    '#validate' => [],
    '#submit' => [
      'filefield_sources_field_submit',
    ],
    '#limit_validation_errors' => [
      $element['#parents'],
    ],
    '#ajax' => $ajax_settings,
  ];
  return $element;
}