You are here

function _social_auth_extra_get_buttons in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  2. 8 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  3. 8.2 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  4. 8.3 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  5. 8.5 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  6. 8.6 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  7. 8.7 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()
  8. 8.8 modules/custom/social_auth_extra/social_auth_extra.module \_social_auth_extra_get_buttons()

Returns form array with social network auth buttons.

2 calls to _social_auth_extra_get_buttons()
social_auth_extra_form_social_user_login_form_alter in modules/custom/social_auth_extra/social_auth_extra.module
Implements hook_form_FORM_ID_alter().
social_auth_extra_form_user_register_form_alter in modules/custom/social_auth_extra/social_auth_extra.module
Implements hook_form_FORM_ID_alter().

File

modules/custom/social_auth_extra/social_auth_extra.module, line 80
Contains social_auth_extra.module.

Code

function _social_auth_extra_get_buttons($type = 'login') {
  $form = [];
  $network_manager = \Drupal::service('plugin.network.manager');
  $networks = $network_manager
    ->getDefinitions();
  $provider = \Drupal::request()
    ->get('provider');
  if (!empty($networks)) {
    $sign_up = new TranslatableMarkup('Sign up');
    $login = new TranslatableMarkup('Log in');
    $form['social_auth_extra'] = [
      '#type' => 'fieldset',
      '#attributes' => [
        'class' => [
          'social-auth-buttons',
        ],
      ],
      '#title' => new TranslatableMarkup('@action with <b>Social Accounts</b>', [
        '@action' => $type === 'register' ? $sign_up : $login,
      ]),
      '#weight' => -100,
      '#attached' => [
        'library' => [
          'socialbase/form--social-auth-buttons',
        ],
      ],
    ];

    // If there's a help text for social signup/login we display it to the user.
    $help_key = $type === 'login' ? 'social_login_help' : 'social_signup_help';
    $help_text = \Drupal::config('social_auth_extra.settings')
      ->get($help_key);
    if (!empty($help_text)) {
      $form['social_auth_extra']['#description'] = $help_text;
    }
    foreach ($networks as $key => $network) {
      $instance = $network_manager
        ->createInstance($network['id']);
      $status = \Drupal::config("{$key}.settings")
        ->get('status');
      $network_btnlabel = strtolower('btn--' . $network['social_network']);
      $btn_attributes = [
        'class' => [
          'btn',
          'btn--with-bgicon',
          $network_btnlabel,
        ],
      ];
      if ($provider && $provider != $instance
        ->getSocialNetworkKey()) {
        $btn_attributes['disabled'] = 'disabled';
      }
      if ($status) {
        $form['social_auth_extra'][$key] = [
          '#type' => 'link',
          '#title' => $network['social_network'],
          '#url' => Url::fromRoute("{$key}.user_{$type}"),
          '#attributes' => $btn_attributes,
        ];
      }
    }

    // If there are no any active network, hide fieldset.
    if (empty(array_intersect_key($networks, $form['social_auth_extra']))) {
      unset($form['social_auth_extra']);
    }
  }
  return $form;
}