You are here

public function SimplesamlphpAuthBlock::build in simpleSAMLphp Authentication 8.3

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/SimplesamlphpAuthBlock.php, line 73

Class

SimplesamlphpAuthBlock
Provides a 'SimpleSAMLphp authentication status' block.

Namespace

Drupal\simplesamlphp_auth\Plugin\Block

Code

public function build() {
  $content = [
    '#title' => $this
      ->t('SimpleSAMLphp Auth Status'),
    '#cache' => [
      'contexts' => [
        'user',
      ],
      'tags' => $this->config
        ->getCacheTags(),
    ],
  ];
  if ($this->simplesamlAuth
    ->isActivated()) {
    if ($this->simplesamlAuth
      ->isAuthenticated()) {
      $content['#markup'] = $this
        ->t('Logged in as %authname<br /><a href=":logout">Log out</a>', [
        '%authname' => $this->simplesamlAuth
          ->getAuthname(),
        ':logout' => Url::fromRoute('user.logout')
          ->toString(),
      ]);
    }
    else {
      $label = $this->config
        ->get('login_link_display_name');
      $login_link = [
        '#title' => $label,
        '#type' => 'link',
        '#url' => Url::fromRoute('simplesamlphp_auth.saml_login'),
        '#attributes' => [
          'title' => $label,
          'class' => [
            'simplesamlphp-auth-login-link',
          ],
        ],
      ];
      $content['link'] = $login_link;
    }
  }
  else {
    $content['#markup'] = $this
      ->t('Warning: SimpleSAMLphp is not activated.');
  }
  return $content;
}