0

I'm working on a drupal 8 project, I need to access the content type X nodes via two aliases depending on some conditions, for this I've created two Pathauto patterns using Pathauto contrib module:

path_1 --> /local/[node:title]

path_2 --> /global/[node:title]

path_1 & path_2 are supposed the machine name of those two patterns.

What are those two patterns? actually what I want to do is check if a node of type X has the checkbox field 'global' checked so use /global/[node:title] as alias for this node else use /local/[node:title].

Which hook should I implements for selecting/altering a pathauto pattern for a node? And can I use those aliases as a route for a controller?

2 Answers 2

2

Another option, and I find this to be preferable to altering the token because it's exposed to site builders and makes what's happening perfectly clear in the UI, is to create your own custom token that will return /global or /local for that piece of the url. You just need to implement hook_token_info and hook_tokens. This blog post walks you through the process.

Ex:

/**
 * Implements hook_token_info().
 */
function MY_MODULE_token_info() {
  $info['tokens']['node']['MY_TOKEN_ID'] = [
    'name' => t('MY TOKEN LABEL'),
    'description' => t('Return "/global" for nodes with the global checkbox checked, otherwise returns "/local".'),
    'type' => 'url-fragment',
  ];
  return $info;
}

/**
 * Implements hook_tokens().
 */
function MY_MODULE_tokens($type, $tokens, array $data, array $options, Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'node' && !empty($data['node']) && isset($tokens['MY_TOKEN_ID'])) {
    $node = $data['node'];
    $global_or_local = '/local';
    if ($node->hasField('MY_GLOBAL_FIELD') && !empty($node->MY_GLOBAL_FIELD->value)) {
      $global_or_local = '/global';
    }
    $replacements['[node:MY_TOKEN_ID]'] = $global_or_local;
  }
  return $replacements;
}

Note: I'm not sure if that logic to determine global or local is perfect, but it should be close.

0

Try with hook_pathauto_pattern_alter something like:


/**
 * Implements hook_pathauto_pattern_alter().
 */
function your_module_pathauto_pattern_alter(\Drupal\pathauto\PathautoPatternInterface &$pattern, array $context){
    /* @var $node Drupal\node\Entity\Node */
    //- If you want it just on update operation add $context['op'] == 'update' condition 
    if ($context['module'] !== 'node' || !($node = $context['data']['node']) || $node->bundle() !== 'type_X') {
        if ($node->global) {
            $pattern->setPattern('global/[node:title]');
        }else{
             $pattern->setPattern('local/[node:title]');
        }
    }
}

3
  • what does setPattern methode do exactly??
    – MiharbKH
    Sep 27, 2018 at 12:05
  • does setPattern methode update/re-generate the path matching the new pattern value or it's necessary to update/re-generate path via BO
    – MiharbKH
    Sep 27, 2018 at 12:15
  • 1
    On creation and update of nodes that hook update the pattern. Depends on the condition. you don't have to update it on BO, at least the new created nodes.
    – berramou
    Sep 27, 2018 at 13:18

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.