|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Form\Widget; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
|
8
|
|
|
use Yiisoft\Form\Helper\HtmlForm; |
|
9
|
|
|
use Yiisoft\Html\Tag\Label as LabelTag; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Generates a label tag for the given form attribute. |
|
13
|
|
|
* |
|
14
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html |
|
15
|
|
|
* |
|
16
|
|
|
* @psalm-suppress MissingConstructor |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Label extends AbstractWidget |
|
19
|
|
|
{ |
|
20
|
|
|
private ?string $label = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The id of a labelable form-related element in the same document as the tag label element. |
|
24
|
|
|
* |
|
25
|
|
|
* The first element in the document with an id matching the value of the for attribute is the labeled control for |
|
26
|
|
|
* this label element, if it is a labelable element. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string|null $value The id of a labelable form-related element in the same document as the tag label |
|
29
|
|
|
* element. If null, the attribute will be removed. |
|
30
|
|
|
* |
|
31
|
|
|
* @return static |
|
32
|
|
|
* |
|
33
|
|
|
* @link https://www.w3.org/TR/2012/WD-html-markup-20120329/label.html#label.attrs.for |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function forId(?string $value): self |
|
36
|
|
|
{ |
|
37
|
3 |
|
$new = clone $this; |
|
38
|
3 |
|
$new->attributes['for'] = $value; |
|
39
|
3 |
|
return $new; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* This specifies the label to be displayed. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string|null $value The label to be displayed. |
|
46
|
|
|
* |
|
47
|
|
|
* @return static |
|
48
|
|
|
* |
|
49
|
|
|
* Note that this will NOT be encoded. |
|
50
|
|
|
* - If this is not set, {@see \Yiisoft\Form\FormModel::getAttributeLabel() will be called to get the label for |
|
51
|
|
|
* display (after encoding). |
|
52
|
|
|
*/ |
|
53
|
12 |
|
public function label(?string $value): self |
|
54
|
|
|
{ |
|
55
|
12 |
|
$new = clone $this; |
|
56
|
12 |
|
$new->label = $value; |
|
57
|
12 |
|
return $new; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string the generated label tag. |
|
62
|
|
|
*/ |
|
63
|
166 |
|
protected function run(): string |
|
64
|
|
|
{ |
|
65
|
166 |
|
$new = clone $this; |
|
66
|
|
|
|
|
67
|
166 |
|
if ($new->label === '') { |
|
68
|
155 |
|
$new->label = HtmlForm::getAttributeLabel($new->getFormModel(), $new->getAttribute()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @var string */ |
|
72
|
166 |
|
$forId = ArrayHelper::remove( |
|
73
|
166 |
|
$new->attributes, |
|
74
|
166 |
|
'for', |
|
75
|
166 |
|
HtmlForm::getInputId($new->getFormModel(), $new->getAttribute()) |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
166 |
|
return $new->label !== null |
|
79
|
163 |
|
? LabelTag::tag() |
|
80
|
163 |
|
->attributes($new->attributes) |
|
81
|
163 |
|
->content($new->label) |
|
82
|
163 |
|
->encode($new->getEncode()) |
|
83
|
163 |
|
->forId($forId) |
|
84
|
163 |
|
->render() |
|
85
|
166 |
|
: ''; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|