HasLabelCollection
API reference › HTML Mixin › HasLabelCollection
Provides an immutable API for label content and label attributes.
See also: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label.
Methods
Section titled “Methods”| Method | Description | Defined 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 |
Method details
Section titled “Method details”getLabel()
Section titled “getLabel() ”public function getLabel(): stringReturns the label content.
Returns
string: Label content.
getLabelAttribute()
Section titled “getLabelAttribute() ”public function getLabelAttribute(string|UnitEnum $key, mixed $default = null, string $prefix = ''): mixedReturns one label attribute value.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name. |
$default | mixed | Default value when the attribute is missing. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
mixed: Attribute value, or $default when missing.
getLabelAttributes()
Section titled “getLabelAttributes() ”public function getLabelAttributes(): arrayReturns label attributes.
Returns
array: Label attributes.
isLabel()
Section titled “isLabel() ”public function isLabel(): boolReturns whether the label element should be rendered.
Returns
bool: true if the label element should be rendered, or false to skip rendering.
label()
Section titled “label() ”public function label(string|Stringable $content): staticSets the label content.
Parameters
| Parameter | Type | Description |
|---|---|---|
$content | string|Stringable | Label content. |
Returns
static: New instance with the updated label value.
labelAttributes()
Section titled “labelAttributes() ”public function labelAttributes(array $attributes, string $prefix = ''): staticSets the label attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
$attributes | array | Label attributes. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
static: New instance with the updated labelAttributes value.
labelClass()
Section titled “labelClass() ”public function labelClass(string|Stringable|UnitEnum|null $value, bool $override = false): staticAdds 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
| Parameter | Type | Description |
|---|---|---|
$value | string|Stringable|UnitEnum| | CSS class value, or null to preserve existing classes. |
$override | bool | Whether to replace existing classes (true) or merge (false) when valid classes are supplied. |
Returns
static: New instance with the updated labelAttributes['class'] value.
labelFor()
Section titled “labelFor() ”public function labelFor(string|null $value): staticSets the for attribute of the label.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string| | for attribute value. |
Returns
static: New instance with the updated labelAttributes['for'] value.
labelRemoveAttribute()
Section titled “labelRemoveAttribute() ”public function labelRemoveAttribute(string|UnitEnum $key, string $prefix = ''): staticRemoves a specific label attribute.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name to remove. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
static: New instance without the specified label attribute.
labelSetAttribute()
Section titled “labelSetAttribute() ”public function labelSetAttribute(string|UnitEnum $key, mixed $value, string $prefix = ''): staticSets a single HTML attribute for the label element.
Parameters
| Parameter | Type | Description |
|---|---|---|
$key | string|UnitEnum | Attribute name. |
$value | mixed | Attribute value. |
$prefix | string | Prefix to ensure (for example, aria-, data-, on). |
Returns
static: New instance with the updated labelAttributes value.
notLabel()
Section titled “notLabel() ”public function notLabel(bool $value = true): staticSets whether to skip rendering the label.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | bool | Whether to skip rendering the label, or false to re-enable a suppressed label. |
Returns
static: New instance with the updated notLabel value.