It would be helpful to have a file token that would give you the file name without the extension. The existing token [file:basename] does not strip the extension.

Original report by jhodgdon on project Bulk Media Upload:

When I set the title to use [file:basename] instead of [file:name], it is still putting the .jpg extensions from the file name into the node titles.

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

fmr’s picture

You will only see the file:basename option if you have the token module installed, which provides extra functionality over the tokens that have been moved to Drupal 7 core (see http://drupal.org/node/113614). Here is a snippet from token.tokens.inc:

  // File tokens.
  if ($type == 'file' && !empty($data['file'])) {
    $file = $data['file'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'basename':
          $basename = pathinfo($file->uri, PATHINFO_BASENAME);
          $replacements[$original] = $sanitize ? check_plain($basename) : $basename;
          break;
        case 'extension':
          $extension = pathinfo($file->uri, PATHINFO_EXTENSION);
          $replacements[$original] = $sanitize ? check_plain($extension) : $extension;
          break;
        case 'size-raw':
          $replacements[$original] = (int) $file->filesize;
          break;
      }
    }
  }

It uses the PHP pathinfo function for the basename. "PATHINFO_BASENAME" is confusing IMHO, because it actually refers to the filename with all of the extentions, while "PATHINFO_FILENAME" refers to the filename with the last extention (but not all extentions) stripped. See http://php.net/manual/en/function.pathinfo.php for more info.

AFAIK, there is currently no token that uses PATHINFO_FILENAME, although the php function is used directly in function file_scan_directory in file.inc.

This should be a feature request for the token module. I will post it there when I have time.

jhodgdon’s picture

Title: Tokens for title not being respected? » Could we have a file token that gives you the file name without extension
Project: Bulk Media Upload » Token
Category: Bug report » Feature request
Issue summary: View changes

Ah, thanks for the explanation. That is definitely confusing! Agreed that this is a token module issue. How about if we just move it there?

fmr’s picture

I did not realise moving a post is possible. But it's kind of obvious, knowing how Drupal works. :D I also wrote about it here https://drupal.org/comment/8175101#comment-8175101 yesterday.

jhodgdon’s picture

a.vakulenko’s picture

Bump this.

nikkwong’s picture

Any status on this ?

NickWilde’s picture

Status: Active » Needs review
FileSize
1.29 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch filename_15.patch. Unable to apply patch. See the log in the details link for more information. View

I needed this functionality for a project; so I wrote it up. Patch attached. (the diff's area includes changes from https://www.drupal.org/node/2386003 but not in the changeset itself).

Status: Needs review » Needs work

The last submitted patch, 7: filename.patch, failed testing.

jainaman23’s picture

Try with patch -p1 < filename_15.patch.

It worked for me.

rootwork’s picture

Status: Needs work » Needs review
FileSize
1.29 KB

This does seem to still apply. Let's see if the testbot picks up on it this time. This is the same patch as in #7.

Status: Needs review » Needs work

The last submitted patch, 10: token_filename-without-extension_2106979-7.patch, failed testing.

rootwork’s picture

Status: Needs work » Needs review
FileSize
1.15 KB

Oh I see, it applied but with fuzz because the surrounding lines had changed. Let's see if this works.

Functionally this is the same patch as #7. No interdiff because only the surrounding lines changed.

rootwork’s picture

Status: Needs review » Reviewed & tested by the community

There we go. I'm marking this RTBC as it was reported as working in both #9 and (by me) in #10. If maintainers need to review other aspects of what this patch does prior to committing, perhaps they can comment on that here.

maxplus’s picture

Thanks,
is working for me.

I'm using it so set a auto entity label of a Group.

It is not displayed in the token browser:

Field file - [group:field-file:?] - Field "field_file". The following properties may be appended to the token: file (The file.)

But it really does work when I use it like this [group:field-file:file:filename]

Dave Reid’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests

This needs test coverage to confirm the token works as expected.

NickWilde’s picture

oh forgot totally about that. That was in my misty past when I didn't really know testing. To be fair I still don't know the D7 testing system very well - I'm much more comfortable with D8 tests. But I'd like to see this closed instead of having that in my patch queue on a couple sites. So if no one gets to it before then, I'll try to do some tests for this next week.

donaldwbabcock’s picture

I needed this for a project as well. So here is the patch against 7.x-1.x-dev (2017-Jan-25). The patch also successfully applies against 7.x-1.7, where I tested it in one of my dev projects. I was able to use [file:filename] without issue.

I changed the description of the token slightly to not conflict with [file:name].

I added the token to the two existing tests for file tokens.

donaldwbabcock’s picture

Testing is in a holding pattern due to a previous commit. Issue #2871343: Tests are failing is seeking to resolve current test failure in the 7.x-1.x-dev branch.

Status: Needs review » Needs work

The last submitted patch, 17: token-filename_without_extension_2106979-17.patch, failed testing.

donaldwbabcock’s picture

donaldwbabcock’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
darrenwh’s picture

Status: Needs review » Needs work
+++ b/token.test
@@ -1039,6 +1039,7 @@ class TokenFileTestCase extends TokenTestHelper {
+	  'filename' => 'test',

@@ -1050,6 +1051,7 @@ class TokenFileTestCase extends TokenTestHelper {
+	  'filename' => 'test',

+++ b/token.tokens.inc
@@ -543,6 +547,10 @@ function token_tokens($type, $tokens, array $data = array(), array $options = ar
+		case 'filename':

Drupal Coding Standards: The indentation looks wrong here?