Final Class Yiisoft\Bootstrap5\Toggler
| Inheritance | Yiisoft\Bootstrap5\Toggler |
|---|
Toggler represents a single collapsible item within a Bootstrap Collapse component.
Each item consists of a toggler (button/link) that controls the visibility of collapsible content. The toggler can be rendered as either a button or link, and can control single or multiple collapse targets.
Example usage:
`php
// Basic usage
Toggler::for(
'Collapsible content here',
togglerContent: 'Toggle collapse'
);
// As a link with custom ID Toggler::for(
'Collapsible content',
togglerContent: 'Toggle collapse',
togglerAsLink: true,
id: 'customId'
);
// Multiple collapse control Toggler::for()
->togglerMultiple(true)
->ariaControls('collapse1 collapse2')
->togglerContent('Toggle multiple collapses');
Public Methods
| Method | Description | Defined By |
|---|---|---|
| ariaControls() | Sets the aria-controls attribute value for the toggler. |
Yiisoft\Bootstrap5\Toggler |
| content() | Sets the content to be displayed in the collapsible item. | Yiisoft\Bootstrap5\Toggler |
| encode() | Sets whether to HTML encode the content. | Yiisoft\Bootstrap5\Toggler |
| for() | Yiisoft\Bootstrap5\Toggler | |
| getContent() | Yiisoft\Bootstrap5\Toggler | |
| getId() | Generates the ID. | Yiisoft\Bootstrap5\Toggler |
| getTogglerMultiple() | Yiisoft\Bootstrap5\Toggler | |
| id() | Sets the ID. | Yiisoft\Bootstrap5\Toggler |
| renderToggler() | Render the toggler to be displayed in the collapsible item. | Yiisoft\Bootstrap5\Toggler |
| togglerAsLink() | Sets whether the toggler should be rendered as a link. | Yiisoft\Bootstrap5\Toggler |
| togglerAttributes() | Sets the HTML attributes for the toggler. | Yiisoft\Bootstrap5\Toggler |
| togglerContent() | Sets the content to be displayed in the toggler. | Yiisoft\Bootstrap5\Toggler |
| togglerMultiple() | Sets whether the toggler should control multiple collapse items. | Yiisoft\Bootstrap5\Toggler |
| togglerTag() | Sets the tag name to be used to render the toggler. | Yiisoft\Bootstrap5\Toggler |
Method Details
Sets the aria-controls attribute value for the toggler.
| public self ariaControls ( string $ariaControls ) | ||
| $ariaControls | string |
The |
| return | self |
A new instance with the specified |
|---|---|---|
public function ariaControls(string $ariaControls): self
{
$new = clone $this;
$new->ariaControls = $ariaControls;
return $new;
}
Sets the content to be displayed in the collapsible item.
| public self content ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The content to be displayed in the collapsible item. |
| return | self |
A new instance with the specified content to be displayed in the collapsible item. |
|---|---|---|
public function content(string|Stringable $content): self
{
$new = clone $this;
$new->content = (string)$content;
return $new;
}
Sets whether to HTML encode the content.
| public self encode ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to HTML encode the content. |
| return | self |
A new instance with the specified encoding behavior. |
|---|---|---|
public function encode(bool $enabled): self
{
$new = clone $this;
$new->encode = $enabled;
return $new;
}
| public static self for ( string $content = '', string|boolean $id = true, string $togglerTag = 'button', string $togglerContent = '', boolean $togglerAsLink = false, array $togglerAttributes = [], boolean $encode = true, boolean $togglerMultiple = false, string $ariaControls = '' ) | ||
| $content | string | |
| $id | string|boolean | |
| $togglerTag | string | |
| $togglerContent | string | |
| $togglerAsLink | boolean | |
| $togglerAttributes | array | |
| $encode | boolean | |
| $togglerMultiple | boolean | |
| $ariaControls | string | |
public static function for(
string $content = '',
string|bool $id = true,
string $togglerTag = 'button',
string $togglerContent = '',
bool $togglerAsLink = false,
array $togglerAttributes = [],
bool $encode = true,
bool $togglerMultiple = false,
string $ariaControls = '',
): self {
$new = new self(
$content,
$id,
$togglerTag,
$togglerContent,
$togglerAsLink,
$togglerAttributes,
$encode,
$togglerMultiple,
$ariaControls,
);
return $new->id($new->getId());
}
| public string getContent ( ) | ||
| return | string |
The content to be displayed in the collapsible item. |
|---|---|---|
public function getContent(): string
{
return $this->encode ? Html::encode($this->content) : $this->content;
}
Generates the ID.
| public string getId ( ) | ||
| return | string |
The generated ID. |
|---|---|---|
| throws | InvalidArgumentException |
if the ID is an empty string or |
public function getId(): string
{
return match ($this->id) {
true => Html::generateId('collapse-'),
'', false => throw new InvalidArgumentException('The "id" must be specified.'),
default => $this->id,
};
}
| public boolean getTogglerMultiple ( ) |
public function getTogglerMultiple(): bool
{
return $this->togglerMultiple;
}
Sets the ID.
| public self id ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the component. If |
| return | self |
A new instance with the specified ID. |
|---|---|---|
| throws | InvalidArgumentException |
if the ID is an empty string or |
public function id(bool|string $id): self
{
$new = clone $this;
$new->id = $id;
return $new;
}
Render the toggler to be displayed in the collapsible item.
| public string renderToggler ( ) | ||
| return | string |
The HTML representation of the element. |
|---|---|---|
| throws | InvalidArgumentException |
if the toggler tag is an empty string. |
public function renderToggler(): string
{
if ($this->togglerTag === '') {
throw new InvalidArgumentException('Toggler tag cannot be empty string.');
}
$tagName = $this->togglerAsLink ? 'a' : $this->togglerTag;
$togglerAttributes = $this->togglerAttributes;
$togglerClasses = $this->togglerAttributes['class'] ?? 'btn btn-primary';
unset($togglerAttributes['class']);
return Html::tag($tagName, $this->togglerContent)
->attribute('type', $tagName === 'button' ? 'button' : null)
->attribute('data-bs-toggle', 'collapse')
->attribute('data-bs-target', $this->togglerMultiple ? '.multi-collapse' : '#' . $this->id)
->attribute('role', $this->togglerAsLink ? 'button' : null)
->attribute('aria-expanded', 'false')
->attribute('aria-controls', $this->togglerMultiple ? $this->ariaControls : $this->id)
->addClass($togglerClasses)
->addAttributes($togglerAttributes)
->render();
}
Sets whether the toggler should be rendered as a link.
| public self togglerAsLink ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to render the toggler as a link.
When true, render as an |
| return | self |
A new instance with the specified toggler as link setting. |
|---|---|---|
public function togglerAsLink(bool $enabled): self
{
$new = clone $this;
$new->togglerAsLink = $enabled;
return $new;
}
Sets the HTML attributes for the toggler.
| public self togglerAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
A new instance with the specified attributes for the toggler. |
|---|---|---|
public function togglerAttributes(array $attributes): self
{
$new = clone $this;
$new->togglerAttributes = $attributes;
return $new;
}
Sets the content to be displayed in the toggler.
| public self togglerContent ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The content to be displayed in the toggler. |
| return | self |
A new instance with the specified content to be displayed in the toggler. Example usage:
|
|---|---|---|
public function togglerContent(string|Stringable $content): self
{
$new = clone $this;
$new->togglerContent = (string) $content;
return $new;
}
Sets whether the toggler should control multiple collapse items.
| public self togglerMultiple ( boolean $enabled ) | ||
| $enabled | boolean |
Whether the toggler should control multiple collapse items.
When true, the toggler will target all collapse items with class |
| return | self |
A new instance with the specified toggler multiple setting. Example usage:
|
|---|---|---|
public function togglerMultiple(bool $enabled): self
{
$new = clone $this;
$new->togglerMultiple = $enabled;
return $new;
}
Sets the tag name to be used to render the toggler.
| public self togglerTag ( string $tag ) | ||
| $tag | string |
The tag name to be used to render the toggler. |
| return | self |
A new instance with the specified tag name to be used to render the toggler. Example usage:
|
|---|---|---|
| throws | InvalidArgumentException |
if the tag name is an empty string. |
public function togglerTag(string $tag): self
{
$new = clone $this;
$new->togglerTag = $tag;
return $new;
}
Signup or Login in order to comment.