diff --git a/contao/dca/tl_news_category.php b/contao/dca/tl_news_category.php index 635fe34..3bbfd05 100644 --- a/contao/dca/tl_news_category.php +++ b/contao/dca/tl_news_category.php @@ -101,8 +101,8 @@ 'inputType' => 'text', 'eval' => [ 'rgxp' => 'alias', - 'unique' => true, - 'spaceToUnderscore' => true, + 'doNotCopy'=>true, + 'alwaysSave' => true, 'maxlength' => 128, 'tl_class' => 'w50', ], diff --git a/src/EventListener/DataContainer/NewsCategoryAliasListener.php b/src/EventListener/DataContainer/NewsCategoryAliasListener.php index 7130dda..b677478 100644 --- a/src/EventListener/DataContainer/NewsCategoryAliasListener.php +++ b/src/EventListener/DataContainer/NewsCategoryAliasListener.php @@ -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'))) { @@ -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(); + } }