| Conditions | 2 |
| Paths | 2 |
| Total Lines | 75 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 60 | public function build() { |
||
| 61 | $block = []; |
||
| 62 | $links_cache_contexts = []; |
||
| 63 | |||
| 64 | // Display different links to depending on whether the user is logged in. |
||
| 65 | if ($this->user->isAnonymous()) { |
||
| 66 | // Create a 'person' icon that toggles a login form. |
||
| 67 | $markup = ''; |
||
| 68 | $markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-login-wrapper" aria-controls="user-login-wrapper" aria-expanded="false">'; |
||
| 69 | $markup .= '<i class="icon ion-ios-person"><span>Login</span></i>'; |
||
| 70 | $markup .= '</a>'; |
||
| 71 | |||
| 72 | $block['login'] = [ |
||
| 73 | '#markup'=> $markup, |
||
| 74 | ]; |
||
| 75 | |||
| 76 | // Wrap the login form with some required foundation classes/attributes |
||
| 77 | $login_wrapper = [ |
||
| 78 | '#type' => 'container', |
||
| 79 | '#attributes' =>[ |
||
| 80 | 'class' => ['dropdown-pane', 'display-none'], |
||
| 81 | 'id' => 'user-login-wrapper', |
||
| 82 | 'data-dropdown' => '', |
||
| 83 | 'aria-hidden' => 'true', |
||
| 84 | 'aria-autoclose' => 'true', |
||
| 85 | 'data-auto-focus' => 'true', |
||
| 86 | 'tabindex' => '-1' |
||
| 87 | ], |
||
| 88 | ]; |
||
| 89 | |||
| 90 | // Get the user login form. |
||
| 91 | $form = $this->formBuilder->getForm('\Drupal\user\Form\UserLoginForm'); |
||
| 92 | |||
| 93 | // Remove default form descriptions. |
||
| 94 | unset($form['name']['#description']); |
||
| 95 | unset($form['name']['#attributes']['aria-describedby']); |
||
| 96 | unset($form['pass']['#description']); |
||
| 97 | unset($form['pass']['#attributes']['aria-describedby']); |
||
| 98 | |||
| 99 | $login_wrapper['form'] = $form; |
||
| 100 | |||
| 101 | $block['wrapper'] = $login_wrapper; |
||
| 102 | } |
||
| 103 | else { |
||
| 104 | $username = $this->user->getDisplayName(); |
||
| 105 | $user_page_url = Url::fromRoute('user.page')->toString(); |
||
| 106 | $user_logout_url = Url::fromRoute('user.logout')->toString(); |
||
| 107 | |||
| 108 | // Create a 'person' icon that toggles user profile and log out links. |
||
| 109 | $markup = ''; |
||
| 110 | $markup .= '<a class="login-dropdown-button standard-icon meta-icon-size left" data-toggle="user-logout-wrapper" aria-controls="user-logout-wrapper" aria-expanded="false">'; |
||
| 111 | $markup .= '<i class="icon ion-ios-person"></i>'; |
||
| 112 | $markup .= '<span> '. $username . '</span>'; |
||
| 113 | $markup .= '</a>'; |
||
| 114 | |||
| 115 | $markup .= '<div id="user-logout-wrapper" class="dropdown-pane f-dropdown" data-dropdown aria-hidden="true" aria-autoclose="false" data-auto-focus="false">'; |
||
| 116 | $markup .= '<div class="user-links">'; |
||
| 117 | $markup .= '<a class="" href="' . $user_page_url . '"><i class="icon ion-ios-person"></i> ' . $username . '</a>'; |
||
| 118 | $markup .= '<a class="logout-button" href="' . $user_logout_url . '"><i class="icon ion-log-out"></i> ' . t('Log Out') . '</a>'; |
||
| 119 | $markup .= '</div>'; |
||
| 120 | $markup .= '</div>'; |
||
| 121 | |||
| 122 | $block['login'] = [ |
||
| 123 | '#markup'=> $markup, |
||
| 124 | ]; |
||
| 125 | |||
| 126 | // The "Edit user account" link is per-user. |
||
| 127 | $links_cache_contexts[] = 'user'; |
||
| 128 | } |
||
| 129 | |||
| 130 | // Cacheable per "authenticated or not", because the links to |
||
| 131 | // display depend on that. |
||
| 132 | $block['#cache']['contexts'] = Cache::mergeContexts(['user.roles:authenticated'], $links_cache_contexts); |
||
| 133 | |||
| 134 | return $block; |
||
| 135 | } |
||
| 138 |