You are here

function template_preprocess_hybridauth_widget in HybridAuth Social Login 7

Same name and namespace in other branches
  1. 6.2 hybridauth.theme.inc \template_preprocess_hybridauth_widget()
  2. 7.2 hybridauth.theme.inc \template_preprocess_hybridauth_widget()

File

./hybridauth.widget.inc, line 3

Code

function template_preprocess_hybridauth_widget(&$vars, $hook) {

  // We need to include the auth functions to get a list of enabled providers
  module_load_include('inc', 'hybridauth', 'hybridauth.auth');
  $style = $vars['style'];
  $size = $vars['size'];
  $vars['widget_id'] = drupal_html_id($hook);
  $vars['classes_array'][] = drupal_html_class('hybridauth-style-' . $style);
  $vars['classes_array'][] = drupal_html_class('hybridauth-size-' . $size);
  $vars['providers'] = array();
  $providers = array();
  switch ($style) {
    case 'button':
      $providers = array(
        'none' => t('Sign in using a social networking account'),
      );
      break;
    case 'link':
      break;
    case 'list':
    default:
      $providers = hybridauth_get_enabled_providers();
      break;
  }
  if (!empty($providers)) {
    foreach ($providers as $provider_id => $provider_name) {
      $icon_id = drupal_html_id('hybridauth-widget-provider-' . $provider_id);
      $icon_url = url(drupal_get_path('module', 'hybridauth_widget') . '/images/icons/' . strtolower($provider_id) . '.png');
      $icon_class = array(
        'hybridauth-widget-provider',
      );
      $query = array(
        'provider' => $provider_id,
        'style' => $vars['style'],
      ) + drupal_get_destination();
      if ($style != 'list' && variable_get('hybridauth_widget_use_overlay', HYBRIDAUTH_WIDGET_USE_OVERLAY_DEFAULT)) {
        if (module_exists('colorbox')) {

          // We need to load the colorbox-load behavior
          drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_load.js');
          $query['overlay'] = 'true';

          //$icon_class[] = 'colorbox';
          $icon_class[] = 'colorbox-load';
          $query['width'] = 430;
          $query['height'] = 222;
          $query['iframe'] = 'true';
        }
      }
      $vars['providers'][$provider_id] = l(theme('hybridauth_provider_icon', array(
        'size' => $size,
        'provider_id' => $provider_id,
      )), 'hybridauth/popup', array(
        'html' => TRUE,
        'query' => $query,
        'attributes' => array(
          'id' => $icon_id,
          'class' => $icon_class,
        ),
      ));
    }
    drupal_add_js('
      (function ($) {
        Drupal.behaviors.hybridauthWidget = {
          attach: function (context, settings) {
            $(".hybridauth-widget-provider", context).once("hybridauth-widget-provider", function() {
              $(this).click(function(event) {
                event.preventDefault();
                var url = $(this).attr("href");
                window.open(
                  url + "&authenticate=1",
                  "hybridauth_social_sign_on",
                  "location=0,status=0,scrollbars=0,width=800,height=500"
                );
                return false;
              });
            });
          }
        };
      }(jQuery));
    ', 'inline');
  }
  elseif ($style == 'link') {
    $provider_id = 'none';
    $link_id = drupal_html_id('hybridauth-widget-provider-' . $provider_id);
    $link_class = array(
      'hybridauth-widget-provider',
    );
    $query = array(
      'provider' => $provider_id,
      'style' => $vars['style'],
    ) + drupal_get_destination();
    if (variable_get('hybridauth_widget_use_overlay', HYBRIDAUTH_WIDGET_USE_OVERLAY_DEFAULT)) {
      if (module_exists('colorbox')) {

        // We need to load the colorbox-load behavior
        drupal_add_js(drupal_get_path('module', 'colorbox') . '/js/colorbox_load.js');
        $query['overlay'] = 'true';

        //$link_class[] = 'colorbox';
        $link_class[] = 'colorbox-load';
        $query['width'] = 430;
        $query['height'] = 222;
        $query['iframe'] = 'true';
      }
    }
    $vars['providers'][$provider_id] = l(variable_get('hybridauth_widget_attach_loginform_link_title', HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_LINK_TITLE_DEFAULT), 'hybridauth/popup', array(
      'html' => TRUE,
      'query' => $query,
      'attributes' => array(
        'id' => $link_id,
        'class' => $link_class,
        'title' => variable_get('hybridauth_widget_attach_loginform_link_alt', HYBRIDAUTH_WIDGET_ATTACH_LOGINFORM_LINK_ALT_DEFAULT),
      ),
    ));
  }
  _hybridauth_add_widget_css();
}