0 follower

Final Class Yiisoft\Bootstrap5\Toggler

InheritanceYiisoft\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

Hide inherited 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

Hide inherited methods

ariaControls() public method

Sets the aria-controls attribute value for the toggler.

public self ariaControls ( string $ariaControls )
$ariaControls string

The aria-controls attribute value for the toggler.

return self

A new instance with the specified aria-controls attribute value for the toggler.

                public function ariaControls(string $ariaControls): self
{
    $new = clone $this;
    $new->ariaControls = $ariaControls;
    return $new;
}

            
content() public method

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;
}

            
encode() public method

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;
}

            
for() public static method

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());
}

            
getContent() public method

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;
}

            
getId() public method

Generates the ID.

public string getId ( )
return string

The generated ID.

throws InvalidArgumentException

if the ID is an empty string or false.

                public function getId(): string
{
    return match ($this->id) {
        true => Html::generateId('collapse-'),
        '', false => throw new InvalidArgumentException('The "id" must be specified.'),
        default => $this->id,
    };
}

            
getTogglerMultiple() public method

public boolean getTogglerMultiple ( )

                public function getTogglerMultiple(): bool
{
    return $this->togglerMultiple;
}

            
id() public method

Sets the ID.

public self id ( boolean|string $id )
$id boolean|string

The ID of the component. If true, an ID will be generated automatically.

return self

A new instance with the specified ID.

throws InvalidArgumentException

if the ID is an empty string or false.

                public function id(bool|string $id): self
{
    $new = clone $this;
    $new->id = $id;
    return $new;
}

            
renderToggler() public method

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();
}

            
togglerAsLink() public method

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 <a> tag with role="button". When false, renders as the specified togglerTag (defaults to button).

return self

A new instance with the specified toggler as link setting.

togglerAttributes() public method

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;
}

            
togglerContent() public method

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: `php Toggler::to()->togglerContent('Toggle collapse'); `

                public function togglerContent(string|Stringable $content): self
{
    $new = clone $this;
    $new->togglerContent = (string) $content;
    return $new;
}

            
togglerMultiple() public method

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 .multi-collapse. When false, the toggler will only target the collapse item with the specified ID.

return self

A new instance with the specified toggler multiple setting.

Example usage: `php Toggler::to()->togglerMultiple(true)->ariaControls('collapseOne collapseTwo'); `

                public function togglerMultiple(bool $enabled): self
{
    $new = clone $this;
    $new->togglerMultiple = $enabled;
    return $new;
}

            
togglerTag() public method

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: `php Toggler::to()->togglerTag('a'); `

throws InvalidArgumentException

if the tag name is an empty string.

                public function togglerTag(string $tag): self
{
    $new = clone $this;
    $new->togglerTag = $tag;
    return $new;
}