Class yii\authclient\widgets\GooglePlusButton
| Inheritance | yii\ |
|---|---|
| Available since extension's version | 2.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\
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $authChoice | yii\ |
Parent AuthChoice widget | yii\ |
| $buttonHtmlOptions | array | Button tag HTML options, which will be merged with the default ones. | yii\ |
| $callback | string|array | Callback JavaScript function name or URL config. | yii\ |
| $client | yii\ |
Auth client instance. | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| getCallback() | yii\ |
|
| init() | Initializes the widget. | yii\ |
| run() | Runs the widget. | yii\ |
| setCallback() | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| generateCallback() | Generates JavaScript callback function, which will be used to handle auth response. | yii\ |
| registerClientScript() | Registers necessary JavaScript. | yii\ |
| renderButton() | Renders sign-in button. | yii\ |
Property Details
Method Details
Generates JavaScript callback function, which will be used to handle auth response.
| protected string generateCallback ( array $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;
}
| 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;
}
Initializes the widget.
| public mixed init ( ) |
public function init()
{
if (!($this->client instanceof GoogleHybrid)) {
throw new InvalidConfigException('"' . self::class . '::client" must be instance of "' . GoogleHybrid::class . '"');
}
}
Registers necessary JavaScript.
| protected mixed 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__);
}
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']);
}
Runs the widget.
| public mixed run ( ) |
public function run()
{
$this->registerClientScript();
return $this->renderButton();
}