Class yii\authclient\widgets\GooglePlusButton

Inheritanceyii\authclient\widgets\GooglePlusButton » yii\authclient\widgets\AuthChoiceItem » yii\base\Widget
Available since extension's version2.0.4
Source Code https://github.com/yiisoft/yii2-authclient/blob/master/src/widgets/GooglePlusButton.php

GooglePlusButton renders Google+ sign-in button.

This widget is designed to interact with yii\authclient\clients\GoogleHybrid.

See also:

Public Properties

Hide inherited properties

Property Type Description Defined By
$authChoice yii\authclient\widgets\AuthChoice Parent AuthChoice widget yii\authclient\widgets\AuthChoiceItem
$buttonHtmlOptions array Button tag HTML options, which will be merged with the default ones. yii\authclient\widgets\GooglePlusButton
$callback string Callback JavaScript function name. yii\authclient\widgets\GooglePlusButton
$client yii\authclient\ClientInterface Auth client instance. yii\authclient\widgets\AuthChoiceItem

Protected Methods

Hide inherited methods

Method Description Defined By
generateCallback() Generates JavaScript callback function, which will be used to handle auth response. yii\authclient\widgets\GooglePlusButton
registerClientScript() Registers necessary JavaScript. yii\authclient\widgets\GooglePlusButton
renderButton() Renders sign-in button. yii\authclient\widgets\GooglePlusButton

Property Details

Hide inherited properties

$buttonHtmlOptions public property

Button tag HTML options, which will be merged with the default ones.

$callback public property

Callback JavaScript function name. Note that the type of this property differs in getter and setter. See getCallback() and setCallback() for details.

public string $callback null

Method Details

Hide inherited methods

generateCallback() protected method

Generates JavaScript callback function, which will be used to handle auth response.

protected string generateCallback ( $url = [] )
$url array

Auth callback URL.

return string

JavaScript function name.

                protected function generateCallback($url = [])
{
    if (empty($url)) {
        $url = $this->authChoice->createClientUrl($this->client);
    } else {
        $url = Url::to($url);
    }
    if (strpos($url, '?') === false) {
        $url .= '?';
    } else {
        $url .= '&';
    }
    $callbackName = 'googleSignInCallback' . md5($this->id);
    $js = <<<JS
tion $callbackName(authResult) {
var urlParams = [];
if (authResult['code']) {
    urlParams.push('code=' + encodeURIComponent(authResult['code']));
} else if (authResult['error']) {
    if (authResult['error'] == 'immediate_failed') {
        return;
    }
    urlParams.push('error=' + encodeURIComponent(authResult['error']));
    urlParams.push('error_description=' + encodeURIComponent(authResult['error_description']));
} else {
    for (var propName in authResult) {
        var propValue = authResult[propName];
        if (typeof propValue != 'object') {
            urlParams.push(encodeURIComponent(propName) + '=' + encodeURIComponent(propValue));
        }
    }
}
window.location = '$url' + urlParams.join('&');
    $this->view->registerJs($js, View::POS_END, __CLASS__ . '#' . $this->id);
    return $callbackName;
}

            
getCallback() public method

public string getCallback ( )
return string

Callback JavaScript function name.

                public function getCallback()
{
    if (empty($this->_callback)) {
        $this->_callback = $this->generateCallback();
    } elseif (is_array($this->_callback)) {
        $this->_callback = $this->generateCallback($this->_callback);
    }
    return $this->_callback;
}

            
init() public method

Initializes the widget.

public void init ( )

                public function init()
{
    if (!($this->client instanceof GoogleHybrid)) {
        throw new InvalidConfigException('"' . $this->className() . '::client" must be instance of "' . GoogleHybrid::className() . '"');
    }
}

            
registerClientScript() protected method

Registers necessary JavaScript.

protected void registerClientScript ( )

                protected function registerClientScript()
{
    $js = <<<JS
ction() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
;
    $this->view->registerJs($js, View::POS_END, __CLASS__);
}

            
renderButton() protected method

Renders sign-in button.

protected string renderButton ( )
return string

Button HTML.

                protected function renderButton()
{
    $buttonHtmlOptions = array_merge(
        [
            'class' => 'g-signin',
            'data-callback' => $this->getCallback(),
            'data-clientid' => $this->client->clientId,
            'data-cookiepolicy' => 'single_host_origin',
            'data-requestvisibleactions' => null,
            'data-scope' => $this->client->scope,
            'data-accesstype' => 'offline',
            'data-width' => 'iconOnly',
        ],
        $this->buttonHtmlOptions
    );
    return Html::tag('span', Html::tag('span', '', $buttonHtmlOptions), ['id' => 'signinButton']);
}

            
run() public method

Runs the widget.

public void run ( )

                public function run()
{
    $this->registerClientScript();
    return $this->renderButton();
}

            
setCallback() public method

public void setCallback ( $callback )
$callback string|array

Callback JavaScript function name or URL config.

                public function setCallback($callback)
{
    $this->_callback = $callback;
}