Skip to content
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

Use contao.slug to generate tag alias #36

Open
wants to merge 2 commits 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
2 changes: 2 additions & 0 deletions docs/02-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ codefog_tags:
my_manager:
source: 'tl_table.tags' # in format <table>.<field>, 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
```

Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/CodefogTagsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion src/EventListener/DataContainer/TagListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ public function onAliasSaveCallback(string $value, DataContainer $dc): string
// Generate alias if there is none
if (!$value) {
$autoAlias = true;
$value = StringUtil::generateAlias($dc->activeRecord->name);
$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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System::getContainer() should not be used. Use Depdendency Injection instead.

}

$existingAliases = $this->db->fetchOne("SELECT COUNT(*) FROM {$dc->table} WHERE alias=? AND source=?", [$value, $dc->activeRecord->source]);
Expand Down
38 changes: 36 additions & 2 deletions src/Manager/DefaultManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Codefog\TagsBundle\Tag;
use Contao\DataContainer;
use Contao\StringUtil;
use Contao\System;

class DefaultManager implements ManagerInterface, DcaAwareInterface, InsertTagsAwareInterface
{
Expand All @@ -33,6 +34,16 @@ class DefaultManager implements ManagerInterface, DcaAwareInterface, InsertTagsA
*/
protected $sources;

/**
* @var string
*/
protected $locale;

/**
* @var string
*/
protected $validChars;

/**
* @var TagFinder
*/
Expand All @@ -46,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;
}

/**
Expand Down Expand Up @@ -272,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.
*/
Expand Down Expand Up @@ -299,7 +329,11 @@ protected function getSource(string $source = null): string
*/
protected function generateAlias(TagModel $model, string $source = null): void
{
$alias = StringUtil::generateAlias($model->name);
$aliasOptions = [
'locale' => $this->locale,
'validChars' => $this->validChars
];
$alias = System::getContainer()->get('contao.slug')->generate($model->name, $aliasOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System::getContainer() should not be used. Use Depdendency Injection instead.


// Add ID to alias if it already exists
if (null !== ($existingTag = $this->tagFinder->findSingle($this->createTagCriteria($source)->setAlias($alias)))) {
Expand Down
10 changes: 10 additions & 0 deletions src/Manager/ManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}