1

I want pages to have an auto generated path if they are in a menu and another one if they are not in the menu; not an empty path like Pathauto currently generates if the page is not in a menu.

How can I do that?

Details: I'm using this pattern for my pages content type which works perfectly when a page is in a menu.

[node:menu-link:parents:join-path]/[node:menu-link]

The problem is, when a page is not in a menu, it gets an empty path. I want to give it another path based on the "title" of the page, for example.

3 Answers 3

0

Sadly, conditional tokens was requested many times and never implemented, so fallback you ask for is not possible.

You can use Rules instead. Description on Drupal.org claims it's easy - instead of pathauto, set up a rule on node creation / update, and create aliases needed. Rules support conditions, as you can see in Rules examples project, so you can create aliases conditionally all right.

1
  • Sad to hear that. If I find a better answer I'll update here for future reference. Thanks.
    – Mario Awad
    Jan 21, 2014 at 13:06
1

You can now achieve this using hook_pathauto_pattern_alter(). Something like:

<?php
/**
 * Implements hook_pathauto_pattern_alter().
 */
function mymodule_pathauto_pattern_alter(&$pattern, array $context) {
  // Alter any node entity path patterns we need to
  if (isset($context['data']['node'])) {
    $node = $context['data']['node'];

    switch ($node->type) {
      case 'page':
        // Check condition, and change the pattern when appropriate
        $pattern = '[node:title]';
        break;

    }
  }
?>
1
  • Thanks for the code. However, this changes the pattern depending on node type no matter what. I needed a way to change the pattern and regenerate the path only when the original pattern fails to generate a proper path.
    – Mario Awad
    Dec 30, 2014 at 9:46
0

I took what m4olivei wrote and made it work for falling back to node title if no menu item is generated:

/**
 * Implements hook_pathauto_pattern_alter().
 */
function mymodule_pathauto_pattern_alter(&$pattern, array $context) {
  // Set up pathauto fallback to node title if no menu item is generated
  if (isset($context['data']) && isset($context['data']['node'])) {
    if (isset($context['data']['node']->menu) && isset($context['data']['node']->menu['enabled'])) {
      if (!$context['data']['node']->menu['enabled']) {
        $pattern = '[node:title]';
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.