diff --git a/core/modules/field/field.module b/core/modules/field/field.module index d8be776..d59e418 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -13,6 +13,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\field\Entity\FieldStorageConfig; +use Drupal\Component\Utility\String; /* * Load all public Field API functions. Drupal currently has no @@ -339,3 +340,49 @@ function field_form_config_admin_import_form_alter(&$form, FormStateInterface $f } } } + +/** + * Implements hook_token_info(). + */ +function field_token_info() { + return [ + 'types' => [ + 'field-storage' => [ + 'name' => t('Field storage'), + 'description' => t('Tokens related to field storage items.'), + 'needs-data' => 'field-stoarge', + ], + ], + '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()) { + $sanitize = !empty($options['sanitize']); + $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] = $sanitize ? String::checkPlain($field_storage->getName()) : $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 bb95185..5a16644 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php @@ -50,7 +50,7 @@ public static function defaultStorageSettings() { public static function defaultFieldSettings() { return array( 'file_extensions' => 'txt', - 'file_directory' => 'field/[field_name]/[date:custom:Y]/[date:custom:m]', + 'file_directory' => 'field/[field-storage:name]/[date:custom:Y]/[date:custom:m]', 'max_filesize' => '', 'description_field' => 0, ) + parent::defaultFieldSettings(); @@ -269,8 +269,7 @@ public function getUploadLocation($data = array()) { $settings = $this->getSettings(); $destination = trim($settings['file_directory'], '/'); - // There is no token for field name so we have to use a little workaround. - $destination = str_replace('[field_name]', $this->parent->getName(), $destination); + $data += ['field-storage' => $this->getFieldDefinition()->getFieldStorageDefinition()]; // Replace tokens. $destination = \Drupal::token()->replace($destination, $data); @@ -319,8 +318,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin * @see FileItem::getUploadLocation() */ $destination = trim($settings['file_directory'], '/'); - $destination = str_replace('[field_name]', $field_definition->getName(), $destination); - $destination = \Drupal::token()->replace($destination, []); + $destination = \Drupal::token()->replace($destination, ['field-storage' => $field_definition->getFieldStorageDefinition()]); // Generate a file entity. $destination = $settings['uri_scheme'] . '://' . $destination . '/' . $random->name(10, TRUE) . '.txt';