Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#23 | 2106979-23.patch | 1.7 KB | shubham.prakash |
| |||
#20 | token-filename_without_extension_2106979-20.patch | 1.69 KB | donaldwbabcock |
| |||
#17 | token-filename_without_extension_2106979-17.patch | 1.81 KB | donaldwbabcock |
Comments
Comment #1
rudolfbykerYou 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:
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
infile.inc.
This should be a feature request for the token module. I will post it there when I have time.
Comment #2
jhodgdonAh, thanks for the explanation. That is definitely confusing! Agreed that this is a token module issue. How about if we just move it there?
Comment #3
rudolfbykerI 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.
Comment #4
jhodgdonComment #5
a.vakulenko CreditAttribution: a.vakulenko commentedBump this.
Comment #6
nikkwong CreditAttribution: nikkwong commentedAny status on this ?
Comment #7
NickDickinsonWildeI 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).
Comment #9
jainaman23 CreditAttribution: jainaman23 at Innoraft commentedTry with
patch -p1 < filename_15.patch
.It worked for me.
Comment #10
rootworkThis 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.
Comment #12
rootworkOh 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.
Comment #13
rootworkThere 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.
Comment #14
maxplus CreditAttribution: maxplus commentedThanks,
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:
But it really does work when I use it like this [group:field-file:file:filename]
Comment #15
Dave ReidThis needs test coverage to confirm the token works as expected.
Comment #16
NickDickinsonWildeoh 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.
Comment #17
donaldwbabcock CreditAttribution: donaldwbabcock commentedI 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.
Comment #18
donaldwbabcock CreditAttribution: donaldwbabcock commentedTesting 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.
Comment #20
donaldwbabcock CreditAttribution: donaldwbabcock commentedOne more time.
Comment #21
donaldwbabcock CreditAttribution: donaldwbabcock commentedComment #22
darrenwh CreditAttribution: darrenwh as a volunteer and at Investis Digital commentedDrupal Coding Standards: The indentation looks wrong here?
Comment #23
shubham.prakash CreditAttribution: shubham.prakash at OpenSense Labs commentedThis patch should fix the issue also coding standards.
Comment #24
darrenwh CreditAttribution: darrenwh as a volunteer and at Investis Digital commentedLooks good
Comment #25
jwilson3Drupal 8 version of this feature request is included here #3107532: Calculate file tokens using filename instead of uri field, add filename token w/o extension .