Skip to content

Commit

Permalink
Correctly generate alias for language records
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Feb 12, 2024
1 parent caa7aad commit 8106a7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions contao/dca/tl_news_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
'inputType' => 'text',
'eval' => [
'rgxp' => 'alias',
'unique' => true,
'spaceToUnderscore' => true,
'doNotCopy'=>true,
'alwaysSave' => true,
'maxlength' => 128,
'tl_class' => 'w50',
],
Expand Down
20 changes: 12 additions & 8 deletions src/EventListener/DataContainer/NewsCategoryAliasListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public function validateAlias(string $value, DataContainer $dc): string
#[AsCallback('tl_news_category', 'config.onbeforesubmit')]
public function generateAlias(array $values, DataContainer $dc): array
{
if (!isset($values['alias']) || '' !== $values['alias']) {
return $values;
}

$currentRecord = $dc->getCurrentRecord();
$title = $values['frontendTitle'] ?? $currentRecord['frontendTitle'] ?: ($values['title'] ?? $currentRecord['title']);

if ('' !== ($values['alias'] ?? $currentRecord['alias'] ?? '')) {
return $values;
}

$slugOptions = [];

if (!empty($validChars = Config::get('news_categorySlugSetting'))) {
Expand All @@ -70,12 +70,16 @@ public function generateAlias(array $values, DataContainer $dc): array

private function aliasExists(string $value, DataContainer $dc): bool
{
$query = "SELECT id FROM {$dc->table} WHERE alias=? AND id!=?";
$params = [$value, $dc->id];
$query = "SELECT id FROM {$dc->table} WHERE alias=?";
$params = [$value];

if ($dc instanceof Driver) {
$query .= " AND {$dc->getLanguageColumn()}=?";
if ($dc instanceof Driver && '' !== $dc->getCurrentLanguage()) {
$query .= " AND pid!=? AND {$dc->getLanguageColumn()}=?";
$params[] = $dc->id;
$params[] = $dc->getCurrentLanguage();
} else {
$query .= ' AND id!=?';
$params[] = $dc->id;
}

return false !== $this->db->fetchOne($query, $params);
Expand Down

0 comments on commit 8106a7b

Please sign in to comment.