diff --git a/core/modules/field/field.tokens.inc b/core/modules/field/field.tokens.inc new file mode 100644 index 0000000..41c66ab --- /dev/null +++ b/core/modules/field/field.tokens.inc @@ -0,0 +1,50 @@ + [ + 'field-storage' => [ + 'name' => t('Field storage'), + 'description' => t('Tokens related to field storage items.'), + 'needs-data' => 'field-storage', + ], + ], + 'tokens' => [ + 'field-storage' => [ + 'name' => [ + 'name' => t('Field name'), + 'description' => t('Machine name of the field.'), + ], + ], + ], + ]; +} + +/** + * Implements hook_tokens(). + */ +function field_tokens($type, $tokens, array $data = array(), array $options = array()) { + $replacements = []; + + if ($type == 'field-storage' && !empty($data['field-storage'])) { + /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage */ + $field_storage = $data['field-storage']; + + foreach ($tokens as $name => $original) { + switch ($name) { + case 'name': + $replacements[$original] = $field_storage->getName(); + break; + } + } + } + + return $replacements; +} diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php index 5841604..fe12a87 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' => '[field-storage:name]/[date:custom:Y]/[date:custom:m]', 'max_filesize' => '', 'description_field' => 0, ) + parent::defaultFieldSettings(); @@ -270,8 +270,8 @@ public function getUploadLocation($data = array()) { $settings = $this->getSettings(); $destination = trim($settings['file_directory'], '/'); - // Replace tokens. As the tokens might contain HTML we convert it to plain - // text. + $data += ['field-storage' => $this->getFieldDefinition()->getFieldStorageDefinition()]; + // Replace tokens. As the tokens might contain HTML we convert to plaintext. $destination = PlainTextOutput::renderFromHtml(\Drupal::token()->replace($destination, $data)); return $settings['uri_scheme'] . '://' . $destination; } @@ -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, ['field-storage' => $field_definition->getFieldStorageDefinition()]); 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..1f73614 100644 --- a/core/modules/file/src/Tests/FileFieldPathTest.php +++ b/core/modules/file/src/Tests/FileFieldPathTest.php @@ -19,20 +19,30 @@ 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://' . + $field_name . '/' . + $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..dc84244 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: '[field-storage:name]/[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..4923c88 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: '[field-storage:name]/[date:custom:Y]/[date:custom:m]' max_filesize: '30 KB' alt_field: false title_field: false