ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
8 
56 function base()
57 {
58  //Step 0: Declare dependencies
59  global $DIC;
60  $ui = $DIC->ui()->factory();
61  $renderer = $DIC->ui()->renderer();
62  $df = new \ILIAS\Data\Factory();
63  $refinery = $DIC['refinery'];
64  $request = $DIC->http()->request();
65  $query = $DIC->http()->wrapper()->query();
66 
67  $here_uri = $df->uri($request->getUri()->__toString());
68  $url_builder = new URLBuilder($here_uri);
69  $example_namespace = ['input', 'switchable_group'];
70  list($url_builder, $example_name) = $url_builder->acquireParameters($example_namespace, "example_name");
71  $url_builder = $url_builder->withParameter($example_name, "standard");
72 
73  //Step 1: Define the groups (with their fields and a label each)
74  $group1 = $ui->input()->field()->group(
75  [
76  "field_1_1" => $ui->input()->field()->text("Item 1.1", "Just some field"),
77  "field_1_2" => $ui->input()->field()->text("Item 1.2", "Just some other field"),
78  "field_1_3" => $ui->input()->field()->datetime("Item 1.3", "a date")->withFormat($df->dateFormat()->germanShort())
79  ],
80  "Switchable Group number one (with numeric key)"
81  );
82  $group2 = $ui->input()->field()->group(
83  [
84  "field_2_1" => $ui->input()->field()->text("Item 2", "Just another field")
85  ->withValue('some val')
86  ],
87  "Switchable Group number two",
88  "with byline"
89  );
90  $group3 = $ui->input()->field()->group([], 'No items in this group', 'but a byline');
91 
92  //Step 2: Switchable Group - one or the other:
93  $sg = $ui->input()->field()->switchableGroup(
94  [
95  "1" => $group1,
96  "g2" => $group2,
97  "g3" => $group3
98  ],
99  "Pick One",
100  "...or the other"
101  );
102 
103  $form_action = $url_builder->buildURI()->__toString();
104  $form = $ui->input()->container()->form()->standard(
105  $form_action,
106  [
107  'switchable_group' => $sg,
108  'switchable_group_required' => $sg->withRequired(true),
109  'switchable_group_preset' => $sg->withValue("g2")
110  ->withLabel("Again, Pick One")
111  ->withByline("... or the other.
112  Second option is selected by default here.")
113  ]
114  );
115 
116  //Step 3: implement some form data processing.
117  if ($query->has($example_name->getName())
118  && $query->retrieve($example_name->getName(), $refinery->custom()->transformation(fn($v) => $v === 'standard'))
119  ) {
120  $form = $form->withRequest($request);
121  $result = $form->getData();
122  } else {
123  $result = "No result yet.";
124  }
125 
126  //Step 4: Render.
127  return
128  "<pre>" . htmlspecialchars(print_r($result, true), ENT_QUOTES) . "</pre><br/>" .
129  $renderer->render($form);
130 }
$renderer
base()
description: > Example showing how a dependent group (aka sub form) might be attached to a radio...
Definition: base.php:56
global $DIC
Definition: shib_login.php:25
URLBuilder.
Definition: URLBuilder.php:39