From 4970daf4778194f0ff9eb007f94d484b3cc8c208 Mon Sep 17 00:00:00 2001 From: Arndt von Lucadou Date: Fri, 16 Sep 2022 15:24:21 +0200 Subject: [PATCH 1/2] Use contao.slug to generate tag alias instead of StringUtil::generateAlias --- src/EventListener/DataContainer/TagListener.php | 3 ++- src/Manager/DefaultManager.php | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/EventListener/DataContainer/TagListener.php b/src/EventListener/DataContainer/TagListener.php index ac08aee..69a64e2 100644 --- a/src/EventListener/DataContainer/TagListener.php +++ b/src/EventListener/DataContainer/TagListener.php @@ -271,7 +271,8 @@ public function onAliasSaveCallback(string $value, DataContainer $dc): string // Generate alias if there is none if (!$value) { $autoAlias = true; - $value = StringUtil::generateAlias($dc->activeRecord->name); + $aliasOptions = \Contao\PageModel::findBytype('root')->id ?? []; + $value = System::getContainer()->get('contao.slug')->generate($dc->activeRecord->name, $aliasOptions); } $existingAliases = $this->db->fetchOne("SELECT COUNT(*) FROM {$dc->table} WHERE alias=? AND source=?", [$value, $dc->activeRecord->source]); diff --git a/src/Manager/DefaultManager.php b/src/Manager/DefaultManager.php index a4741c8..898ad0d 100644 --- a/src/Manager/DefaultManager.php +++ b/src/Manager/DefaultManager.php @@ -20,6 +20,7 @@ use Codefog\TagsBundle\Tag; use Contao\DataContainer; use Contao\StringUtil; +use Contao\System; class DefaultManager implements ManagerInterface, DcaAwareInterface, InsertTagsAwareInterface { @@ -299,7 +300,8 @@ protected function getSource(string $source = null): string */ protected function generateAlias(TagModel $model, string $source = null): void { - $alias = StringUtil::generateAlias($model->name); + $aliasOptions = \Contao\PageModel::findBytype('root')->id ?? []; + $alias = System::getContainer()->get('contao.slug')->generate($model->name, $aliasOptions); // Add ID to alias if it already exists if (null !== ($existingTag = $this->tagFinder->findSingle($this->createTagCriteria($source)->setAlias($alias)))) { From d73dc4c5e288930bf922bf52b6ad0dd97b93bb3c Mon Sep 17 00:00:00 2001 From: Arndt von Lucadou Date: Wed, 21 Sep 2022 11:00:11 +0200 Subject: [PATCH 2/2] Add alias options to tag manager configuration --- docs/02-config.md | 2 ++ .../CodefogTagsExtension.php | 2 +- src/DependencyInjection/Configuration.php | 2 ++ .../DataContainer/TagListener.php | 6 +++- src/Manager/DefaultManager.php | 36 +++++++++++++++++-- src/Manager/ManagerInterface.php | 10 ++++++ 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/docs/02-config.md b/docs/02-config.md index ae93ef4..eb2b0c6 100644 --- a/docs/02-config.md +++ b/docs/02-config.md @@ -22,6 +22,8 @@ codefog_tags: my_manager: source: 'tl_table.tags' # in format ., or an array of such service: '' # optional, manager service to use (defaults to "codefog_tags.default_manager") + locale: '' # optional, locale to use for alias generation (defaults to "en") + validChars: '' # optional, validChars to use for alias generation (defaults to "0-9a-z") alias: '' # optional, alias of the newly created service ``` diff --git a/src/DependencyInjection/CodefogTagsExtension.php b/src/DependencyInjection/CodefogTagsExtension.php index d649e9b..0e1c178 100755 --- a/src/DependencyInjection/CodefogTagsExtension.php +++ b/src/DependencyInjection/CodefogTagsExtension.php @@ -44,7 +44,7 @@ private function createManager(string $name, array $config, ContainerBuilder $co $container ->setDefinition($id, new ChildDefinition($config['service'])) - ->setArguments([$name, $config['source']]) + ->setArguments([$name, $config['source'], $config['locale'], $config['validChars']]) ->addTag(ManagerPass::TAG_NAME, ['alias' => $name]) ->setPublic(true) ; diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 0ae3355..53419cc 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -49,6 +49,8 @@ static function (string $value): array { ->end() ->end() ->scalarNode('service')->defaultValue('codefog_tags.default_manager')->end() + ->scalarNode('locale')->defaultValue('en')->end() + ->scalarNode('validChars')->defaultValue('0-9a-z')->end() ->scalarNode('alias')->defaultNull()->end() ->end() ->end() diff --git a/src/EventListener/DataContainer/TagListener.php b/src/EventListener/DataContainer/TagListener.php index 69a64e2..f19a0f7 100644 --- a/src/EventListener/DataContainer/TagListener.php +++ b/src/EventListener/DataContainer/TagListener.php @@ -271,7 +271,11 @@ public function onAliasSaveCallback(string $value, DataContainer $dc): string // Generate alias if there is none if (!$value) { $autoAlias = true; - $aliasOptions = \Contao\PageModel::findBytype('root')->id ?? []; + $manager = $this->registry->get($dc->activeRecord->source); + $aliasOptions = [ + 'locale' => $manager->getLocale(), + 'validChars' => $manager->getValidChars() + ]; $value = System::getContainer()->get('contao.slug')->generate($dc->activeRecord->name, $aliasOptions); } diff --git a/src/Manager/DefaultManager.php b/src/Manager/DefaultManager.php index 898ad0d..a615286 100644 --- a/src/Manager/DefaultManager.php +++ b/src/Manager/DefaultManager.php @@ -34,6 +34,16 @@ class DefaultManager implements ManagerInterface, DcaAwareInterface, InsertTagsA */ protected $sources; + /** + * @var string + */ + protected $locale; + + /** + * @var string + */ + protected $validChars; + /** * @var TagFinder */ @@ -47,10 +57,12 @@ class DefaultManager implements ManagerInterface, DcaAwareInterface, InsertTagsA /** * DefaultManager constructor. */ - public function __construct(string $name, array $sources) + public function __construct(string $name, array $sources, string $locale, string $validChars) { $this->name = $name; $this->sources = $sources; + $this->locale = $locale; + $this->validChars = $validChars; } /** @@ -273,6 +285,23 @@ public function getSourceFinder(): SourceFinder return $this->sourceFinder; } + /** + * Get locale. + */ + public function getLocale(): string + { + return $this->locale; + } + + /** + * Get validChars. + */ + public function getValidChars(): string + { + return $this->validChars; + } + + /** * Create the source criteria. */ @@ -300,7 +329,10 @@ protected function getSource(string $source = null): string */ protected function generateAlias(TagModel $model, string $source = null): void { - $aliasOptions = \Contao\PageModel::findBytype('root')->id ?? []; + $aliasOptions = [ + 'locale' => $this->locale, + 'validChars' => $this->validChars + ]; $alias = System::getContainer()->get('contao.slug')->generate($model->name, $aliasOptions); // Add ID to alias if it already exists diff --git a/src/Manager/ManagerInterface.php b/src/Manager/ManagerInterface.php index 412406d..f2fb258 100644 --- a/src/Manager/ManagerInterface.php +++ b/src/Manager/ManagerInterface.php @@ -23,4 +23,14 @@ public function getAllTags(string $source = null): array; * Get tags optionally filtered by values. */ public function getFilteredTags(array $values, string $source = null): array; + + /** + * Get locale. + */ + public function getLocale(): string; + + /** + * Get validChars. + */ + public function getValidChars(): string; }