You are here

public static function Clipboard::value in FileField Sources 8

Value callback for file field source plugin.

Parameters

array $element: An associative array containing the properties of the element.

mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

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

Return value

mixed The value to assign to the element.

Overrides FilefieldSourceInterface::value

File

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

Class

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

Namespace

Drupal\filefield_sources\Plugin\FilefieldSource

Code

public static function value(array &$element, &$input, FormStateInterface $form_state) {
  if (isset($input['filefield_clipboard']['contents']) && strlen($input['filefield_clipboard']['contents']) > 0) {

    // Check that the destination is writable.
    $temporary_directory = 'temporary://';
    if (!\Drupal::service('file_system')
      ->prepareDirectory($temporary_directory, FileSystemInterface::MODIFY_PERMISSIONS)) {
      \Drupal::logger('filefield_sources')
        ->log(E_NOTICE, 'The directory %directory is not writable, because it does not have the correct permissions set.', [
        '%directory' => \Drupal::service('file_system')
          ->realpath($temporary_directory),
      ]);
      \Drupal::messenger()
        ->addError(t('The file could not be transferred because the temporary directory is not writable.'), 'error');
      return;
    }

    // Check that the destination is writable.
    $directory = $element['#upload_location'];
    $mode = Settings::get('file_chmod_directory', FileSystem::CHMOD_DIRECTORY);

    // This first chmod check is for other systems such as S3, which don't
    // work with file_prepare_directory().
    if (!\Drupal::service('file_system')
      ->chmod($directory, $mode) && !\Drupal::service('file_system')
      ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
      $url = $input['filefield_clipboard']['filename'];
      \Drupal::logger('filefield_sources')
        ->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', [
        '%file' => $url,
        '%destination' => \Drupal::service('file_system')
          ->realpath($directory),
      ]);
      \Drupal::messenger()
        ->addError(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.', [
        '%file' => $url,
      ]), 'error');
      return;
    }

    // Split the file information in mimetype and base64 encoded binary.
    $base64_data = $input['filefield_clipboard']['contents'];
    $comma_position = strpos($base64_data, ',');
    $semicolon_position = strpos($base64_data, ';');
    $file_contents = base64_decode(substr($base64_data, $comma_position + 1));
    $mimetype = substr($base64_data, 5, $semicolon_position - 5);
    $extension = \Drupal::service('file.mime_type.guesser.extension')
      ->convertMimeTypeToExtension($mimetype);
    $filename = trim($input['filefield_clipboard']['filename']);
    $filename = preg_replace('/\\.[a-z0-9]{3,4}$/', '', $filename);
    $filename = (empty($filename) ? 'paste_' . REQUEST_TIME : $filename) . '.' . $extension;
    $filepath = \Drupal::service('file_system')
      ->createFilename($filename, $temporary_directory);
    $copy_success = FALSE;
    if ($fp = @fopen($filepath, 'w')) {
      fwrite($fp, $file_contents);
      fclose($fp);
      $copy_success = TRUE;
    }
    if ($copy_success && ($file = filefield_sources_save_file($filepath, $element['#upload_validators'], $element['#upload_location']))) {
      if (!in_array($file
        ->id(), $input['fids'])) {
        $input['fids'][] = $file
          ->id();
      }
    }

    // Remove the temporary file generated from paste.
    @unlink($filepath);
  }
}