You are here

public function ShibbolethLoginBlock::build in Shibboleth Authentication 8

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/ShibbolethLoginBlock.php, line 21

Class

ShibbolethLoginBlock
Provides a 'ShibbolethLoginBlock' block.

Namespace

Drupal\shib_auth\Plugin\Block

Code

public function build() {
  $config = \Drupal::config('shib_auth.advancedsettings');
  $current_user = \Drupal::currentUser();
  $markup = '<div class="shibboleth-links">';
  if (!$current_user
    ->id()) {
    $markup .= '<div class="shibboleth-login">' . shib_auth_get_login_link() . '</div>';
  }
  else {
    $markup .= '<div class="shibboleth-logout">' . shib_auth_get_logout_link() . '</div>';
  }
  $markup .= '</div>';
  $build['shibboleth_login_block'] = [
    '#markup' => $markup,
    '#cache' => [
      'contexts' => [
        'user.roles:anonymous',
      ],
    ],
  ];
  if (!$config
    ->get('url_redirect_login')) {

    // Redirect is not set, so it will use the current path. That means it
    // will differ per page.
    $build['shibboleth_login_block']['#cache']['contexts'][] = 'url.path';
    $build['shibboleth_login_block']['#cache']['contexts'][] = 'url.query_args';
  }
  return $build;
}