0

I use the Pathauto module and I'm trying to get, for the same content type, two different path aliases depending on a Boolean field that content type has.

I would like to have a path alias like mysite/node_name if the Boolean field value is false and mysite/[a generic term]/node_name if its value is true.

Is there a way to do this?

New contributor
Fizzz69 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    Create a custom node token which outputs a blank string if the node field value is false, and the generic string if it's true. Then use that token as the 2nd URL part, and pathauto will take care of removing the extra slash if the token value is empty – Clive Mar 26 at 16:26
1

if you don't won’t to create a new token as @Clive mentioned in comment, you can use hook_pathauto_alias_alter in your custom module like the following.

/**
 * Implements hook_pathauto_alias_alter().
 */
function MY_MODULE_pathauto_alias_alter(&$alias, array &$context) {
  if ($context['module'] === 'node' && $context['bundle'] === 'MY_BUNDLE') {
    // Get the node.
    $node = $context['data']['node'];

    // Clean string service.
    $clean_string_service = \Drupal::service('pathauto.alias_cleaner');

    $is_checked = $node->field_SOME_NAME->value;
    //   If your field is checked.
    if ($is_checked === 1) {
      $alias = '/mysite/term/' . $clean_string_service->cleanString($node->label());
    }
    else {
      $alias = '/mysite/' . $clean_string_service->cleanString($node->label());
    }
  }
}
  • thanks a lot. I'll try this. – Fizzz69 14 hours ago

Your Answer

Fizzz69 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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