5 * Contains \Drupal\filefield_sources\Plugin\FilefieldSource\Clipboard.
8 namespace Drupal\filefield_sources\Plugin\FilefieldSource;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\filefield_sources\FilefieldSourceInterface;
12 use Drupal\Core\Site\Settings;
15 * A FileField source plugin to allow transfer of files through the clipboard.
19 * name = @Translation("Paste from clipboard (<a href=""http://drupal.org/node/1775902"">limited browser support</a>)"),
20 * label = @Translation("Clipboard"),
21 * description = @Translation("Allow users to paste a file directly from the clipboard."),
25 class Clipboard implements FilefieldSourceInterface {
30 public static function value(array &$element, &$input, FormStateInterface $form_state) {
31 if (isset($input['filefield_clipboard']['contents']) && strlen($input['filefield_clipboard']['contents']) > 0) {
32 // Check that the destination is writable.
33 $temporary_directory = 'temporary://';
34 if (!file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS)) {
35 \Drupal::logger('filefield_sources')->log(E_NOTICE, 'The directory %directory is not writable, because it does not have the correct permissions set.', array('%directory' => drupal_realpath($temporary_directory)));
36 drupal_set_message(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
39 // Check that the destination is writable.
40 $directory = $element['#upload_location'];
41 $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
43 // This first chmod check is for other systems such as S3, which don't
44 // work with file_prepare_directory().
45 if (!drupal_chmod($directory, $mode) && !file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
46 $url = $input['filefield_clipboard']['filename'];
47 \Drupal::logger('filefield_sources')->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', array('%file' => $url, '%destination' => drupal_realpath($directory)));
48 drupal_set_message(t('The specified file %file could not be copied, because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $url)), 'error');
52 // Split the file information in mimetype and base64 encoded binary.
53 $base64_data = $input['filefield_clipboard']['contents'];
54 $comma_position = strpos($base64_data, ',');
55 $semicolon_position = strpos($base64_data, ';');
56 $file_contents = base64_decode(substr($base64_data, $comma_position + 1));
57 $mimetype = substr($base64_data, 5, $semicolon_position - 5);
59 $extension = \Drupal::service('file.mime_type.guesser.extension')->convertMimeTypeToExtension($mimetype);
61 $filename = trim($input['filefield_clipboard']['filename']);
62 $filename = preg_replace('/\.[a-z0-9]{3,4}$/', '', $filename);
63 $filename = (empty($filename) ? 'paste_' . REQUEST_TIME : $filename) . '.' . $extension;
64 $filepath = file_create_filename($filename, $temporary_directory);
66 $copy_success = FALSE;
67 if ($fp = @fopen($filepath, 'w')) {
68 fwrite($fp, $file_contents);
73 if ($copy_success && $file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location'])) {
74 if (!in_array($file->id(), $input['fids'])) {
75 $input['fids'][] = $file->id();
79 // Remove the temporary file generated from paste.
87 public static function process(array &$element, FormStateInterface $form_state, array &$complete_form) {
88 $element['filefield_clipboard'] = array(
90 '#theme' => 'filefield_sources_element',
91 '#source_id' => 'clipboard',
92 // Required for proper theming.
93 '#filefield_source' => TRUE,
94 '#filefield_sources_hint_text' => t('Enter filename then paste.'),
95 '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
98 $element['filefield_clipboard']['capture'] = array(
100 '#markup' => '<div class="filefield-source-clipboard-capture" contenteditable="true"><span class="hint">example_filename.png</span></div> <span class="hint">' . t('ctrl + v') . '</span>',
101 '#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>.'),
104 $element['filefield_clipboard']['filename'] = array(
106 '#attributes' => array('class' => array('filefield-source-clipboard-filename')),
108 $element['filefield_clipboard']['contents'] = array(
110 '#attributes' => array('class' => array('filefield-source-clipboard-contents')),
113 $class = '\Drupal\file\Element\ManagedFile';
115 'callback' => [$class, 'uploadAjaxCallback'],
118 'element_parents' => implode('/', $element['#array_parents']),
121 'wrapper' => $element['upload_button']['#ajax']['wrapper'],
124 'type' => 'throbber',
125 'message' => t('Transfering file...'),
129 $element['filefield_clipboard']['upload'] = [
130 '#name' => implode('_', $element['#parents']) . '_clipboard_upload_button',
132 '#value' => t('Upload'),
133 '#attributes' => ['class' => ['js-hide']],
135 '#submit' => ['filefield_sources_field_submit'],
136 '#limit_validation_errors' => [$element['#parents']],
137 '#ajax' => $ajax_settings,
144 * Theme the output of the clipboard element.
146 public static function element($variables) {
147 $element = $variables['element'];
149 return '<div class="filefield-source filefield-source-clipboard clear-block">' . drupal_render_children($element) . '</div>';