summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoreffulgentsia2015-10-20 20:17:55 (GMT)
committereffulgentsia2015-10-20 20:17:55 (GMT)
commitf081fa811f6a0607530103f0c3b99432061a1d95 (patch)
tree7c64708bf0ac7c82318060ee4a780432199ea38a
parent4e760c35bb16860356d19a64f35945ba46314ceb (diff)
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
-rw-r--r--core/modules/file/src/Plugin/Field/FieldType/FileItem.php21
-rw-r--r--core/modules/file/src/Tests/FileFieldPathTest.php15
-rw-r--r--core/profiles/standard/config/install/field.field.node.article.field_image.yml2
-rw-r--r--core/profiles/standard/config/install/field.field.user.user.user_picture.yml2
4 files changed, 32 insertions, 8 deletions
diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
index 5841604..1633147 100644
--- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
+++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
@@ -51,7 +51,7 @@ class FileItem extends EntityReferenceItem {
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 @@ class FileItem extends EntityReferenceItem {
* @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 @@ class FileItem extends EntityReferenceItem {
$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.
diff --git a/core/modules/file/src/Tests/FileFieldPathTest.php b/core/modules/file/src/Tests/FileFieldPathTest.php
index 346fc7a..acafd4b 100644
--- a/core/modules/file/src/Tests/FileFieldPathTest.php
+++ b/core/modules/file/src/Tests/FileFieldPathTest.php
@@ -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'));
diff --git a/core/profiles/standard/config/install/field.field.node.article.field_image.yml b/core/profiles/standard/config/install/field.field.node.article.field_image.yml
index b69d603..b4b1c14 100644
--- a/core/profiles/standard/config/install/field.field.node.article.field_image.yml
+++ b/core/profiles/standard/config/install/field.field.node.article.field_image.yml
@@ -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: ''
diff --git a/core/profiles/standard/config/install/field.field.user.user.user_picture.yml b/core/profiles/standard/config/install/field.field.user.user.user_picture.yml
index 5145085..b2e61f6 100644
--- a/core/profiles/standard/config/install/field.field.user.user.user_picture.yml
+++ b/core/profiles/standard/config/install/field.field.user.user.user_picture.yml
@@ -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