You are here

public function DropDownLoginBlock::build in Drop Down Login 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/DropdownLoginBlock.php, line 97

Class

DropDownLoginBlock
Provides a 'Drop Down Login' block.

Namespace

Drupal\drop_down_login\Plugin\Block

Code

public function build() {

  // Checking User anonymous then open block.
  $user = $this->account
    ->isAnonymous();

  // For anonymous user.
  if ($user) {

    // User Login Block.
    $customblock = $this->blockManager
      ->createInstance('user_login_block', []);
    $login_form = $customblock
      ->build();
    $login_url = $this->urlGenerator
      ->generateFromRoute('user.page');
    $login_link_text = $this
      ->t('Login');
    $output = [
      '#attached' => [
        'library' => [
          'drop_down_login/drop_down_login_setting',
        ],
      ],
      '#theme' => 'drop_down_login',
      '#login_form' => $login_form,
      '#login_url' => $login_url,
      '#login_link_text' => $login_link_text,
    ];
  }
  else {

    // Get Configuration.
    $settings = $this->configFactory
      ->getEditable('drop_down_login.admin.settings');
    $myAccount = $settings
      ->get('drop_down_login_want_myaccount');
    if (!empty($myAccount['drop_down_login_want_myaccount']) && isset($myAccount['drop_down_login_want_myaccount'])) {
      $myAccountLinkConfig = $settings
        ->get('drop_down_login_myaccount_links');
      $myaccount_url = $this->urlGenerator
        ->generateFromRoute('user.page');
      $logout_url = $this->urlGenerator
        ->generateFromRoute('user.logout');
      $logout_link_text = $this
        ->t('Log Out');
      $myaccount_text = $this
        ->t('My Account');
      $myAccountlink = $myAccountLinkConfig['table'];
      $output = [
        '#attached' => [
          'library' => [
            'drop_down_login/drop_down_login_setting',
          ],
        ],
        '#theme' => 'drop_down_myaccount',
        '#myaccount_links' => $myAccountlink,
        '#myaccount_url' => $myaccount_url,
        '#myaccount_link_text' => $myaccount_text,
        '#name' => $this->account
          ->getUsername(),
        '#logout_url' => $logout_url,
        '#logout_link_text' => $logout_link_text,
      ];
    }
    else {
      $logout_url = $this->urlGenerator
        ->generateFromRoute('user.logout');
      $logout_link_text = $this
        ->t('Log Out');
      $output = [
        '#attached' => [
          'library' => [
            'drop_down_login/drop_down_login_setting',
          ],
        ],
        '#theme' => 'drop_down_logout',
        '#logout_url' => $logout_url,
        '#logout_link_text' => $logout_link_text,
      ];
    }
  }
  return $output;
}