Debian Codegraph @

vendor/ipl/web/src/Widget/ButtonLink.php

raw
1 <?php
2
3 namespace ipl\Web\Widget;
4
5 use ipl\Html\Attribute;
6
7 /**
8 * Button like link generally pointing to CRUD actions
9 */
10 class ButtonLink extends ActionLink
11 {
12 protected $defaultAttributes = [
13 'role' => 'button',
14 'class' => 'button-link',
15 'data-base-target' => '_main'
16 ];
17
18 /** @var ?string The explanation why the button is disabled */
19 protected ?string $disabledExplanation = null;
20
21 /**
22 * Disable the button with explanation
23 *
24 * @param string $explanation Why the button is disabled
25 *
26 * @return $this
27 */
28 public function disable(string $explanation): self
29 {
30 $this->disabledExplanation = $explanation;
31
32 return $this;
33 }
34
35 public function createHrefAttribute(): ?Attribute
36 {
37 if ($this->disabledExplanation) {
38 return null;
39 }
40
41 return parent::createHrefAttribute();
42 }
43
44 protected function assemble()
45 {
46 parent::assemble();
47
48 if ($this->disabledExplanation) {
49 $this->addAttributes([
50 'aria-disabled' => 'true',
51 'title' => $this->disabledExplanation,
52 ]);
53 }
54 }
55 }