ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Link.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
21 
22 use ILIAS\UI\Component as C;
24 use ILIAS\Data\URI;
26 use ilLanguage;
27 
31 class Link extends Group implements C\Input\Field\Link
32 {
33  public function __construct(
34  DataFactory $data_factory,
37  Factory $field_factory,
38  string $label,
39  ?string $byline
40  ) {
41  $inputs = [
42  $field_factory->text($lng->txt('ui_link_label'), null)->withDedicatedName('label'),
43  $field_factory->url($lng->txt('ui_link_url'), null)->withDedicatedName('url')
44  ];
45 
46  parent::__construct($data_factory, $refinery, $lng, $inputs, $label, $byline);
47  $this->addValidation();
48  $this->addTransformation();
49  }
50 
51  protected function addValidation(): void
52  {
53  $txt_id = 'label_cannot_be_empty_if_url_is_set';
54  $error = fn (callable $txt, $value) => $txt($txt_id, $value);
55  $is_ok = function ($v) {
56  list($label, $url) = $v;
57  return (
58  (is_null($label) || $label === '') &&
59  is_null($url)
60  ) || (
61  !is_null($label) && !is_null($url)
62  && strlen($label) > 0
63  && is_a($url, URI::class)
64  );
65  };
66 
67  $label_is_set_for_url = $this->refinery->custom()->constraint($is_ok, $error);
68  $this->setAdditionalTransformation($label_is_set_for_url);
69  }
70 
71 
72  protected function addTransformation(): void
73  {
74  $trafo = $this->refinery->custom()->transformation(function ($v): ?\ILIAS\Data\Link {
75  list($label, $url) = $v;
76  if (is_null($url) || $url === "") {
77  return null;
78  }
79  return $this->data_factory->link($label ?? "", $url);
80  });
81 
82  $this->setAdditionalTransformation($trafo);
83  }
84 
88  protected function isClientSideValueOk($value): bool
89  {
90  return true;
91  }
92 
96  protected function getConstraintForRequirement(): ?Constraint
97  {
98  if ($this->requirement_constraint !== null) {
99  return $this->requirement_constraint;
100  }
101 
102  return null;
103  }
104 }
This implements the group input.
Definition: Group.php:42
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
$lng
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withDedicatedName(string $dedicated_name)
Sets an optional dedicated name for this input which is used in the NAME attribute of the rendered in...
$txt
Definition: error.php:13
__construct(Container $dic, ilPlugin $plugin)
ilErrorHandling $error
Definition: class.ilias.php:55
Refinery Factory $refinery