You are here

LoginLogoutMenuLink.php in SAML Authentication 4.x

File

src/Plugin/Menu/LoginLogoutMenuLink.php
View source
<?php

namespace Drupal\samlauth\Plugin\Menu;

use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\user\Plugin\Menu\LoginLogoutMenuLink as DrupalLoginLogoutMenuLink;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * A menu link that shows "Log in" or "Log out" as appropriate.
 */
class LoginLogoutMenuLink extends DrupalLoginLogoutMenuLink {

  /**
   * A configuration object containing samlauth settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Constructs a new LoginLogoutMenuLink.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
   *   The static override storage.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override, $current_user);
    $this->config = $config_factory
      ->get('samlauth.authentication');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('menu_link.static.overrides'), $container
      ->get('current_user'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    if ($this->currentUser
      ->isAuthenticated()) {
      $setting = 'logout_menu_item_title';
    }
    else {
      $setting = 'login_menu_item_title';
    }
    $title = $this->config
      ->get($setting);
    return $title ?: parent::getTitle();
  }

  /**
   * {@inheritdoc}
   */
  public function getRouteName() {
    if ($this->currentUser
      ->isAuthenticated()) {
      return 'samlauth.saml_controller_logout';
    }
    else {
      return 'samlauth.saml_controller_login';
    }
  }

}

Classes

Namesort descending Description
LoginLogoutMenuLink A menu link that shows "Log in" or "Log out" as appropriate.