Skip to content
PHP Ecosystem
Explore

HasLabelCollection

API reference › HTML Mixin › HasLabelCollection

traitUIAwesome\Html\Mixin\HasLabelCollection
Sourcesrc/HasLabelCollection.php

Provides an immutable API for label content and label attributes.

See also: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label.

MethodDescriptionDefined by
getLabel()Returns the label content.HasLabelCollection
getLabelAttribute()Returns one label attribute value.HasLabelCollection
getLabelAttributes()Returns label attributes.HasLabelCollection
isLabel()Returns whether the label element should be rendered.HasLabelCollection
label()Sets the label content.HasLabelCollection
labelAttributes()Sets the label attributes.HasLabelCollection
labelClass()Adds a CSS class to the label element attributes.HasLabelCollection
labelFor()Sets the for attribute of the label.HasLabelCollection
labelRemoveAttribute()Removes a specific label attribute.HasLabelCollection
labelSetAttribute()Sets a single HTML attribute for the label element.HasLabelCollection
notLabel()Sets whether to skip rendering the label.HasLabelCollection
public function getLabel(): string

Returns the label content.

Returns

string: Label content.

public function getLabelAttribute(string|UnitEnum $key, mixed $default = null, string $prefix = ''): mixed

Returns one label attribute value.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name.
$defaultmixedDefault value when the attribute is missing.
$prefixstringPrefix to ensure (for example, aria-, data-, on).

Returns

mixed: Attribute value, or $default when missing.

public function getLabelAttributes(): array

Returns label attributes.

Returns

array: Label attributes.

public function isLabel(): bool

Returns whether the label element should be rendered.

Returns

bool: true if the label element should be rendered, or false to skip rendering.

public function label(string|Stringable $content): static

Sets the label content.

Parameters

ParameterTypeDescription
$contentstring|StringableLabel content.

Returns

static: New instance with the updated label value.

public function labelAttributes(array $attributes, string $prefix = ''): static

Sets the label attributes.

Parameters

ParameterTypeDescription
$attributesarrayLabel attributes.
$prefixstringPrefix to ensure (for example, aria-, data-, on).

Returns

static: New instance with the updated labelAttributes value.

public function labelClass(string|Stringable|UnitEnum|null $value, bool $override = false): static

Adds a CSS class to the label element attributes.

Passing null preserves existing classes, even when $override is true. Use labelRemoveAttribute('class') to remove the attribute explicitly.

Parameters

ParameterTypeDescription
$valuestring|Stringable|UnitEnum|nullCSS class value, or null to preserve existing classes.
$overrideboolWhether to replace existing classes (true) or merge (false) when valid classes are supplied.

Returns

static: New instance with the updated labelAttributes['class'] value.

public function labelFor(string|null $value): static

Sets the for attribute of the label.

Parameters

ParameterTypeDescription
$valuestring|nullfor attribute value.

Returns

static: New instance with the updated labelAttributes['for'] value.

public function labelRemoveAttribute(string|UnitEnum $key, string $prefix = ''): static

Removes a specific label attribute.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name to remove.
$prefixstringPrefix to ensure (for example, aria-, data-, on).

Returns

static: New instance without the specified label attribute.

public function labelSetAttribute(string|UnitEnum $key, mixed $value, string $prefix = ''): static

Sets a single HTML attribute for the label element.

Parameters

ParameterTypeDescription
$keystring|UnitEnumAttribute name.
$valuemixedAttribute value.
$prefixstringPrefix to ensure (for example, aria-, data-, on).

Returns

static: New instance with the updated labelAttributes value.

public function notLabel(bool $value = true): static

Sets whether to skip rendering the label.

Parameters

ParameterTypeDescription
$valueboolWhether to skip rendering the label, or false to re-enable a suppressed label.

Returns

static: New instance with the updated notLabel value.