Skip to content

Instantly share code, notes, and snippets.

@58bits
Created March 23, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 58bits/81125d4dcbc646c4a1f8 to your computer and use it in GitHub Desktop.
Save 58bits/81125d4dcbc646c4a1f8 to your computer and use it in GitHub Desktop.
<?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