You are here

Ulogin.php in uLogin (advanced version) 8

File

src/Plugin/Block/Ulogin.php
View source
<?php

namespace Drupal\ulogin\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Provides a 'Ulogin' block.
 *
 * @Block(
 *   id = "ulogin_block",
 *   admin_label = @Translation("User login - uLogin widget only")
 * )
 */
class Ulogin extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Creates a HelpBlock instance.
   *
   * @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 \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, ModuleHandlerInterface $module_handler, RouteMatchInterface $route_match) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->request = $request;
    $this->moduleHandler = $module_handler;
    $this->routeMatch = $route_match;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('request_stack')
      ->getCurrentRequest(), $container
      ->get('module_handler'), $container
      ->get('current_route_match'));
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $block = [
      '#cache' => [
        'max-age' => 0,
      ],
    ];
    if (!\Drupal::currentUser()
      ->isAuthenticated() && \Drupal::currentUser()
      ->hasPermission('use ulogin')) {
      $block['subject'] = $this
        ->t('User login');
      $block['content'] = [
        'ulogin' => [
          '#type' => 'ulogin_widget',
        ],
      ];
    }
    return $block;
  }

}

Classes

Namesort descending Description
Ulogin Provides a 'Ulogin' block.