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.
$alias
? Please provide your complete code example.