3 namespace Drupal\config_translation\Access;
5 use Drupal\config_translation\ConfigMapperInterface;
6 use Drupal\config_translation\Exception\ConfigMapperLanguageException;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Routing\RouteMatchInterface;
9 use Drupal\Core\Session\AccountInterface;
12 * Checks access for displaying the translation add, edit, and delete forms.
14 class ConfigTranslationFormAccess extends ConfigTranslationOverviewAccess {
17 * Checks access to the overview based on permissions and translatability.
19 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
20 * The route_match to check against.
21 * @param \Drupal\Core\Session\AccountInterface $account
22 * The account to check access for.
23 * @param string $langcode
24 * The language code of the target language.
26 * @return \Drupal\Core\Access\AccessResultInterface
29 public function access(RouteMatchInterface $route_match, AccountInterface $account, $langcode = NULL) {
30 $mapper = $this->getMapperFromRouteMatch($route_match);
33 $source_langcode = $mapper->getLangcode();
34 $source_language = $this->languageManager->getLanguage($source_langcode);
36 $target_language = $this->languageManager->getLanguage($langcode);
38 return $this->doCheckAccess($account, $mapper, $source_language, $target_language);
40 catch (ConfigMapperLanguageException $exception) {
41 return AccessResult::forbidden();
46 * Checks access given an account, configuration mapper, and source language.
48 * In addition to the checks performed by
49 * ConfigTranslationOverviewAccess::doCheckAccess() this makes sure the target
50 * language is not locked and the target language is not the source language.
52 * Although technically configuration can be overlaid with translations in the
53 * same language, that is logically not a good idea.
55 * @param \Drupal\Core\Session\AccountInterface $account
56 * The account to check access for.
57 * @param \Drupal\config_translation\ConfigMapperInterface $mapper
58 * The configuration mapper to check access for.
59 * @param \Drupal\Core\Language\LanguageInterface|null $source_language
60 * The source language to check for, if any.
61 * @param \Drupal\Core\Language\LanguageInterface|null $target_language
62 * The target language to check for, if any.
64 * @return \Drupal\Core\Access\AccessResultInterface
65 * The result of the access check.
67 * @see \Drupal\config_translation\Access\ConfigTranslationOverviewAccess::doCheckAccess()
69 protected function doCheckAccess(AccountInterface $account, ConfigMapperInterface $mapper, $source_language = NULL, $target_language = NULL) {
70 $base_access_result = parent::doCheckAccess($account, $mapper, $source_language);
74 !$target_language->isLocked() &&
75 (!$source_language || ($target_language->getId() !== $source_language->getId()));
77 return $base_access_result->andIf(AccessResult::allowedIf($access));