diff --git a/autoslug.info.yml b/autoslug.info.yml index c818ed8..555a9ff 100644 --- a/autoslug.info.yml +++ b/autoslug.info.yml @@ -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 diff --git a/autoslug.services.yml b/autoslug.services.yml index 0540c8f..98f0c86 100644 --- a/autoslug.services.yml +++ b/autoslug.services.yml @@ -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: diff --git a/src/AliasGenerator.php b/src/AliasGenerator.php index 940fd6d..121a9e8 100644 --- a/src/AliasGenerator.php +++ b/src/AliasGenerator.php @@ -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; } @@ -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; } @@ -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; } } @@ -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; diff --git a/src/Controller/SlugController.php b/src/Controller/SlugController.php index 9e38655..9450dd7 100644 --- a/src/Controller/SlugController.php +++ b/src/Controller/SlugController.php @@ -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) { @@ -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); } diff --git a/src/Form/RuleForm.php b/src/Form/RuleForm.php index 9e148d8..fa83655 100644 --- a/src/Form/RuleForm.php +++ b/src/Form/RuleForm.php @@ -71,7 +71,7 @@ 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() { @@ -79,7 +79,7 @@ protected function getEntityTypeOptions() { $options = []; foreach ($types as $type) { - if ($type->isSubClassOf(ContentEntityInterface::class)) { + if ($type->entityClassImplements(ContentEntityInterface::class)) { $options[$type->id()] = (string)$type->getLabel(); } } @@ -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(); diff --git a/src/RuleListBuilder.php b/src/RuleListBuilder.php index cd8594f..6fc1d7f 100644 --- a/src/RuleListBuilder.php +++ b/src/RuleListBuilder.php @@ -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() { @@ -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(); diff --git a/src/Slugger/DefaultSlugger.php b/src/Slugger/DefaultSlugger.php index a248f8d..4bf3ff4 100644 --- a/src/Slugger/DefaultSlugger.php +++ b/src/Slugger/DefaultSlugger.php @@ -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; @@ -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; diff --git a/src/Slugger/DeprecatedConfigBasedSlugger.php b/src/Slugger/DeprecatedConfigBasedSlugger.php index 6e96582..7020103 100644 --- a/src/Slugger/DeprecatedConfigBasedSlugger.php +++ b/src/Slugger/DeprecatedConfigBasedSlugger.php @@ -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; diff --git a/src/TimeLimitedIterator.php b/src/TimeLimitedIterator.php index 48e8e34..ff9c2f7 100644 --- a/src/TimeLimitedIterator.php +++ b/src/TimeLimitedIterator.php @@ -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() {