Commit f081fa81 authored by effulgentsia's avatar effulgentsia

Issue #2128055 by slashrsm, anavarre, timhilliard, estoyausente, nielsvm,...

Issue #2128055 by slashrsm, anavarre, timhilliard, estoyausente, nielsvm, effulgentsia, rteijeiro, rootwork, stijntilleman, jonhattan, Crell, webchick, Dave Reid, attiks, meba, Fidelix, Pls, toamit, brantwynn, YesCT, erwangel: Files should be uploaded to directories based on tokens by default
parent 4e760c35
......@@ -51,7 +51,7 @@ public static function defaultStorageSettings() {
public static function defaultFieldSettings() {
return array(
'file_extensions' => 'txt',
'file_directory' => '',
'file_directory' => '[date:custom:Y]-[date:custom:m]',
'max_filesize' => '',
'description_field' => 0,
) + parent::defaultFieldSettings();
......@@ -267,7 +267,22 @@ public static function validateMaxFilesize($element, FormStateInterface $form_st
* @see token_replace()
*/
public function getUploadLocation($data = array()) {
$settings = $this->getSettings();
return static::doGetUploadLocation($this->getSettings(), $data);
}
/**
* Determines the URI for a file field.
*
* @param array $settings
* The array of field settings.
* @param array $data
* An array of token objects to pass to token_replace().
*
* @return string
* An unsanitized file directory URI with tokens replaced. The result of
* the token replacement is then converted to plain text and returned.
*/
protected static function doGetUploadLocation(array $settings, $data = []) {
$destination = trim($settings['file_directory'], '/');
// Replace tokens. As the tokens might contain HTML we convert it to plain
......@@ -312,7 +327,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
$settings = $field_definition->getSettings();
// Prepare destination.
$dirname = $settings['uri_scheme'] . '://' . $settings['file_directory'];
$dirname = static::doGetUploadLocation($settings);
file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
// Generate a file entity.
......
......@@ -19,20 +19,29 @@ class FileFieldPathTest extends FileFieldTestBase {
* Tests the normal formatter display on node display.
*/
function testUploadPath() {
/** @var \Drupal\node\NodeStorageInterface $node_storage */
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$field_name = strtolower($this->randomMachineName());
$type_name = 'article';
$this->createFileField($field_name, 'node', $type_name);
/** @var \Drupal\file\FileInterface $test_file */
$test_file = $this->getTestFile('text');
// Create a new node.
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
// Check that the file was uploaded to the file root.
// Check that the file was uploaded to the correct location.
$node_storage->resetCache(array($nid));
$node = $node_storage->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this->assertPathMatch('public://' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri())));
/** @var \Drupal\file\FileInterface $node_file */
$node_file = $node->{$field_name}->entity;
$date_formatter = $this->container->get('date.formatter');
$expected_filename =
'public://' .
$date_formatter->format(REQUEST_TIME, 'custom', 'Y') . '-' .
$date_formatter->format(REQUEST_TIME, 'custom', 'm') . '/' .
$test_file->getFilename();
$this->assertPathMatch($expected_filename, $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->getFileUri())));
// Change the path to contain multiple subdirectories.
$this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz'));
......
......@@ -17,7 +17,7 @@ translatable: true
default_value: { }
default_value_callback: ''
settings:
file_directory: field/image
file_directory: '[date:custom:Y]-[date:custom:m]'
file_extensions: 'png gif jpg jpeg'
max_filesize: ''
max_resolution: ''
......
......@@ -18,7 +18,7 @@ default_value: { }
default_value_callback: ''
settings:
file_extensions: 'png gif jpg jpeg'
file_directory: pictures
file_directory: 'pictures/[date:custom:Y]-[date:custom:m]'
max_filesize: '30 KB'
alt_field: false
title_field: false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment