Hi, Pathauto seems like a perfect match for the project I'm working on except for one thing. I would like certain content types to always have a path with a certain directory structure, although I do not care what the ending of the path is. For example, a news page might be:

www.example.com/news/sports/my_page

In this example, I don't mind a person changing 'my_page', but I never want them to mess around with 'news/sports'. Can this already be done or should we add this to the queue?

Thanks!

Comments

klonos’s picture

I was about to reply to ghosty that this can already be done and set this to 'fixed' and 'support request'. After a little extra thinking I've realized that it actually is a valid feature request. First, let me propose a workaround for ghosty's request and I'll get back to this in a next post...

@ghosty: For your case, I see this solution:

- Create a new content type (similar to a page or whatever) and call it 'News' with a machine-readable name of 'news'.
- Create a new taxonomy vocabulary called 'News' with a machine-readable name of 'news'.
- Add some terms in that vocabulary. One of these terms would be 'Sports'
- Add a field called 'News category' in your 'News' content type that will allow you to add tags (assign taxonomy terms to it) from the 'News' vocabulary.
- Ad one of these patterns to your new content type:

[type]/[term-raw]/[title-raw]
[vocab-raw]/[term-raw]/[title-raw]

Both will work as long as the content type name or the vocabulary name are 'News'.

dave reid’s picture

Version: 6.x-1.3 » 7.x-1.x-dev

If you have a need to lock down part of the URL alias, you probably shouldn't let those users have the 'create url alias' permission. Let your trusted editors to be the ones to fix URL aliases.

Since this is a new feature, it would have to go in the Drupal 7 version since 6.x-1.x is closed to feature requests.

dave reid’s picture

Component: Tokens » Code
klonos’s picture

Version: 7.x-1.x-dev » 6.x-1.3
Component: Code » Tokens

... now for the feature request thing:

As it is now, one can set the default auto-path of a content type to be for example '[type]/[title-raw]' (in the new 'Patterns' tab, under the 'Node paths section'). A user with proper rights can uncheck the 'Automatic alias' checkbox under the 'URL path settings' and edit this path.

What ghosty proposes is that the '[type]/' part should NOT be overridden/editable. Only the [title-raw] path could be changed. To achieve this I think we'll need two things:

1. We need a way to distinguish the part of the URL that is permitted to be changed. I propose something like a '*' (as in required fields). For example:

[*type*]/[title-raw]

or

my-custom-path-without-patterns*/[title-raw]

2. We are going to need to give super-users the permission to override required parts of the path. So I guess an extra permission for that(?). Path-related permissions so far are:

path module
- administer url aliases
- create url aliases <-- required in order to enable the 'URL path settings' in node creation/edit
pathauto module
- administer pathauto
- notify of path changes

Perhaps a new 'override required path parts' or 'override fixed path parts' under pathauto module permissions?

So... +1 from me! Thank you ghosty.

dave reid’s picture

Version: 6.x-1.3 » 7.x-1.x-dev
Component: Tokens » Code

Ack, this gets a hearty -1 from me based on my #2.

klonos’s picture

Sorry Dave, I never meant to change this... I started writing a post before you did, but you saved yours before I save mine.

... resetting as Dave did.

klonos’s picture

see... it happened again ;)

Anyways, I hope once this is in 7.x it will be backported to a 6.x-2.x perhaps (?)

klonos’s picture

Title: Assign a directory alias, but let user name the page alias » Add persistent/fixed tokens in addition to current editable tokens.

generalized the title a bit... not perfect yet.

ghosty’s picture

Thanks everyone for looking into this and to klonos for the support. Dave, the reason why I would like users to create their own page aliases is because we will have about 200 users who need this type of feature. At the same time, I want to lock down the first parts of that url and only allow them to name the last part of the url. I think this would be a nice feature in even 6.x, but if it can't be done, hopefully 7.x will see it.

For now, I see that I will probably be stuck with 6.x for sometime. I did think of a solution after looking at klonos' suggestion. First, create a CCK field of type text that will server as the new alias. Then, with the following url:

www.example.com/news/sports/my_page

The path auto settings for the sports content type would be: news/sports/[field_page_alias-raw]

In the form users would fill out, I can easily hide the path alias fieldset and instead create a new fieldset for the new CCK page alias field. Thus, they can now name the last part of the url, but not the first part (news/sports).

One final hurdle - how do you ensure that the alias is not already used? Usually pathauto would take care of this by appending a number, user id, or some other variable that you can set in the pathauto settings. That's nice, but then whatever pathauto sets, how would you take that and insert it back into the CCK page alias field when the author edits their page the next time. I think a better solution is to avoid the pathauto renaming of the alias. Instead, use hook_validate and check the path (news/sports/[field_page_alias-raw]) against the url_alias table. If you find the path, set the form error.

I think that solves my problem. Let me know if any of you have any other ideas or if you think this is a good workaround.

Thanks!
ghosty

klonos’s picture

No problem ghosty. As for your idea for a solution... I honestly couldn't know. Please remember to report back if you have any luck with it.

Now for the use case scenarios... It's ssdd here:

I was explaining to the site owners how to add content (elementary school style... step1: do this... step2: do that). When I was reaching the path settings step of the process, I always needed to stress to them...

- You uncheck the 'Automatic alias' checkbox...
- Then enter a URL... be careful here!...
- You always need to add page/ before the name you need to give to the alias.

Experience showed before that non-tech-savvy people tent to forget 'geeky' things you ask of them. So this will come back and bite me at a later point. I know that what Dave said about not letting those users have the 'create url alias' permission in the first place is a solution (kinda), but this is functionality I was asked to add. And the people asking this are the site owners. So, I am at a dead end with this :/

Issues like this one made me start refreshing my php, css and js coding skills (old c++, vb and delphi coder here), but it will take me a looong time before I start being any good. Plus I need to digest Drupal APIs, module developing and so on.

ghosty’s picture

No kidding klonos, I'm in the same bag as you. My users need that type of alias control because it was an item of need from our manager. I think my solution is 95% doable, but right now I'm working on other projects in the meantime. I suggest that you go with your instincts and start refreshing your PHP coding, that will definitely open up your options.

I consider myself an OK programmer. After looking at the structure of Drupal I'm in awe of how well developed this platform is. Kudos to all the contributors. What's nice is that I'll look at a piece of code and think "wow, that's how they do it, I can add that feature to my other sites". So yeah, dive into the code, don't worry if you're not there yet programming wise because I still don't think I am, but I've come along some way since picking up Drupal a couple months ago. I also suggest reading Pro Drupal Development by John K. VanDyk. He goes through all the nooks and crannies of the system and helps you build some useful modules along the way. A very good way to get your hands Drupal dirty!

ghosty

ghosty’s picture

Hey, I wrote some quick code to see if my idea would work and it did! Here is the code for a the following url www.example.com/news/sports/my_page:

function news_form_validate($form, &$form_state) {

  // Pathauto node subpath
  $sub_path = 'news/sports/';

  // CCK page alias field
  $alias = $form_state['values']['field_page_alias'][0]['value'];
	
  // If there is a value for the CCK page alias
  if (strlen($alias)) {
    // See if the alias exists in the database
    $result = db_result(db_query_range("SELECT pid FROM {url_alias} WHERE dst = CONCAT('%s','%s')", array($sub_path, $alias), 0, 1));
    if ($result) {
      form_set_error('field_page_alias', t('Sorry, this page alias (%alias) is already in use.', array('%alias' => $alias)));
    }
  }
  else {
    // Since the user did not enter a page alias, we use the title 
    $form_state['values']['field_page_alias'][0]['value'] = $form_state['values']['title'];
  }
}
dave reid’s picture

This sounds probably like a good idea for a separate contrib module. For instance if you wanted to allow your users to be able to give themselves an alias for their account pages, but wanted to lock them into only using paths that start with users/*.

ghosty’s picture

Yeah, it works perfectly too. I tested it out a few times. I just updated the code and omitted the call to the pathauto module because pathauto will do the cleaning of the title anyways. Here is the change:

  module_load_include('inc', 'pathauto', 'pathauto');
  $alias = pathauto_cleanstring($form_state['values']['title']);
  $form_state['values']['field_page_alias'][0]['value'] = $alias;

/*** CHANGED TO ***/

  $form_state['values']['field_page_alias'][0]['value'] = $form_state['values']['title'];

klonos’s picture

@ghosty: just to get this straight... this is against 7.x dev code right?

Also this is a simple (hard-coded) solution for any /news/sports/ path right?

greggles’s picture

I think I have a simpler solution:

1. Create a cck text field that allows a single line of text input - call this "URL suffix" or something
2. Make your pattern for this node type "[consistent]/[pattern]/[pieces]/[cck_url_suffix_field]"
3. Enjoy!

I've used this recipe on many sites where we wanted people to have control over the URL (or parts of the URL) but didn't want to give them administer paths permission.

I would mark this as support and fixed except that I didn't read all the comments here and fear I may have missed something.

dave reid’s picture

That actually fulfils the need exactly. We should document that as a recipe in http://drupal.org/handbook/modules/pathauto

Freso’s picture

Title: Add persistent/fixed tokens in addition to current editable tokens. » Add recipe: How to let users control part of the path, without having "administer paths" permission
Component: Code » Documentation
Category: feature » task

We sure should. ;)

ghosty’s picture

Yeah, I tried that before and it works pretty nice. The only things that didn't fly was the ability to not set a suffix if the editor chose not to add one. That's why I wanted a way to grab the title field in that case. I think the only other thing was when the user edited the node once again the suffix in that cck field was not formatted. Thus, from my test users, they would ask where is question mark character in my url since it is still there in the (cck) field. I had a line of help text there saying any non alphanumeric, dash, or underscore character would be converted to a dash, but they still would ask why that wasn't reflected in the field itself.

Klonos, the code I wrote was for 6.x. For 7.x, I think it would be nice to have some type of way to implement a fixed path like klonos suggested. As an added feature, in the url input field, the input field itself would be preceded by this fixed path:

               ____________________
/news/sports/ |                    |
               --------------------

ghosty

greggles’s picture

The only things that didn't fly was the ability to not set a suffix if the editor chose not to add one.

I don't understand what you mean by that.

Regarding the question marks - it sounds like you need some validation on the cck field which is possible in a form_alter by adding a validate function or in a hook_nodeapi.

To be 100% clear: I think the idea of a new feature along these lines is not a reasonable feature for Pathauto.

ghosty’s picture

I just meant that it doesn't act like the normal create alias where if you do not fill in an alias then the system will use the title of your node (if that's how you set it).

webservant316’s picture

how do I compose the token to reference my cck field? cannot figure it out.
now I am using Pathauto 6.x-1.4. will this feature work with that version of path auto?

my cck field label is "URL Path" and the field name itself is "field_url_path".
How do I access the value of that field via a token within Pathauto?

henrijs.seso’s picture

+1. must have for d7

dave reid’s picture

Category: Task » Support request
Issue summary: View changes
Status: Active » Fixed

I would recommend a solution like https://www.drupal.org/project/slug or constructing it manually with a text field and using the field's token value in the Pathauto pattern.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.