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 47bebe4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 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
35 changes: 28 additions & 7 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']) {
$currentRecord = $this->getCurrentRecord($dc);
$title = $values['frontendTitle'] ?? $currentRecord['frontendTitle'] ?: ($values['title'] ?? $currentRecord['title']);

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

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

$slugOptions = [];

if (!empty($validChars = Config::get('news_categorySlugSetting'))) {
Expand All @@ -70,14 +70,35 @@ 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->getCurrentLanguage()) {
$query .= " AND {$dc->getPidColumn()}!=? AND {$dc->getLanguageColumn()}=?";
} else {
$query .= " AND id!=? AND {$dc->getLanguageColumn()}=?";
}

$params[] = $dc->id;
$params[] = $dc->getCurrentLanguage();
} else {
$query .= ' AND id!=?';
$params[] = $dc->id;
}

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

private function getCurrentRecord(DataContainer $dc): array
{
if ($dc instanceof Driver && '' !== $dc->getCurrentLanguage()) {
return $this->db->fetchAssociative(
"SELECT * FROM {$dc->table} WHERE {$dc->getPidColumn()}=? AND {$dc->getLanguageColumn()}=?",
[$dc->id, $dc->getCurrentLanguage()]
);
}

return $dc->getCurrentRecord();
}
}

0 comments on commit 47bebe4

Please sign in to comment.