Skip to content

Drupal 10 Upgrade #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion autoslug.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ type: module
package: Kirjastot.fi
description: Generate SEO URLs for content automaticly
core: 8.x
core_version_requirement: ">=8"
configure: entity.autoslug_rule.collection
dependencies: { }
dependencies:
- drupal:path
2 changes: 1 addition & 1 deletion autoslug.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
arguments: ['@config.factory']
autoslug.alias_generator:
class: Drupal\autoslug\AliasGenerator
arguments: ['@path.alias_storage']
arguments: ['@entity_type.manager', '@path_alias.repository']
tags:
- { name: service_collector, tag: autoslug_slugger, call: addSlugger }
autoslug.slugger.default:
Expand Down
24 changes: 16 additions & 8 deletions src/AliasGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

use SplPriorityQueue;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Path\AliasStorageInterface;
use Drupal\path_alias\AliasRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

class AliasGenerator {
protected $aliasStorage;
protected $aliasRepository;
protected $sluggers;

public function __construct(AliasStorageInterface $alias_storage) {
$this->aliasStorage = $alias_storage;
public function __construct(EntityTypeManagerInterface $entity_type_manager, AliasRepositoryInterface $alias_repository) {
$this->aliasStorage = $entity_type_manager->getStorage('path_alias');
$this->aliasRepository = $alias_repository;
$this->sluggers = new SplPriorityQueue;
}

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

public function fetchExistingAlias(EntityInterface $entity) {
$langcode = $entity->language()->getId();
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
$match = $this->aliasStorage->lookupPathAlias($cache_key, $langcode);
$cache_key = '/' . $entity->toUrl()->getInternalPath();
$match = $this->aliasRepository->lookupBySystemPath($cache_key, $langcode);
return $match;
}

Expand All @@ -37,8 +40,13 @@ public function createAlias(EntityInterface $entity, $force = FALSE) {
$langcode = $entity->language()->getId();
$alias = $slugger->build($entity);
$alias = $this->ensureAliasUnique($alias, $langcode);
$cache_key = '/' . $entity->urlInfo()->getInternalPath();
$this->aliasStorage->save($cache_key, $alias, $langcode);
$cache_key = '/' . $entity->toUrl()->getInternalPath();
$alias = $this->aliasStorage->create([
'path' => $cache_key,
'alias' => $alias,
'langcode' => $langcode
]);
$alias->save();
return TRUE;
}
}
Expand All @@ -55,7 +63,7 @@ public function createAllAliases(EntityInterface $entity) {

public function ensureAliasUnique($base, $langcode) {
$alias = $base;
for ($i = 1; $this->aliasStorage->lookupPathSource($alias, $langcode); $i++) {
for ($i = 1; $this->aliasRepository->lookupByAlias($alias, $langcode); $i++) {
$alias = implode('-', [$base, $i]);
}
return $alias;
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/SlugController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(EntityTypeManagerInterface $entity_manager, RequestS
}

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

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

$query->accessCheck(TRUE); // Added this line to specify access check

if ($result = $query->execute()) {
return $storage->loadMultiple($result);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Form/RuleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$form_state->setRedirect('entity.autoslug_rule.collection');

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

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

foreach ($types as $type) {
if ($type->isSubClassOf(ContentEntityInterface::class)) {
if ($type->entityClassImplements(ContentEntityInterface::class)) {
$options[$type->id()] = (string)$type->getLabel();
}
}
Expand All @@ -94,7 +94,7 @@ protected function getBundleOptions() {
$options = [];

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

Expand Down
18 changes: 9 additions & 9 deletions src/RuleListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
use Symfony\Component\DependencyInjection\ContainerInterface;

class RuleListBuilder extends EntityListBuilder {
protected $entityManager;
protected $entityTypeManager;

public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$container->get('entity.manager'),
$container->get('entity_type.manager'),
$entity_type
);
}

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

public function buildHeader() {
Expand All @@ -48,15 +48,15 @@ public function buildRow(EntityInterface $entity) {
}

protected function entityTypeLabel($type_id) {
return $this->entityManager->getDefinition($type_id)->getLabel();
return $this->entityTypeManager->getDefinition($type_id)->getLabel();

// $label = $this->entityManager->getDefinition($type_id)->getLabel();
// return new FormattableMarkup('@label (@type)', ['@label' => $label, '@type' => $type_id]);
}

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

// $bundle_type = $this->entityManager->getDefinition($type_id)->getBundleEntityType();
Expand Down
4 changes: 2 additions & 2 deletions src/Slugger/DefaultSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function extractTokens(EntityInterface $entity, $pattern, $max_words =
$key = $match[2];

if (strpos($key, ':')) {
list($child, $key) = explode(':', $key);
[$child, $key] = explode(':', $key);
$value = $entity->get($child)->entity->get($key)->value;
} else {
$value = $entity->get($key)->value;
Expand All @@ -80,7 +80,7 @@ protected function extractTokens(EntityInterface $entity, $pattern, $max_words =
* Replace tokens from the URL pattern.
*/
protected function processPattern($pattern, array $tokens) {
$keys = array_map(function($t) { return sprintf('{%s}', $t); }, array_keys($tokens));
$keys = array_map(fn($t) => sprintf('{%s}', $t), array_keys($tokens));
$alias = str_replace($keys, array_values($tokens), $pattern);
$alias = Unicode::truncate($alias, 128, TRUE);
return $alias;
Expand Down
2 changes: 1 addition & 1 deletion src/Slugger/DeprecatedConfigBasedSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function aliasByPattern(EntityInterface $entity, $pattern) {
$replace_match = function(array $matches) use ($entity) {
$prop = $matches[1];
if (strpos($prop, ':')) {
list($child, $prop) = explode(':', $prop);
[$child, $prop] = explode(':', $prop);
$value = $entity->get($child)->entity->get($prop)->value;
} else {
$value = $entity->get($prop)->value;
Expand Down
6 changes: 3 additions & 3 deletions src/TimeLimitedIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public function next() {
}

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

public function valid() {
if ($this->i >= count($this->data)) {
if ($this->i >= (is_countable($this->data) ? count($this->data) : 0)) {
$this->fetchMore();
}
return $this->i < count($this->data);
return $this->i < (is_countable($this->data) ? count($this->data) : 0);
}

public function rewind() {
Expand Down