-
-
Save 58bits/81125d4dcbc646c4a1f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_entity_insert(). | |
* https://www.drupal.org/node/2689459 | |
* @param EntityInterface $entity | |
*/ | |
function hook_entity_insert(EntityInterface $entity) { | |
if ($entity instanceof FieldableEntityInterface && $entity->getFieldDefinition('path') && !$entity->getFieldDefinition('path')->isTranslatable()) { | |
$path = $entity->get('path')->first(); | |
$source = '/node/' . $entity->id(); //Source in $path will be null until the insert is complete. | |
if(isset($path)) { | |
$path = $path->getValue(); | |
if(!empty($path['alias'])) { | |
$alias_storage = \Drupal::service('path.alias_storage'); | |
$language_manager = \Drupal::service('language_manager'); | |
foreach ($language_manager->getLanguages() as $langcode => $language) { | |
if ($path['langcode'] != $langcode) { | |
if ($translation_path = $alias_storage->load([ | |
'source' => $source, | |
'langcode' => $langcode | |
]) | |
) { | |
$alias_storage->save($source, $path['alias'], $langcode, $translation_path['pid']); | |
} | |
else { | |
$alias_storage->save($source, $path['alias'], $langcode); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Implements hook_entity_update(). | |
* https://www.drupal.org/node/2689459 | |
* @param EntityInterface $entity | |
*/ | |
function hook_entity_update(EntityInterface $entity) { | |
if ($entity instanceof FieldableEntityInterface && $entity->getFieldDefinition('path') && !$entity->getFieldDefinition('path')->isTranslatable()) { | |
$alias = ''; | |
$path = $entity->get('path')->first(); | |
if(isset($path)) { | |
$path = $path->getValue(); | |
if(isset($path) && !empty($path['alias'])) { | |
$alias = $path['alias']; | |
} else { | |
$alias = $entity->toUrl()->toString(); | |
} | |
} else { | |
$alias = $entity->toUrl()->toString(); | |
} | |
$source = '/node/' . $entity->id(); | |
$alias_storage = \Drupal::service('path.alias_storage'); | |
$language_manager = \Drupal::service('language_manager'); | |
foreach ($language_manager->getLanguages() as $langcode => $language) { | |
if ($translation_path = $alias_storage->load([ | |
'source' => $source, | |
'langcode' => $langcode | |
]) | |
) { | |
$alias_storage->save($source, $alias, $langcode, $translation_path['pid']); | |
} | |
else { | |
$alias_storage->save($source, $alias, $langcode); | |
} | |
} | |
} | |
} | |
/** | |
* Implements hook_entity_delete(). | |
* @param EntityInterface $entity | |
*/ | |
function hook_entity_delete(EntityInterface $entity) { | |
if ($entity instanceof FieldableEntityInterface && $entity->getFieldDefinition('path') && !$entity->getFieldDefinition('path')->isTranslatable()) { | |
$alias_storage = \Drupal::service('path.alias_storage'); | |
$language_manager = \Drupal::service('language_manager'); | |
$source = '/node/' . $entity->id(); | |
foreach ($language_manager->getLanguages() as $langcode => $language) { | |
if ($translation_path = $alias_storage->load(['source' => $source, 'langcode' => $langcode])) { | |
$alias_storage->delete(['source' => $source, 'langcode' => $langcode]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment