You are here

public static function Attach::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/Attach.php, line 30

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 value(array &$element, &$input, FormStateInterface $form_state) {
  if (!empty($input['filefield_attach']['filename'])) {
    $instance = \Drupal::entityTypeManager()
      ->getStorage('field_config')
      ->load($element['#entity_type'] . '.' . $element['#bundle'] . '.' . $element['#field_name']);
    $filepath = $input['filefield_attach']['filename'];

    // 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)) {
      \Drupal::logger('filefield_sources')
        ->log(E_NOTICE, 'File %file could not be copied, because the destination directory %destination is not configured correctly.', [
        '%file' => $filepath,
        '%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' => $filepath,
      ]), 'error');
      return;
    }

    // Clean up the file name extensions and transliterate.
    $original_filepath = $filepath;
    $new_filepath = filefield_sources_clean_filename($filepath, $instance
      ->getSetting('file_extensions'));
    rename($filepath, $new_filepath);
    $filepath = $new_filepath;

    // Run all the normal validations, minus file size restrictions.
    $validators = $element['#upload_validators'];
    if (isset($validators['file_validate_size'])) {
      unset($validators['file_validate_size']);
    }

    // Save the file to the new location.
    if ($file = filefield_sources_save_file($filepath, $validators, $directory)) {
      if (!in_array($file
        ->id(), $input['fids'])) {
        $input['fids'][] = $file
          ->id();
      }

      // Delete the original file if "moving" the file instead of copying.
      if ($element['#filefield_sources_settings']['source_attach']['attach_mode'] !== FILEFIELD_SOURCE_ATTACH_MODE_COPY) {
        @unlink($filepath);
      }
    }

    // Restore the original file name if the file still exists.
    if (file_exists($filepath) && $filepath != $original_filepath) {
      rename($filepath, $original_filepath);
    }
    $input['filefield_attach']['filename'] = '';
  }
}