Skip to content

Commit

Permalink
Do not show language warning if user cannot change the field
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Nov 26, 2024
1 parent 6617862 commit 615fa19
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions contao/dca/tl_page.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Config.
*/
Expand Down
31 changes: 28 additions & 3 deletions src/EventListener/DataContainer/MissingLanguageIconListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Contao\Backend;
use Contao\BackendUser;
use Contao\Config;
use Contao\CoreBundle\Security\ContaoCorePermissions;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\Date;
use Contao\Input;
use Contao\StringUtil;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Service\ResetInterface;
use Terminal42\ChangeLanguage\Helper\LabelCallback;

Expand All @@ -23,6 +25,8 @@
*/
class MissingLanguageIconListener implements ResetInterface
{
private AuthorizationCheckerInterface $authorizationChecker;

/**
* @var array<string, string>|null
*/
Expand All @@ -42,10 +46,11 @@ class MissingLanguageIconListener implements ResetInterface
*/
private ?array $translationCache = null;

public function __construct(TokenStorageInterface $tokenStorage, Connection $connection)
public function __construct(TokenStorageInterface $tokenStorage, Connection $connection, AuthorizationCheckerInterface $authorizationChecker)
{
$this->tokenStorage = $tokenStorage;
$this->connection = $connection;
$this->authorizationChecker = $authorizationChecker;
}

/**
Expand Down Expand Up @@ -83,7 +88,12 @@ private function onPageLabel(array $args, $previousResult = null): string
$label = $previousResult;
}

if ('root' === $row['type'] || 'folder' === $row['type'] || 'page' !== Input::get('do')) {
if (
'root' === $row['type']
|| 'folder' === $row['type']
|| 'page' !== Input::get('do')
|| !$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, 'tl_page.languageMain')
) {
return $label;
}

Expand Down Expand Up @@ -129,7 +139,10 @@ private function onArticleLabel(array $args, $previousResult = null): string
$label = $previousResult;
}

if (!$row['showTeaser']) {
if (
!$row['showTeaser']
|| !$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, 'tl_article.languageMain')
) {
return $label;
}

Expand All @@ -155,6 +168,10 @@ private function onNewsChildRecords(array $args, $previousResult = null): string
$label = '<div class="tl_content_left">'.$row['headline'].' <span style="color:#999;padding-left:3px">['.Date::parse(Config::get('datimFormat'), $row['date']).']</span></div>';
}

if (!$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, 'tl_news.languageMain')) {
return $label;
}

if (0 === $this->getChildTranslation((int) $row['id'], 'tl_news', 'tl_news_archive', 'master')) {
return preg_replace(
'#</div>#',
Expand All @@ -178,6 +195,10 @@ private function onCalendarEventChildRecords(array $args, $previousResult = null
$row = $args[0];
$label = (string) $previousResult;

if (!$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, 'tl_calendar_events.languageMain')) {
return $label;
}

if (0 === $this->getChildTranslation((int) $row['id'], 'tl_calendar_events', 'tl_calendar', 'master')) {
return preg_replace(
'#</div>#',
Expand All @@ -201,6 +222,10 @@ private function onFaqChildRecords(array $args, $previousResult = null): string
$row = $args[0];
$label = (string) $previousResult;

if (!$this->authorizationChecker->isGranted(ContaoCorePermissions::USER_CAN_EDIT_FIELD_OF_TABLE, 'tl_faq.languageMain')) {
return $label;
}

if (0 === $this->getChildTranslation((int) $row['id'], 'tl_faq', 'tl_faq_category', 'master')) {
return preg_replace(
'#</div>#',
Expand Down

0 comments on commit 615fa19

Please sign in to comment.