Skip to content

Commit ac910da

Browse files
Drupal 10 deprecation fixes
- Updated *.info.yml to be compatible with Drupal 10. - Added 'drupal:path' as a dependency, since it heavily relied on this module. - Replaced deprecated entityManager with entityTypeManager. - Refactor AliasGenerator constructor to use EntityTypeManagerInterface and AliasRepositoryInterface due to aliasStorage deprecation. - Update fetchExistingAlias to use lookupBySystemPath from AliasRepositoryInterface due to aliasStorage deprecation. - Replace deprecated lookupPathSource with lookupByAlias in alias uniqueness check. - Replaced deprecated urlInfo() with toUrl() for generating URLs. - Applied code style improvements using Rector for enhanced readability and consistency. - Updated entity queries to explicitly manage access checks. - Replaced deprecated drupal_set_message() with \Drupal::messenger() ->addStatus(). - Replace deprecated isSubClassOf with entityClassImplements. - Added is_countable() checks before count(); lack of it results in a warning starting from PHP 7.2.
1 parent 7f5d737 commit ac910da

9 files changed

+41
-29
lines changed

autoslug.info.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ type: module
33
package: Kirjastot.fi
44
description: Generate SEO URLs for content automaticly
55
core: 8.x
6+
core_version_requirement: ">=8"
67
configure: entity.autoslug_rule.collection
7-
dependencies: { }
8+
dependencies:
9+
- drupal:path

autoslug.services.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
arguments: ['@config.factory']
55
autoslug.alias_generator:
66
class: Drupal\autoslug\AliasGenerator
7-
arguments: ['@path.alias_storage']
7+
arguments: ['@entity_type.manager', '@path_alias.repository']
88
tags:
99
- { name: service_collector, tag: autoslug_slugger, call: addSlugger }
1010
autoslug.slugger.default:

src/AliasGenerator.php

+16-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
use SplPriorityQueue;
66
use Drupal\Core\Entity\EntityInterface;
7-
use Drupal\Core\Path\AliasStorageInterface;
7+
use Drupal\path_alias\AliasRepositoryInterface;
8+
use Drupal\Core\Entity\EntityTypeManagerInterface;
89

910
class AliasGenerator {
1011
protected $aliasStorage;
12+
protected $aliasRepository;
1113
protected $sluggers;
1214

13-
public function __construct(AliasStorageInterface $alias_storage) {
14-
$this->aliasStorage = $alias_storage;
15+
public function __construct(EntityTypeManagerInterface $entity_type_manager, AliasRepositoryInterface $alias_repository) {
16+
$this->aliasStorage = $entity_type_manager->getStorage('path_alias');
17+
$this->aliasRepository = $alias_repository;
1518
$this->sluggers = new SplPriorityQueue;
1619
}
1720

@@ -21,8 +24,8 @@ public function addSlugger(SluggerInterface $slugger, $priority = 0) {
2124

2225
public function fetchExistingAlias(EntityInterface $entity) {
2326
$langcode = $entity->language()->getId();
24-
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
25-
$match = $this->aliasStorage->lookupPathAlias($cache_key, $langcode);
27+
$cache_key = '/' . $entity->toUrl()->getInternalPath();
28+
$match = $this->aliasRepository->lookupBySystemPath($cache_key, $langcode);
2629
return $match;
2730
}
2831

@@ -37,8 +40,13 @@ public function createAlias(EntityInterface $entity, $force = FALSE) {
3740
$langcode = $entity->language()->getId();
3841
$alias = $slugger->build($entity);
3942
$alias = $this->ensureAliasUnique($alias, $langcode);
40-
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
41-
$this->aliasStorage->save($cache_key, $alias, $langcode);
43+
$cache_key = '/' . $entity->toUrl()->getInternalPath();
44+
$alias = $this->aliasStorage->create([
45+
'path' => $cache_key,
46+
'alias' => $alias,
47+
'langcode' => $langcode
48+
]);
49+
$alias->save();
4250
return TRUE;
4351
}
4452
}
@@ -55,7 +63,7 @@ public function createAllAliases(EntityInterface $entity) {
5563

5664
public function ensureAliasUnique($base, $langcode) {
5765
$alias = $base;
58-
for ($i = 1; $this->aliasStorage->lookupPathSource($alias, $langcode); $i++) {
66+
for ($i = 1; $this->aliasRepository->lookupByAlias($alias, $langcode); $i++) {
5967
$alias = implode('-', [$base, $i]);
6068
}
6169
return $alias;

src/Controller/SlugController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(EntityTypeManagerInterface $entity_manager, RequestS
3131
}
3232

3333
public function aliases($type) {
34-
list($entity_type, $bundle) = explode('.', $type . '.');
34+
[$entity_type, $bundle] = explode('.', $type . '.');
3535
$storage = $this->entityManager->getStorage($entity_type);
3636

3737
$iterator = new TimeLimitedIterator(function($first, $count) use ($storage, $bundle) {
@@ -43,6 +43,8 @@ public function aliases($type) {
4343
$query->condition($storage->getEntityType()->getKey('bundle'), $bundle);
4444
}
4545

46+
$query->accessCheck(TRUE); // Added this line to specify access check
47+
4648
if ($result = $query->execute()) {
4749
return $storage->loadMultiple($result);
4850
}

src/Form/RuleForm.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ public function save(array $form, FormStateInterface $form_state) {
7171
parent::save($form, $form_state);
7272
$form_state->setRedirect('entity.autoslug_rule.collection');
7373

74-
drupal_set_message($this->t('New path alias rule was created.'));
74+
$this->messenger()->addStatus($this->t('New path alias rule was created.'));
7575
}
7676

7777
protected function getEntityTypeOptions() {
7878
$types = $this->entityTypeManager->getDefinitions();
7979
$options = [];
8080

8181
foreach ($types as $type) {
82-
if ($type->isSubClassOf(ContentEntityInterface::class)) {
82+
if ($type->entityClassImplements(ContentEntityInterface::class)) {
8383
$options[$type->id()] = (string)$type->getLabel();
8484
}
8585
}
@@ -94,7 +94,7 @@ protected function getBundleOptions() {
9494
$options = [];
9595

9696
foreach ($types as $type) {
97-
if ($type->isSubClassOf(ContentEntityInterface::class) && $type->getBundleEntityType()) {
97+
if ($type->entityClassImplements(ContentEntityInterface::class) && $type->getBundleEntityType()) {
9898
$bundles = $this->entityTypeManager->getStorage($type->getBundleEntityType())->loadMultiple();
9999
$group = (string)$type->getLabel();
100100

src/RuleListBuilder.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
use Symfony\Component\DependencyInjection\ContainerInterface;
1111

1212
class RuleListBuilder extends EntityListBuilder {
13-
protected $entityManager;
13+
protected $entityTypeManager;
1414

1515
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
1616
return new static(
17-
$container->get('entity.manager'),
17+
$container->get('entity_type.manager'),
1818
$entity_type
1919
);
2020
}
2121

22-
public function __construct(EntityTypeManagerInterface $entity_manager, EntityTypeInterface $entity_type) {
23-
parent::__construct($entity_type, $entity_manager->getStorage($entity_type->id()));
24-
$this->entityManager = $entity_manager;
22+
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeInterface $entity_type) {
23+
parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
24+
$this->entityTypeManager = $entity_type_manager;
2525
}
2626

2727
public function buildHeader() {
@@ -48,15 +48,15 @@ public function buildRow(EntityInterface $entity) {
4848
}
4949

5050
protected function entityTypeLabel($type_id) {
51-
return $this->entityManager->getDefinition($type_id)->getLabel();
52-
51+
return $this->entityTypeManager->getDefinition($type_id)->getLabel();
52+
5353
// $label = $this->entityManager->getDefinition($type_id)->getLabel();
5454
// return new FormattableMarkup('@label (@type)', ['@label' => $label, '@type' => $type_id]);
5555
}
5656

5757
protected function entityBundleLabel($type_id, $bundle_id) {
58-
$bundle_type = $this->entityManager->getDefinition($type_id)->getBundleEntityType();
59-
$label = $this->entityManager->getStorage($bundle_type)->load($bundle_id)->label();
58+
$bundle_type = $this->entityTypeManager->getDefinition($type_id)->getBundleEntityType();
59+
$label = $this->entityTypeManager->getStorage($bundle_type)->load($bundle_id)->label();
6060
return $label;
6161

6262
// $bundle_type = $this->entityManager->getDefinition($type_id)->getBundleEntityType();

src/Slugger/DefaultSlugger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function extractTokens(EntityInterface $entity, $pattern, $max_words =
5858
$key = $match[2];
5959

6060
if (strpos($key, ':')) {
61-
list($child, $key) = explode(':', $key);
61+
[$child, $key] = explode(':', $key);
6262
$value = $entity->get($child)->entity->get($key)->value;
6363
} else {
6464
$value = $entity->get($key)->value;
@@ -80,7 +80,7 @@ protected function extractTokens(EntityInterface $entity, $pattern, $max_words =
8080
* Replace tokens from the URL pattern.
8181
*/
8282
protected function processPattern($pattern, array $tokens) {
83-
$keys = array_map(function($t) { return sprintf('{%s}', $t); }, array_keys($tokens));
83+
$keys = array_map(fn($t) => sprintf('{%s}', $t), array_keys($tokens));
8484
$alias = str_replace($keys, array_values($tokens), $pattern);
8585
$alias = Unicode::truncate($alias, 128, TRUE);
8686
return $alias;

src/Slugger/DeprecatedConfigBasedSlugger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function aliasByPattern(EntityInterface $entity, $pattern) {
4646
$replace_match = function(array $matches) use ($entity) {
4747
$prop = $matches[1];
4848
if (strpos($prop, ':')) {
49-
list($child, $prop) = explode(':', $prop);
49+
[$child, $prop] = explode(':', $prop);
5050
$value = $entity->get($child)->entity->get($prop)->value;
5151
} else {
5252
$value = $entity->get($prop)->value;

src/TimeLimitedIterator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public function next() {
3232
}
3333

3434
public function key() {
35-
return $this->i < count($this->data) ? $this->i : FALSE;
35+
return $this->i < (is_countable($this->data) ? count($this->data) : 0) ? $this->i : FALSE;
3636
}
3737

3838
public function valid() {
39-
if ($this->i >= count($this->data)) {
39+
if ($this->i >= (is_countable($this->data) ? count($this->data) : 0)) {
4040
$this->fetchMore();
4141
}
42-
return $this->i < count($this->data);
42+
return $this->i < (is_countable($this->data) ? count($this->data) : 0);
4343
}
4444

4545
public function rewind() {

0 commit comments

Comments
 (0)