Skip to content

Commit 7f5d737

Browse files
author
Samu Juvonen
committed
Tweaks
1 parent f1909f8 commit 7f5d737

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

autoslug.module

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ function autoslug_entity_insert(EntityInterface $entity) {
2424
function autoslug_entity_update(EntityInterface $entity) {
2525
if (autoslug_is_entity_published($entity)) {
2626
try {
27-
$aliases = Drupal::service('autoslug.alias_generator');
28-
if (!$aliases->entityAliasExists($entity)) {
29-
$aliases->createAlias($entity);
30-
}
27+
Drupal::service('autoslug.alias_generator')->createAllAliases($entity);
3128
} catch (RuntimeException $e) {
3229
// pass
3330
}

src/AliasGenerator.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,29 @@ public function entityAliasExists(EntityInterface $entity) {
3030
return $this->fetchExistingAlias($entity) != NULL;
3131
}
3232

33-
public function createAlias(EntityInterface $entity) {
34-
foreach ($this->sluggers as $slugger) {
35-
if ($slugger->applies($entity)) {
36-
$langcode = $entity->language()->getId();
37-
$alias = $slugger->build($entity);
38-
$alias = $this->ensureAliasUnique($alias, $langcode);
39-
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
40-
$this->aliasStorage->save($cache_key, $alias, $langcode);
41-
return TRUE;
33+
public function createAlias(EntityInterface $entity, $force = FALSE) {
34+
if (!$this->entityAliasExists($entity) || $force) {
35+
foreach ($this->sluggers as $slugger) {
36+
if ($slugger->applies($entity)) {
37+
$langcode = $entity->language()->getId();
38+
$alias = $slugger->build($entity);
39+
$alias = $this->ensureAliasUnique($alias, $langcode);
40+
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
41+
$this->aliasStorage->save($cache_key, $alias, $langcode);
42+
return TRUE;
43+
}
4244
}
4345
}
4446

4547
return FALSE;
4648
}
4749

50+
public function createAllAliases(EntityInterface $entity) {
51+
foreach ($entity->getTranslationLanguages() as $language) {
52+
$this->createAlias($entity->getTranslation($language->getId()));
53+
}
54+
}
55+
4856
public function ensureAliasUnique($base, $langcode) {
4957
$alias = $base;
5058
for ($i = 1; $this->aliasStorage->lookupPathSource($alias, $langcode); $i++) {

src/Controller/SlugController.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ public function aliases($type) {
4949
});
5050

5151
foreach ($iterator as $entity) {
52-
foreach ($entity->getTranslationLanguages() as $language) {
53-
$translated = $entity->getTranslation($language->getId());
54-
$this->aliases->createAlias($translated);
55-
}
52+
$this->aliases->createAllAliases($entity);
5653
}
5754

5855
exit('finished ' . $type);

0 commit comments

Comments
 (0)