1

I need to create double URL alias for commerce product. I tried with code below :

function mymodule_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
    if($entity->getEntityTypeId() == 'commerce_product'){
        $alias = '/store/'.$entity->getTitle();
        $source = '/product/' . $entity->id();
        $path = \Drupal::service('path.alias_storage')->save($source, $alias, "en");
    }
}

but doesn't work. My alias is overridden by pathauto and I have 2 equal alias. I need to create /catalog/[commerce_product:title] and /store/[commerce_product:title] URLs.

The first one is set with pathauto pattern and created succesfully. The second one is create with the code above.

After updating the product i have 2 equal URLs:

  1. /catalog/product-title
  2. /catalog/product-title

why do not I have

  1. /catalog/product-title
  2. /store/product-title

how can I save double different alias?

Thanks in advance.

4
  • Where are you setting $alias? Please provide your complete code example.
    – Chance G
    Feb 22, 2018 at 3:04
  • I updated my code.
    – Fabio Riva
    Feb 22, 2018 at 8:43
  • Your example is still bogus. Setting all products to the same alias obviously won't work. Please share your actual code.
    – Chance G
    Feb 22, 2018 at 18:55
  • i updated the code with the right alias text.
    – Fabio Riva
    Feb 22, 2018 at 22:02

3 Answers 3

1

In your hook_entity_update() do like below

    function mymodule_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
      if($entity->getEntityTypeId() == 'commerce_product'){
        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
        $aliasStorage = \Drupal::service('path.alias_storage');
        $source = '/product/' . $entity->id();
        if ($aliasStorage->aliasExists($source, $langcode)) {
          // alias already exist. So save your second alias here.
          $aliasStorage->save('/product/' . $entity->id(), YOURNEWALIAS, $language);

        }
        else {
           $aliasStorage->save('/product/' . $entity->id(), YOURNEWALIAS_1, $language);
           $aliasStorage->save('/product/' . $entity->id(), YOURNEWALIAS_2, $language);
        }
        }
    }
1
  • Your code is almost the same as mine. i think that YOURNEWALIAS is overriden by pathauto patterns. I have updated the question for more informations. Thanks
    – Fabio Riva
    Feb 22, 2018 at 11:40
0
function mymodule_node_update(Drupal\Core\Entity\EntityInterface $node) {
  multiple_alias_callback($node);
}

function multiple_alias_callback(Drupal\Core\Entity\EntityInterface $node) {
// Set of conditions as per your requirement
  $new_alias_val = '/your/custom/new/alias';
  $node->path = \Drupal::service('path.alias_storage')->save('/node/' . $node->id(), $new_alias_val);
}

This is example for saving multiple alias for node, you can use that for saving multiple alias for entity. Please update your code like this $node->path for saving.

0

To store double URL alias you have to disable pathauto patterns created in admin/config/search/path/patterns. Generate desired pattern for products or nodes. In this example pattern_id_1, pattern_id_2. Disable them.

After that you have to use this code:

function MYMODULE_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
    // Convert to lowercase, trim spaces, replace spaces with underscore and then truncate to 60 chars.
    if($entity->getEntityTypeId() == 'commerce_product'){
        //aggiungo URL sezione store
        $aliasStorage = \Drupal::service('path.alias_storage');
        $langcode = $entity->language()->getId();
        $source = '/product/' . $entity->id();

        $alias = MYMODULE_get_product_alias($entity, 'pattern_id_1');

        if($alias){
            if ( !$aliasStorage->aliasExists($alias, $langcode, $source) ) {
                $path = $aliasStorage->save($source, $alias, $langcode);

            }
        }

        $alias = MYMODULE_get_product_alias($entity, 'pattern_id_2');

        if($alias){
            if ( !$aliasStorage->aliasExists($alias, $langcode, $source) ) {
                $path = $aliasStorage->save($source, $alias, $langcode);

            }
        }
    }

}

and this to create alias with second pattern.

function MYMODULE_get_product_alias($entity, $pattern_key){

    $pattern = \Drupal::entityTypeManager()->getStorage('pathauto_pattern')->load($pattern_key);

    $tokenEntityMapper = \Drupal::service('token.entity_mapper');
    $aliasCleaner = \Drupal::service('pathauto.alias_cleaner');
    $storage = \Drupal::service('path.alias_storage');
    $token = \Drupal::service('token');

    $langcode = $entity->language()->getId();

    // Build token data.
    $data = [
        $tokenEntityMapper->getTokenTypeForEntityType($entity->getEntityTypeId()) => $entity,
    ];

    // Replace any tokens in the pattern.
    // Uses callback option to clean replacements. No sanitization.
    // Pass empty BubbleableMetadata object to explicitly ignore cacheablity,
    // as the result is never rendered.
    $alias = $token->replace($pattern->getPattern(), $data, array(
        'clear' => TRUE,
        'callback' => array($aliasCleaner, 'cleanTokenValues'),
        'langcode' => $langcode,
        'pathauto' => TRUE,
    ), new BubbleableMetadata());

    // Check if the token replacement has not actually replaced any values. If
    // that is the case, then stop because we should not generate an alias.
    // @see token_scan()
    $pattern_tokens_removed = preg_replace('/\[[^\s\]:]*:[^\s\]]*\]/', '', $pattern->getPattern());

    if ($alias === $pattern_tokens_removed) {
        return NULL;
    }

    return $aliasCleaner->cleanAlias($alias);
}

Without disable patterns, the URL created programmatically was overriden by pathauto.

Your Answer

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

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