0 follower

Final Class Yiisoft\Bootstrap5\Alert

InheritanceYiisoft\Bootstrap5\Alert » Yiisoft\Widget\Widget

Alert renders an alert bootstrap component.

For example,

<?= Alert::widget()->body('Say hello...')->variant(AlertVariant::PRIMARY) ?>

Public Methods

Hide inherited methods

Method Description Defined By
addAttributes() Adds a sets of attributes. Yiisoft\Bootstrap5\Alert
addClass() Adds one or more CSS classes to the existing classes. Yiisoft\Bootstrap5\Alert
addCloseButtonAttribute() Adds close button attribute value. Yiisoft\Bootstrap5\Alert
addCloseButtonClass() Adds one or more CSS classes to the existing close button classes. Yiisoft\Bootstrap5\Alert
addCloseButtonCssStyle() Adds a close button CSS style. Yiisoft\Bootstrap5\Alert
addCssStyle() Adds a CSS style. Yiisoft\Bootstrap5\Alert
attribute() Adds a sets attribute value. Yiisoft\Bootstrap5\Alert
attributes() Sets the HTML attributes. Yiisoft\Bootstrap5\Alert
body() Sets the body content. Yiisoft\Bootstrap5\Alert
class() Replaces all existing CSS classes with the specified one(s). Yiisoft\Bootstrap5\Alert
closeButtonAttributes() Sets the HTML attributes for the close button. Yiisoft\Bootstrap5\Alert
closeButtonLabel() Sets the label for the close button. Yiisoft\Bootstrap5\Alert
closeButtonTag() Sets the HTML tag to be used for the close button. Yiisoft\Bootstrap5\Alert
dismissable() Makes the alert dismissible by adding a close button. Yiisoft\Bootstrap5\Alert
fade() Adds a fade effect. Yiisoft\Bootstrap5\Alert
header() Sets the header content. Yiisoft\Bootstrap5\Alert
headerAttributes() Sets the HTML attributes for the header section. Yiisoft\Bootstrap5\Alert
headerTag() Sets the HTML tag to be used for the header section. Yiisoft\Bootstrap5\Alert
id() Sets the ID. Yiisoft\Bootstrap5\Alert
render() Run the widget. Yiisoft\Bootstrap5\Alert
templateContent() Sets the template content to be used for rendering. Yiisoft\Bootstrap5\Alert
variant() Set the alert variant. Yiisoft\Bootstrap5\Alert

Constants

Hide inherited constants

Constant Value Description Defined By
CLASS_CLOSE_BUTTON 'btn-close' Yiisoft\Bootstrap5\Alert
NAME 'alert' Yiisoft\Bootstrap5\Alert

Method Details

Hide inherited methods

addAttributes() public method

Adds a sets of attributes.

public self addAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names. for example, ['id' => 'my-id'].

return self

A new instance with the specified attributes added.

Example usage: `php $alert->addAttributes(['data-id' => '123']); `

                public function addAttributes(array $attributes): self
{
    $new = clone $this;
    $new->attributes = [...$this->attributes, ...$attributes];
    return $new;
}

            
addClass() public method

Adds one or more CSS classes to the existing classes.

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self addClass ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or more CSS class names to add. Pass null to skip adding a class.

return self

A new instance with the specified CSS classes added to existing ones.

                public function addClass(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    $new->cssClasses = [...$this->cssClasses, ...$class];
    return $new;
}

            
addCloseButtonAttribute() public method

Adds close button attribute value.

public self addCloseButtonAttribute ( string $name, mixed $value )
$name string

The attribute name.

$value mixed

The attribute value.

return self

A new instance with the specified attribute added.

Example usage: `php $alert->addCloseButtonAttribute('data-id', '123'); `

                public function addCloseButtonAttribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->closeButtonAttributes[$name] = $value;
    return $new;
}

            
addCloseButtonClass() public method

Adds one or more CSS classes to the existing close button classes.

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self addCloseButtonClass ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or more CSS class names to add. Pass null to skip adding a class.

return self

A new instance with the specified CSS classes added to existing ones.

                public function addCloseButtonClass(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    foreach ($class as $item) {
        Html::addCssClass($new->closeButtonAttributes, $item);
    }
    return $new;
}

            
addCloseButtonCssStyle() public method

Adds a close button CSS style.

public self addCloseButtonCssStyle ( array|string $style, boolean $overwrite true )
$style array|string

The CSS style. If the value is an array, a space will separate the values. For example, ['color' => 'red', 'font-weight' => 'bold'] will be rendered as color: red; font-weight: bold;. If it is a string, it will be added as is, for example, color: red.

$overwrite boolean

Whether to overwrite existing styles with the same name. If false, the new value will be appended to the existing one.

return self

A new instance with the specified CSS style value added.

Example usage: `php $alert->addCloseButtonCssStyle('color: red');

// or $alert->addCloseButtonCssStyle(['color' => 'red', 'font-weight' => 'bold']); `

                public function addCloseButtonCssStyle(array|string $style, bool $overwrite = true): self
{
    $new = clone $this;
    Html::addCssStyle($new->closeButtonAttributes, $style, $overwrite);
    return $new;
}

            
addCssStyle() public method

Adds a CSS style.

public self addCssStyle ( array|string $style, boolean $overwrite true )
$style array|string

The CSS style. If the value is an array, a space will separate the values. For example, ['color' => 'red', 'font-weight' => 'bold'] will be rendered as color: red; font-weight: bold;. If it is a string, it will be added as is, for example, color: red.

$overwrite boolean

Whether to overwrite existing styles with the same name. If false, the new value will be appended to the existing one.

return self

A new instance with the specified CSS style value added.

Example usage: `php $alert->addCssStyle('color: red');

// or $alert->addCssStyle(['color' => 'red', 'font-weight' => 'bold']); `

                public function addCssStyle(array|string $style, bool $overwrite = true): self
{
    $new = clone $this;
    Html::addCssStyle($new->attributes, $style, $overwrite);
    return $new;
}

            
attribute() public method

Adds a sets attribute value.

public self attribute ( string $name, mixed $value )
$name string

The attribute name.

$value mixed

The attribute value.

return self

A new instance with the specified attribute added.

Example usage: `php $alert->attribute('data-id', '123'); `

                public function attribute(string $name, mixed $value): self
{
    $new = clone $this;
    $new->attributes[$name] = $value;
    return $new;
}

            
attributes() public method

Sets the HTML attributes.

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

            
body() public method

Sets the body content.

public self body ( string|\Stringable $content, boolean $encode true )
$content string|\Stringable

The body content.

$encode boolean

Whether the body value should be HTML-encoded. Use this when rendering user-generated content to prevent XSS attacks.

return self

A new instance with the specified body content.

Example usage: `php $alert->body('Say hello...'); `

                public function body(string|Stringable $content, bool $encode = true): self
{
    if ($encode) {
        $content = Html::encode($content);
    }
    $new = clone $this;
    $new->body = $content;
    return $new;
}

            
class() public method

Replaces all existing CSS classes with the specified one(s).

Multiple classes can be added by passing them as separate arguments. null values are filtered out automatically.

public self class ( \BackedEnum|string|null $class )
$class \BackedEnum|string|null

One or more CSS class names to set. Pass null to skip setting a class.

return self

A new instance with the specified CSS classes set.

Example usage: `php $alert->class('custom-class', null, 'another-class', BackGroundColor::PRIMARY); `

                public function class(BackedEnum|string|null ...$class): self
{
    $new = clone $this;
    $new->cssClasses = $class;
    return $new;
}

            
closeButtonAttributes() public method

Sets the HTML attributes for the close button.

public self closeButtonAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified HTML attributes for the close button.

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

            
closeButtonLabel() public method

Sets the label for the close button.

public self closeButtonLabel ( string $label )
$label string

The label for the close button.

return self

A new instance with the specified close button label.

Example usage: `php $alert->closeButtonLabel('Close'); `

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

            
closeButtonTag() public method

Sets the HTML tag to be used for the close button.

public self closeButtonTag ( string $tag )
$tag string

The HTML tag name for the close button.

return self

A new instance with the specified close button tag.

Example usage: `php $alert->closeButtonTag('button'); `

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

            
dismissable() public method

Makes the alert dismissible by adding a close button.

public self dismissable ( boolean $enabled )
$enabled boolean

Whether to make the alert dismissible.

return self

A new instance with the specified dismissable value.

Example usage: `php $alert->dismissable(true); `

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

            
fade() public method

Adds a fade effect.

public self fade ( boolean $enabled )
$enabled boolean

Whether to add a fade effect.

return self

A new instance with the specified fade value.

Example usage: `php $alert->fade(true); `

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

            
header() public method

Sets the header content.

public self header ( string|null $content, boolean $encode true )
$content string|null

The header content.

$encode boolean

Whether the body value should be HTML-encoded. Use this when rendering user-generated content to prevent XSS attacks.

return self

A new instance with the specified header content.

Example usage: `php $alert->header('Header content'); `

                public function header(string|null $content, bool $encode = true): self
{
    if ($encode) {
        $content = Html::encode($content);
    }
    $new = clone $this;
    $new->header = $content;
    return $new;
}

            
headerAttributes() public method

Sets the HTML attributes for the header section.

public self headerAttributes ( array $attributes )
$attributes array

Attribute values indexed by attribute names.

return self

A new instance with the specified attributes for the header section.

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

            
headerTag() public method

Sets the HTML tag to be used for the header section.

public self headerTag ( string $tag )
$tag string

The HTML tag name for the header section.

return self

A new instance with the specified header tag.

Example usage: `php $alert->headerTag('h4'); `

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

            
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.

Example usage: `php $alert->id('my-id'); `

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

            
render() public method

Run the widget.

public string render ( )
return string

The HTML representation of the element.

                public function render(): string
{
    $attributes = $this->attributes;
    $attributes['role'] = self::NAME;
    $toggler = '';
    $classes = $attributes['class'] ?? null;
    unset($attributes['class'], $attributes['id']);
    Html::addCssClass($attributes, [self::NAME, $this->alertType->value, $classes, ...$this->cssClasses]);
    if ($this->dismissable) {
        Html::addCssClass($attributes, 'alert-dismissible');
        $toggler = $this->renderToggler();
    }
    if ($this->fade) {
        Html::addCssClass($attributes, 'fade show');
    }
    $content = strtr(
        $this->templateContent,
        [
            '{header}' => $this->renderHeader(),
            '{body}' => $this->body,
            '{toggler}' => $toggler,
        ]
    );
    $content = preg_replace("/\n{2}/", "\n", $content) ?? '';
    return Div::tag()->addAttributes($attributes)->content($content)->encode(false)->id($this->getId())->render();
}

            
templateContent() public method

Sets the template content to be used for rendering.

public self templateContent ( string $content )
$content string

The template content string.

return self

A new instance with the specified template content.

Example usage: `php $alert->templateContent("\n{header}\n{body}\n{toggler}\n"); `

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

            
variant() public method

Set the alert variant.

public self variant ( \Yiisoft\Bootstrap5\AlertVariant $variant )
$variant \Yiisoft\Bootstrap5\AlertVariant

The alert variant.

return self

A new instance with the specified alert variant.

Example usage: `php $alert->variant(AlertVariant::PRIMARY); `

                public function variant(AlertVariant $variant): self
{
    $new = clone $this;
    $new->alertType = $variant;
    return $new;
}