Final Class Yiisoft\Yii\AuthClient\Widget\AuthChoice
| Inheritance | Yiisoft\ |
|---|
AuthChoice renders buttons for authentication via various OAuth2 clients.
By default, it renders client links that redirect to the auth route; with {@see \
Example:
<?= AuthChoice::widget()->authRoute('site/auth'); ?>
Customize appearance with {@see \
<?php
use Yiisoft\Yii\AuthClient\Widget\AuthChoice;
$authChoice = AuthChoice::widget()->authRoute('site/auth');
$authChoice->begin();
?>
<ul>
<?php foreach ($authChoice->getClients() as $client): ?>
<li><?= $authChoice->clientLink($client) ?></li>
<?php endforeach; ?>
</ul>
<?= AuthChoice::end() ?>
Configuration methods ({@see \<div> tag are produced during rendering.
Inline SVG icons are rendered via {@see \<span class="auth-icon {name}"> styling/testing hook (customizable via {@see \<svg> of registry icons only, not client-provided logos. Display mode
({@see \
This widget respects the following keys from {@see \
- popupWidth: int, width of the popup window in pixels (popup mode only).
- popupHeight: int, height of the popup window in pixels (popup mode only).
- widget: array, configuration for rendering a client link via a custom {@see \
Yiisoft\ Yii\ AuthClient\ Widget\ AuthChoiceItem} subclass instead of the default markup.
See also Yiisoft\
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| renderMainContent() | Renders the main content, which includes all external services links. | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $clientCollection | Yiisoft\ |
|
| $urlGenerator | \ |
|
| $assetManager | \ |
|
public function __construct(
Collection $clientCollection,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly AssetManager $assetManager,
) {
$this->clients = $clientCollection->getClients();
}
| public self authRoute ( string $authRoute ) | ||
| $authRoute | string | |
public function authRoute(string $authRoute): self
{
$this->authRoute = $authRoute;
return $this;
}
Opens the widget: registers assets and echoes the opening <div> tag, so that content written directly
to output between {@see begin()} and {@see end()} appears nested inside it.
| public ?string begin ( ) |
public function begin(): ?string
{
parent::begin();
echo $this->renderOpenTag();
return null;
}
Outputs client auth link.
| public string clientLink ( Yiisoft\ | ||
| $client | Yiisoft\ |
Extending from an auth client instance. |
| $text | string |
Link text, if not set - default value will be generated. |
| $htmlOptions | array |
Link HTML options. |
| return | string |
Generated HTML. |
|---|---|---|
| throws | Yiisoft\ |
on wrong configuration. |
| throws | \ |
|
public function clientLink(OAuth2Interface $client, ?string $text = null, array $htmlOptions = []): string
{
$viewOptions = $client->getViewOptions();
if (!empty($viewOptions['widget'])) {
return $this->renderClientItemWidget($client, (array) $viewOptions['widget']);
}
$encodeText = $text !== null;
if ($text === null) {
$icon = $this->renderClientLogo($client);
/** @infection-ignore-all Each arm renders different content for the three display modes */
$text = match ($this->displayMode) {
AuthChoiceDisplayMode::Icon => $icon,
AuthChoiceDisplayMode::Text => $client->getTitle(),
AuthChoiceDisplayMode::Both => $icon
. Html::span($client->getTitle(), ['class' => 'auth-title ms-2'])->render(),
};
$encodeText = $this->displayMode === AuthChoiceDisplayMode::Text;
}
if (!isset($htmlOptions['title'])) {
$htmlOptions['title'] = $client->getTitle();
}
$hasExplicitClass = isset($htmlOptions['class']);
Html::addCssClass($htmlOptions, ['widget' => 'auth-link']);
foreach ($this->linkAttributes as $key => $value) {
if ($key !== 'class') {
$htmlOptions[$key] = $value;
} elseif (!$hasExplicitClass) {
Html::addCssClass($htmlOptions, (string) $value);
}
}
if ($this->popupMode) {
if (isset($viewOptions['popupWidth'])) {
/**
* @var int $viewOptions['popupWidth']
* @var int $htmlOptions['data-popup-width']
*/
$htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
}
if (isset($viewOptions['popupHeight'])) {
/**
* @var int $viewOptions['popupHeight']
* @var int $htmlOptions['data-popup-height']
*/
$htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
}
}
return Html::a($text, $this->createClientUrl($client), $htmlOptions)->encode($encodeText)->render();
}
| public self clientOptions ( array $clientOptions ) | ||
| $clientOptions | array |
Additional options passed to the underlying JS plugin. Must be called before
{@see \ |
public function clientOptions(array $clientOptions): self
{
$this->clientOptions = $clientOptions;
return $this;
}
Composes client auth URL.
| public string createClientUrl ( Yiisoft\ | ||
| $client | Yiisoft\ |
External auth client instance. |
| return | string |
Auth URL. |
|---|---|---|
public function createClientUrl($client): string
{
/** @infection-ignore-all Disable auto-render when URL is requested directly; caller handles rendering */
$this->autoRender = false;
$params = [];
$params[$this->clientIdGetParamName] = $client->getName();
return $this->urlGenerator->generate($this->authRoute, $params);
}
| public self displayMode ( \ | ||
| $displayMode | \ |
|
public function displayMode(AuthChoiceDisplayMode $displayMode): self
{
$this->displayMode = $displayMode;
return $this;
}
| public Yiisoft\ | ||
| $name | string | |
public function getClient(string $name): OAuth2Interface
{
$clients = array_filter(
$this->getClients(),
fn($client) => $client->getName() === $name,
);
$client = end($clients);
if ($client === false) {
throw new InvalidConfigException("OAuth2 client with name '{$name}' not found.");
}
return $client;
}
| public self iconAttributes ( array $iconAttributes ) | ||
| $iconAttributes | array |
HTML attributes for the inner |
public function iconAttributes(array $iconAttributes): self
{
$this->iconAttributes = $iconAttributes;
return $this;
}
| public self iconHeight ( string|null $iconHeight ) | ||
| $iconHeight | string|null |
Height of SVG icons (e.g., '24'). Set to null to omit the attribute.
Must be called before {@see \ |
public function iconHeight(?string $iconHeight): self
{
$this->iconHeight = $iconHeight;
return $this;
}
| public self iconWidth ( string|null $iconWidth ) | ||
| $iconWidth | string|null |
Width of SVG icons (e.g., '24'). Set to null to omit the attribute.
Must be called before {@see \ |
public function iconWidth(?string $iconWidth): self
{
$this->iconWidth = $iconWidth;
return $this;
}
| public self iconWrapperAttributes ( array $iconWrapperAttributes ) | ||
| $iconWrapperAttributes | array |
HTML attributes for the icon's wrapping |
public function iconWrapperAttributes(array $iconWrapperAttributes): self
{
$this->iconWrapperAttributes = $iconWrapperAttributes;
return $this;
}
| public self linkAttributes ( array $linkAttributes ) | ||
| $linkAttributes | array |
HTML attributes for auth links, merged with default |
public function linkAttributes(array $linkAttributes): self
{
$this->linkAttributes = $linkAttributes;
return $this;
}
| public self options ( array $options ) | ||
| $options | array |
The HTML attributes for the container |
public function options(array $options): self
{
$this->options = $options;
return $this;
}
| public self popupMode ( boolean $popupMode ) | ||
| $popupMode | boolean |
Whether a popup window should be used instead of direct links. Must be called
before {@see \ |
public function popupMode(bool $popupMode): self
{
$this->popupMode = $popupMode;
return $this;
}
Runs the widget.
| public string render ( ) | ||
| return | string |
Rendered HTML. |
|---|---|---|
| throws | \ |
|
public function render(): string
{
$content = $this->renderOpenTag();
if ($this->autoRender) {
$content .= $this->renderMainContent();
}
$content .= Html::div()->close();
return $content;
}
Renders the main content, which includes all external services links.
| protected string renderMainContent ( ) | ||
| return | string |
Generated HTML. |
|---|---|---|
| throws | Yiisoft\ |
|
| throws | \ |
|
protected function renderMainContent(): string
{
$content = '';
/**
* @var OAuth2Interface $externalService
*/
foreach ($this->getClients() as $externalService) {
// clientLink() already returns rendered, safe-to-embed HTML.
/** @infection-ignore-all Must concatenate to render all clients, not just the last one */
$content .= $this->clientLink($externalService);
}
return $content;
}
| public void setClients ( array | ||
| $clients | array |
|
public function setClients(array $clients): void
{
$this->clients = $clients;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.