This is a feature for that which I've seen many requests -- namely, entering a custom alias upon editing or creating a node.
So I did some digging. It turns out the block of code into which I can easily slot this feature isn't doing actually doing anything. Namely, the pathauto_form_alter() function in pathauto.module -- it's supposed to remind relevant users that they need not enter an alias, as Pathauto is funky enough to do just that. But this message never appears. It's also supposed to link admin users to the Pathauto admin page, but even this invisible message is broken due to a whoopsie URI.
Hilarity ensues!
Basically, the conditional statement stops it from executing, as the key $form['#attributes']['id'] doesn't exist (or at least on my Drupal installs). It's looking for the value 'node-form' so as to confirm the page it's on, so I've pointed it at the slightly more existent $form['#id'] key instead. With our text now on the creation/editing page, I've added a checkbox and refined the text to reflect its intent: to allow the user to input their own alias.
And that's about it. Here's a quick screenshot. I've set the status as 'needs work' solely because I'm not convinced $_POST is the best option I could have picked (also, a heap of whitespace needs to be added to pathauto_create_alias()). The sooner someone can prod me in the right direction here -- or even submit an updated patch -- the better.
Let me know what you think.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | pathauto_allow_custom_alias_2.patch | 9.14 KB | meatbites |
| pathauto_allow_custom_alias.patch | 2.55 KB | meatbites |
Comments
Comment #1
gregglesI somewhat wish that you had just continued this in the issue http://drupal.org/node/103596 but no matter - you've got a patch which is great!
I believe you can check the $node array in pathauto_nodeapi to see if any work needs to be done. How does that sound?
Thanks for providing this!
One possible enhancement is to disable the inputbox for the alias until the checkbox is unticked. But that can be saved for another issue.
Comment #2
gregglesthanks to dmitrig here's some js that should help:
#textfieldId and #checkboxId need to be swapped out, of course.
It should be in it's own js file and added via drupal_add_js.
@meatbites - let me know if you want to fixup the $_POST problem and I can handle the rest. Or if you want to get it all together that would be awesome.
Thanks!
Comment #3
mariagwyn commentedIf there is a solution to this, easily patched or in an update, I would REALLY like it as I am having some issues along these lines. Not a coder, or I would offer to help. Sorry.
Thanks for the work,
Maria
Comment #4
meatbites commented@Maria: The patch I've provided works, but you may wish to wait until we properly clean it up. Feel free to try it out, but I wouldn't recommend it for production... just in case we find a boogie monster or somesuch.
@Greg: No worries, I wasn't sure whether to pull up a decidedly old thread or not. Will do in future.
I decided to get some Javascript happening before I go digging around some more. Thanks to the snippet you've provided, here's the working sample:
As you can see, I feel it's a good idea to keep the text input visible, if only to remind the user of the current alias that Pathauto will/might replace. Otherwise we're getting into 'check if alias was customised last time' territory, and I'd like to keep that outside the scope of this update.
Regarding the $_POST issue, I'm still looking around for that one, I'm afraid. You wouldn't know how to handle this, perchance?
Comment #5
meatbites commentedNew patch attached. Includes the requested JS concept (using drupal_add_js with drupal_get_path).
- Code is now properly spaced.
- I've updated the JS from above to support preview pages and to focus the cursor on the text input as soon as it's enabled.
- I've included pathauto.js as a new file in this patch.
Inherently, the $_POST problem stands, the ugly bastard it is.
Comment #6
meatbites commentedI'd really like to see this extra functionality committed -- with Greg's approval, of course -- but I've collided with an impasse on the final issue. The closest I've come to figuring out a $_POST alternative is with this snippet I found in node.module:
However, drupal_get_form doesn't like to play in pathauto.inc (using 'story_node_form' which contains our ['#post']['pathauto'] key).
I'll ask the obvious question, here: what is keeping us from simply checking $_POST? In fact, node.module does exactly this, as you can see from a few lines higher:
That last boolean check is functionally identical to my own.
Inherently it's not as large an issue as I originally thought, so I'm flagging the code for review (pathauto_allow_custom_alias_2.patch).
Comment #7
gregglesI'm not really sure why using $_POST is poor form, I'm just familiar with the idea that it is...
The solution is, fortunately, easy so I committed a slightly modified version just now. Thanks very much, meatbites and dmitrig01.
The value is actually in the $node object that is passed to pathauto_nodeapi so I added the check in there. I also changed the name of the checkbox to pathauto_perform_alias since that seems more semantically correct.
Thanks so much for providing this feature and indulding the need for the javascript - brilliant!
Comment #8
meatbites commentedJust a couple of problems with the latest dev build.
1. The Javascript you've committed was just my first attempt. The one I included in the second patch is what you should use -- otherwise, the script breaks a bit on preview pages. Also, I added a little extra functionality.
2. You've made a minor typo in the .install file on line 110 which causes installation to fail. The comma at line's end should be a semicolon.
Otherwise, all brilliant indeed. You've done well with all of these commits.
Comment #9
gregglesThanks for catching those mistakes. I also had another small mistake - variable_set takes two arguments, not one as I had it. Oh well :/
I believe I have it fixed now, let me know if not.
I do hope you will continue contributing and reviewing patches and if so then having your patches in "cvs diff -up" format would be helpful. If you do that then you have to separately add the js file as an attachment.
Thanks again!
Comment #10
meatbites commentedNo worries, Greg, and thanks for the tip. I'll see if I can't take the CVS path for a spin next time.
Comment #11
gregglesJust a note that I believe this caused a regression - http://drupal.org/node/173429
Comment #12
meatbites commentedCoolies -- I've provided a patch for it in the abovementioned issue.
Comment #13
(not verified) commentedComment #14
lbh commentedThis is a great feature! Thanks Meatbite, Greggles and dmitrig. :)
I have an idea which might accompany this feature pretty well. I was thinking if when the user leaves the title box, and the "Automatic alias" checkbox is ticked, it would be beneficial to see what path is being created in the disabled "URL path settings" text field. This might be helpful for people who occasionally want to alter the path which was generated.
I set out to look a solution, but then I realized I have no idea how to conjure jQuery's magic. My feeling is that it could possibly just need to call pathauto_create_alias(), and use the returned value to fill out the text field... but I'm unsure. It could be very easy or very difficult -- is it worthwhile to create a feature request for this idea?
By the way, Greggles, I'm not a professional coder, but the pathauto code seems a lot cleaner than last time I looked at it. :)
Comment #15
gregglesThe urlify module does basically this. It's an interesting idea, though I'm not sure how weell it would work. By the time that pathauto gets the $node object lots of other modules have already interacted with the data and modified it. Pathauto (and now token) expect to get the data in that modified format and might not know what to do with the data in some other form.
If you want to pursue this it should be filed as it's own separate issue as an enhancement to the existing code.
And thanks for the feedback about the code - could be you are just more familiar with it now ;) Or perhaps just coder module has been helping me clean it up ;)