0 follower

Final Class Yiisoft\Bootstrap5\DropdownItem

InheritanceYiisoft\Bootstrap5\DropdownItem

DropdownItem represents a single item within a Bootstrap Dropdown menu.

Supports multiple item types:

  • Buttons: Button-style menu items.
  • Dividers: Horizontal separators.
  • Headers: Section headers.
  • Links: Standard clickable menu items.
  • Text: Plain text items.

Example usage: `php // Create a link item DropdownItem::link('Profile', '/profile');

// Create an active item DropdownItem::link('Dashboard', '/dashboard', true);

// Create a divider DropdownItem::divider();

// Create a header DropdownItem::header('Section Title'); `

Public Methods

Hide inherited methods

Method Description Defined By
active() Sets the active state. Yiisoft\Bootstrap5\DropdownItem
attributes() Sets the HTML attributes. Yiisoft\Bootstrap5\DropdownItem
button() Creates a button-type dropdown item. Yiisoft\Bootstrap5\DropdownItem
content() Sets the content. Yiisoft\Bootstrap5\DropdownItem
disabled() Sets the disabled state. Yiisoft\Bootstrap5\DropdownItem
divider() Creates a divider dropdown item. Yiisoft\Bootstrap5\DropdownItem
getAttributes() Yiisoft\Bootstrap5\DropdownItem
getContent() Yiisoft\Bootstrap5\DropdownItem
getHeaderTag() Yiisoft\Bootstrap5\DropdownItem
getItemAttributes() Yiisoft\Bootstrap5\DropdownItem
getType() Yiisoft\Bootstrap5\DropdownItem
getUrl() Yiisoft\Bootstrap5\DropdownItem
header() Creates a header dropdown item. Yiisoft\Bootstrap5\DropdownItem
headerTag() Sets the header tag. Yiisoft\Bootstrap5\DropdownItem
isActive() Yiisoft\Bootstrap5\DropdownItem
isDisabled() Yiisoft\Bootstrap5\DropdownItem
isVisible() Yiisoft\Bootstrap5\DropdownItem
itemAttributes() Sets the HTML attributes for the item. Yiisoft\Bootstrap5\DropdownItem
link() Creates a link-type dropdown item. Yiisoft\Bootstrap5\DropdownItem
listContent() Creates a list with custom content dropdown item. Yiisoft\Bootstrap5\DropdownItem
text() Creates a text dropdown item. Yiisoft\Bootstrap5\DropdownItem
type() Sets the type. Yiisoft\Bootstrap5\DropdownItem
url() Sets the URL. Yiisoft\Bootstrap5\DropdownItem
visible() Sets the visibility of the dropdown item. Yiisoft\Bootstrap5\DropdownItem

Method Details

Hide inherited methods

active() public method

Sets the active state.

public active( boolean $enabled ): self
$enabled boolean

Whether is active or not.

return self

A new instance with the specified active state.

                public function active(bool $enabled): self
{
    if ($enabled && $this->disabled) {
        throw new InvalidArgumentException('The dropdown item cannot be active and disabled at the same time.');
    }
    $new = clone $this;
    $new->active = $enabled;
    return $new;
}

            
attributes() public method

Sets the HTML attributes.

public attributes( array $attributes ): self
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes.

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

            
button() public static method

Creates a button-type dropdown item.

public static button( string|\Stringable $content '', array $attributes = [], array $itemAttributes = [], boolean $visible true ): self
$content string|\Stringable

The button content.

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <button> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::button('Submit', active: true); `

throws InvalidArgumentException

When item is set as both active and disabled.

                public static function button(
    string|Stringable $content = '',
    array $attributes = [],
    array $itemAttributes = [],
    bool $visible = true,
): self {
    return new self(
        DropdownItemType::BUTTON,
        $content,
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
        $visible,
    );
}

            
content() public method

Sets the content.

public content( string|\Stringable $content ): self
$content string|\Stringable

The content.

return self

A new instance with the specified content.

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

            
disabled() public method

Sets the disabled state.

public disabled( boolean $enabled ): self
$enabled boolean

Whether is disabled or not.

return self

A new instance with the specified disabled state.

                public function disabled(bool $enabled): self
{
    if ($enabled && $this->active) {
        throw new InvalidArgumentException('The dropdown item cannot be active and disabled at the same time.');
    }
    $new = clone $this;
    $new->disabled = $enabled;
    return $new;
}

            
divider() public static method

Creates a divider dropdown item.

public static divider( array $attributes = [], array $itemAttributes = [], boolean $visible true ): self
$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <hr> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::divider(); `

                public static function divider(array $attributes = [], array $itemAttributes = [], bool $visible = true): self
{
    return new self(
        DropdownItemType::DIVIDER,
        '',
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
        $visible,
    );
}

            
getAttributes() public method

public getAttributes( ): array
return array

The HTML attributes for the <li> tag.

                public function getAttributes(): array
{
    return $this->attributes;
}

            
getContent() public method

public getContent( ): string|\Stringable
return string|\Stringable

The content.

                public function getContent(): string|Stringable
{
    return $this->content;
}

            
getHeaderTag() public method

public getHeaderTag( ): string
return string

The header tag.

                public function getHeaderTag(): string
{
    return $this->headerTag;
}

            
getItemAttributes() public method

public getItemAttributes( ): array
return array

The HTML attributes for the item.

                public function getItemAttributes(): array
{
    return $this->itemAttributes;
}

            
getType() public method

public getType( ): \Yiisoft\Bootstrap5\DropdownItemType
return \Yiisoft\Bootstrap5\DropdownItemType

The type.

                public function getType(): DropdownItemType
{
    return $this->type;
}

            
getUrl() public method

public getUrl( ): string
return string

The URL.

                public function getUrl(): string
{
    return $this->url;
}

            
header() public static method

Creates a header dropdown item.

public static header( string|\Stringable $content '', string $headerTag 'h6', array $attributes = [], array $itemAttributes = [], boolean $visible true ): self
$content string|\Stringable

The header text.

$headerTag string

The HTML tag to use (defaults to h6).

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <h6> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::header('Section Title'); `

throws InvalidArgumentException

When header tag is empty.

                public static function header(
    string|Stringable $content = '',
    string $headerTag = 'h6',
    array $attributes = [],
    array $itemAttributes = [],
    bool $visible = true,
): self {
    if ($headerTag === '') {
        throw new InvalidArgumentException('The header tag cannot be empty.');
    }
    return new self(
        DropdownItemType::HEADER,
        $content,
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        $headerTag,
        $visible,
    );
}

            
headerTag() public method

Sets the header tag.

public headerTag( string $tag ): self
$tag string

The header tag.

return self

A new instance with the specified header tag.

                public function headerTag(string $tag): self
{
    if ($tag === '') {
        throw new InvalidArgumentException('The header tag cannot be empty.');
    }
    $new = clone $this;
    $new->headerTag = $tag;
    return $new;
}

            
isActive() public method

public isActive( ): boolean
return boolean

Whether the item is active.

                public function isActive(): bool
{
    return $this->active;
}

            
isDisabled() public method

public isDisabled( ): boolean
return boolean

Whether the item is disabled.

                public function isDisabled(): bool
{
    return $this->disabled;
}

            
isVisible() public method

public isVisible( ): boolean
return boolean

Whether the item is visible.

                public function isVisible(): bool
{
    return $this->visible;
}

            
itemAttributes() public method

Sets the HTML attributes for the item.

public itemAttributes( array $attributes ): self
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the item.

                public function itemAttributes(array $attributes): self
{
    $new = clone $this;
    $new->itemAttributes = $attributes;
    return $new;
}

            
link() public static method

Creates a link-type dropdown item.

public static link( string|\Stringable $content '', string $url '#', boolean $active false, boolean $disabled false, array $attributes = [], array $itemAttributes = [], boolean $visible true ): self
$content string|\Stringable

The link text.

$url string

The URL (defaults to '#').

$active boolean

Whether the link is active.

$disabled boolean

Whether the link is disabled.

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <a> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::link('Profile', '/profile'); `

throws InvalidArgumentException

When item is set as both active and disabled.

listContent() public static method

Creates a list with custom content dropdown item.

public static listContent( string|\Stringable $content '', array $attributes = [], boolean $visible true ): self
$content string|\Stringable

The list content.

$attributes array

The HTML attributes for the <li> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::listContent('Simple list item'); `

                public static function listContent(string|Stringable $content = '', array $attributes = [], bool $visible = true): self
{
    return new self(
        DropdownItemType::CUSTOM_CONTENT,
        $content,
        '',
        false,
        false,
        $attributes,
        [],
        'h6',
        $visible,
    );
}

            
text() public static method

Creates a text dropdown item.

public static text( string|\Stringable $content '', array $attributes = [], array $itemAttributes = [], boolean $visible true ): self
$content string|\Stringable

The text content.

$attributes array

The HTML attributes for the <li> tag.

$itemAttributes array

The HTML attributes for the <span> tag.

$visible boolean

Whether the item is visible.

return self

A new instance with the specified configuration.

Example: `php DropdownItem::text('Simple text item'); `

                public static function text(
    string|Stringable $content = '',
    array $attributes = [],
    array $itemAttributes = [],
    bool $visible = true,
): self {
    return new self(
        DropdownItemType::TEXT,
        $content,
        '',
        false,
        false,
        $attributes,
        $itemAttributes,
        'h6',
        $visible,
    );
}

            
type() public method

Sets the type.

public type( \Yiisoft\Bootstrap5\DropdownItemType $type ): self
$type \Yiisoft\Bootstrap5\DropdownItemType

The type.

return self

A new instance with the specified type.

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

            
url() public method

Sets the URL.

public url( string $url ): self
$url string

The URL.

return self

A new instance with the specified URL.

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

            
visible() public method

Sets the visibility of the dropdown item.

public visible( boolean $enabled ): self
$enabled boolean

Whether the dropdown item is visible.

return self

A new instance with the specified visibility.

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