class LanguageNegotiationUserAdmin

Same name in other branches
  1. 8.9.x core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin
  2. 10 core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin
  3. 11.x core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php \Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin

Identifies admin language from the user preferences.

Plugin annotation


@LanguageNegotiation(
  id = Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin::METHOD_ID,
  types = {Drupal\Core\Language\LanguageInterface::TYPE_INTERFACE},
  weight = -10,
  name = @Translation("Account administration pages"),
  description = @Translation("Account administration pages language setting.")
)

Hierarchy

Expanded class hierarchy of LanguageNegotiationUserAdmin

2 files declare their use of LanguageNegotiationUserAdmin
AccountForm.php in core/modules/user/src/AccountForm.php
LanguageUILanguageNegotiationTest.php in core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php

File

core/modules/user/src/Plugin/LanguageNegotiation/LanguageNegotiationUserAdmin.php, line 28

Namespace

Drupal\user\Plugin\LanguageNegotiation
View source
class LanguageNegotiationUserAdmin extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {
    
    /**
     * The language negotiation method id.
     */
    const METHOD_ID = 'language-user-admin';
    
    /**
     * The admin context.
     *
     * @var \Drupal\Core\Routing\AdminContext
     */
    protected $adminContext;
    
    /**
     * The router.
     *
     * This is only used when called from an event subscriber, before the request
     * has been populated with the route info.
     *
     * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
     */
    protected $router;
    
    /**
     * The path processor manager.
     *
     * @var \Drupal\Core\PathProcessor\PathProcessorManager
     */
    protected $pathProcessorManager;
    
    /**
     * The stacked route match.
     *
     * @var \Drupal\Core\Routing\StackedRouteMatchInterface
     */
    protected $stackedRouteMatch;
    
    /**
     * Constructs a new LanguageNegotiationUserAdmin instance.
     *
     * @param \Drupal\Core\Routing\AdminContext $admin_context
     *   The admin context.
     * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $router
     *   The router.
     * @param \Drupal\Core\PathProcessor\PathProcessorManager $path_processor_manager
     *   The path processor manager.
     * @param \Drupal\Core\Routing\StackedRouteMatchInterface $stacked_route_match
     *   The stacked route match.
     */
    public function __construct(AdminContext $admin_context, UrlMatcherInterface $router, PathProcessorManager $path_processor_manager, StackedRouteMatchInterface $stacked_route_match) {
        $this->adminContext = $admin_context;
        $this->router = $router;
        $this->pathProcessorManager = $path_processor_manager;
        $this->stackedRouteMatch = $stacked_route_match;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($container->get('router.admin_context'), $container->get('router'), $container->get('path_processor_manager'), $container->get('current_route_match'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLangcode(Request $request = NULL) {
        $langcode = NULL;
        // User preference (only for administrators).
        if (($this->currentUser
            ->hasPermission('access administration pages') || $this->currentUser
            ->hasPermission('view the administration theme')) && ($preferred_admin_langcode = $this->currentUser
            ->getPreferredAdminLangcode(FALSE)) && $this->isAdminPath($request)) {
            $langcode = $preferred_admin_langcode;
        }
        // Not an admin, no admin language preference or not on an admin path.
        return $langcode;
    }
    
    /**
     * Checks whether the given path is an administrative one.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request object.
     *
     * @return bool
     *   TRUE if the path is administrative, FALSE otherwise.
     */
    protected function isAdminPath(Request $request) {
        $result = FALSE;
        if ($request && $this->adminContext) {
            // If called from an event subscriber, the request may not have the route
            // object yet (it is still being built), so use the router to look up
            // based on the path.
            $route_match = $this->stackedRouteMatch
                ->getRouteMatchFromRequest($request);
            if ($route_match && !($route_object = $route_match->getRouteObject())) {
                try {
                    // Some inbound path processors make changes to the request. Make a
                    // copy as we're not actually routing the request so we do not want to
                    // make changes.
                    $cloned_request = clone $request;
                    // Process the path as an inbound path. This will remove any language
                    // prefixes and other path components that inbound processing would
                    // clear out, so we can attempt to load the route clearly.
                    $path = $this->pathProcessorManager
                        ->processInbound(urldecode(rtrim($cloned_request->getPathInfo(), '/')), $cloned_request);
                    $attributes = $this->router
                        ->match($path);
                } catch (ResourceNotFoundException $e) {
                    return FALSE;
                } catch (AccessDeniedHttpException $e) {
                    return FALSE;
                }
                $route_object = $attributes[RouteObjectInterface::ROUTE_OBJECT];
            }
            $result = $this->adminContext
                ->isAdminRoute($route_object);
        }
        return $result;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
LanguageNegotiationMethodBase::$config protected property The configuration factory.
LanguageNegotiationMethodBase::$currentUser protected property The current active user.
LanguageNegotiationMethodBase::$languageManager protected property The language manager.
LanguageNegotiationMethodBase::persist public function Notifies the plugin that the language code it returned has been accepted. Overrides LanguageNegotiationMethodInterface::persist 1
LanguageNegotiationMethodBase::setConfig public function Injects the configuration factory. Overrides LanguageNegotiationMethodInterface::setConfig
LanguageNegotiationMethodBase::setCurrentUser public function Injects the current user. Overrides LanguageNegotiationMethodInterface::setCurrentUser
LanguageNegotiationMethodBase::setLanguageManager public function Injects the language manager. Overrides LanguageNegotiationMethodInterface::setLanguageManager
LanguageNegotiationUserAdmin::$adminContext protected property The admin context.
LanguageNegotiationUserAdmin::$pathProcessorManager protected property The path processor manager.
LanguageNegotiationUserAdmin::$router protected property The router.
LanguageNegotiationUserAdmin::$stackedRouteMatch protected property The stacked route match.
LanguageNegotiationUserAdmin::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
LanguageNegotiationUserAdmin::getLangcode public function Performs language negotiation. Overrides LanguageNegotiationMethodInterface::getLangcode
LanguageNegotiationUserAdmin::isAdminPath protected function Checks whether the given path is an administrative one.
LanguageNegotiationUserAdmin::METHOD_ID constant The language negotiation method id.
LanguageNegotiationUserAdmin::__construct public function Constructs a new LanguageNegotiationUserAdmin instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.