diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index 5841604..57ae945 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 @@ 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(); @@ -313,6 +313,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin // Prepare destination. $dirname = $settings['uri_scheme'] . '://' . $settings['file_directory']; + $dirname = \Drupal::token()->replace($dirname); 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..5922204 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: '[date:custom:Y]-[date:custom:m]' max_filesize: '30 KB' alt_field: false title_field: false