Final Class Yiisoft\Bootstrap5\NavLink
| Inheritance | Yiisoft\Bootstrap5\NavLink |
|---|
NavLink represents a navigation link item in a Bootstrap Nav component.
Each NavLink can be either active, disabled, or a regular link. The label can be either a string or a Stringable object, and can be optionally encoded.
Example:
`php
// Create a standard nav link
NavLink::to('Home', '/');
// Create an active nav link NavLink::to('Current Page', '#', true);
// Create a disabled nav link NavLink::to('Disabled', '#', false, true);
// Create a tab with content NavLink::tab('Tab 1', 'Content 1');
// Create an active tab with content
NavLink::tab('Tab 2', 'Content 2', true);
`
Public Methods
Method Details
Sets the active state of the nav item.
| public self active ( boolean $enabled ) | ||
| $enabled | boolean |
Whether the nav item is active. |
| return | self |
A new instance with the specified active state. |
|---|---|---|
public function active(bool $enabled): self
{
$new = clone $this;
$new->active = $enabled;
return $new;
}
Sets the HTML attributes for the nav item.
| public self attributes ( array $attributes ) | ||
| $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;
}
Sets the content of the tab pane.
| public self content ( string|\Stringable $content ) | ||
| $content | string|\Stringable |
The content of the tab pane. |
| return | self |
A new instance with the specified content. |
|---|---|---|
public function content(string|Stringable $content): self
{
$new = clone $this;
$new->content = $content;
return $new;
}
Sets the disabled state of the nav item.
| public self disabled ( boolean $disabled ) | ||
| $disabled | boolean |
Whether the nav item is disabled. |
| return | self |
A new instance with the specified disabled state. |
|---|---|---|
public function disabled(bool $disabled): self
{
$new = clone $this;
$new->disabled = $disabled;
return $new;
}
Sets weather to HTML-encode the content.
| public self encodeContent ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to encode the content. |
| return | self |
New instance with the specified encoded setting. |
|---|---|---|
public function encodeContent(bool $enabled): self
{
$new = clone $this;
$new->encodeContent = $enabled;
return $new;
}
Sets weather to HTML-encode the label.
| public self encodeLabel ( boolean $enabled ) | ||
| $enabled | boolean |
Whether to encode the label. |
| return | self |
New instance with the specified encoded setting. |
|---|---|---|
public function encodeLabel(bool $enabled): self
{
$new = clone $this;
$new->encodeLabel = $enabled;
return $new;
}
| public array getAttributes ( ) | ||
| return | array |
The HTML attributes for the nav item. |
|---|---|---|
public function getAttributes(): array
{
return $this->attributes;
}
| public string|\Stringable getContent ( ) | ||
| return | string|\Stringable |
The content of the tab pane. |
|---|---|---|
public function getContent(): string|Stringable
{
return $this->content;
}
| public string getId ( ) |
public function getId(): string
{
/** @psalm-var non-empty-string|null $id */
return match ($this->paneId) {
true => $this->paneAttributes['id'] ?? Html::generateId('pane'),
'', false => throw new InvalidArgumentException('The tab pane ID must be specified.'),
default => $this->paneId,
};
}
| public string|\Stringable getLabel ( ) | ||
| return | string|\Stringable |
The label of Stringable object. |
|---|---|---|
public function getLabel(): string|Stringable
{
return $this->label;
}
| public array getPaneAttributes ( ) | ||
| return | array |
The HTML attributes for the tab pane. |
|---|---|---|
public function getPaneAttributes(): array
{
return $this->paneAttributes;
}
| public string|null getUrl ( ) | ||
| return | string|null |
The URL of the nav item. |
|---|---|---|
public function getUrl(): string|null
{
return $this->url;
}
| public array getUrlAttributes ( ) | ||
| return | array |
The HTML attributes for the nav item link. |
|---|---|---|
public function getUrlAttributes(): array
{
return $this->urlAttributes;
}
| public boolean hasContent ( ) | ||
| return | boolean |
Whether the nav item has content. |
|---|---|---|
public function hasContent(): bool
{
return $this->content !== '';
}
| public boolean isActive ( ) | ||
| return | boolean |
Whether the nav item is active. |
|---|---|---|
public function isActive(): bool
{
return $this->active;
}
| public boolean isDisabled ( ) | ||
| return | boolean |
Whether the nav item is disabled. |
|---|---|---|
public function isDisabled(): bool
{
return $this->disabled;
}
| public boolean isVisible ( ) | ||
| return | boolean |
Whether the nav item is visible. |
|---|---|---|
public function isVisible(): bool
{
return $this->visible;
}
Sets the label text for the nav item.
| public self label ( string|\Stringable $label ) | ||
| $label | string|\Stringable |
The label text or Stringable object |
| return | self |
New instance with the specified label text. |
|---|---|---|
public function label(string|Stringable $label): self
{
$new = clone $this;
$new->label = $label;
return $new;
}
Sets the HTML attributes for the tab pane.
See also \Yiisoft\Html\Html::renderTagAttributes() for details on how attributes are rendered.
| public self paneAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
New instance with the specified pane attributes. |
|---|---|---|
public function paneAttributes(array $attributes): self
{
$new = clone $this;
$new->paneAttributes = $attributes;
return $new;
}
Sets the ID of the nav component.
| public self paneId ( boolean|string $id ) | ||
| $id | boolean|string |
The ID of the alert component. If |
| return | self |
A new instance with the specified ID. |
|---|---|---|
public function paneId(bool|string $id): self
{
$new = clone $this;
$new->paneId = $id;
return $new;
}
| public boolean shouldEncodeContent ( ) | ||
| return | boolean |
Whether the content should be encoded. |
|---|---|---|
public function shouldEncodeContent(): bool
{
return $this->encodeContent;
}
| public boolean shouldEncodeLabel ( ) | ||
| return | boolean |
Whether the label should be encoded. |
|---|---|---|
public function shouldEncodeLabel(): bool
{
return $this->encodeLabel;
}
Creates a {@see NavLink} instance for a tab.
| public static self tab ( string|\Stringable $label, string|\Stringable $content, boolean $active = false, boolean $encodeLabel = true, boolean $encodeContent = true, array $attributes = [], array $urlAttributes = [], boolean|string $paneId = true, array $paneAttributes = [], boolean $visible = true ) | ||
| $label | string|\Stringable |
The label of the tab. |
| $content | string|\Stringable |
The content of the tab pane. |
| $active | boolean |
Whether the tab is active. |
| $encodeLabel | boolean |
Whether the label should be encoded. |
| $encodeContent | boolean |
Whether the content should be encoded. |
| $attributes | array |
The HTML attributes for the nav item. |
| $urlAttributes | array |
The HTML attributes for the nav item link. |
| $paneId | boolean|string | |
| $paneAttributes | array |
The HTML attributes for the tab pane. |
| $visible | boolean |
Whether the nav item is visible. |
| return | self |
A new instance with the specified attributes. |
|---|---|---|
| throws | InvalidArgumentException |
If the tab is both active and disabled. |
public static function tab(
string|Stringable $label,
string|Stringable $content,
bool $active = false,
bool $encodeLabel = true,
bool $encodeContent = true,
array $attributes = [],
array $urlAttributes = [],
bool|string $paneId = true,
array $paneAttributes = [],
bool $visible = true,
): self {
return new self(
active: $active,
attributes: $attributes,
encodeLabel: $encodeLabel,
encodeContent: $encodeContent,
label: $label,
url: '#',
urlAttributes: $urlAttributes,
visible: $visible,
content: $content,
paneId: $paneId,
paneAttributes: $paneAttributes,
);
}
Creates a {@see NavLink} instance.
| public static self to ( string|\Stringable $label = '', string|null $url = null, boolean $active = false, boolean $disabled = false, boolean $encodeLabel = true, array $attributes = [], array $urlAttributes = [], boolean $visible = true ) | ||
| $label | string|\Stringable |
The label of the link. |
| $url | string|null |
The URL of the link. |
| $active | boolean |
Whether the link is active. |
| $disabled | boolean |
Whether the link is disabled. |
| $encodeLabel | boolean |
Whether the label should be encoded. |
| $attributes | array |
The HTML attributes for the nav item. |
| $urlAttributes | array |
The HTML attributes for the nav item link. |
| $visible | boolean |
Whether the nav item is visible. |
| return | self |
A new instance with the specified attributes. |
|---|---|---|
| throws | InvalidArgumentException |
If the link is both active and disabled. |
public static function to(
string|Stringable $label = '',
string|null $url = null,
bool $active = false,
bool $disabled = false,
bool $encodeLabel = true,
array $attributes = [],
array $urlAttributes = [],
bool $visible = true,
): self {
if ($active && $disabled) {
throw new InvalidArgumentException('A nav link cannot be both active and disabled.');
}
return new self(
active: $active,
attributes: $attributes,
encodeLabel: $encodeLabel,
disabled: $disabled,
label: $label,
url: $url,
urlAttributes: $urlAttributes,
visible: $visible,
);
}
Sets the URL for the nav item.
| public self url ( string|null $url ) | ||
| $url | string|null |
The URL or |
| return | self |
New instance with the specified URL. |
|---|---|---|
public function url(string|null $url): self
{
$new = clone $this;
$new->url = $url;
return $new;
}
Sets HTML attributes for the nav item link.
See also \Yiisoft\Html\Html::renderTagAttributes() for details on how attributes are rendered.
| public self urlAttributes ( array $attributes ) | ||
| $attributes | array |
Attribute values indexed by attribute names. |
| return | self |
New instance with the specified link attributes. |
|---|---|---|
public function urlAttributes(array $attributes): self
{
$new = clone $this;
$new->urlAttributes = $attributes;
return $new;
}
Signup or Login in order to comment.