ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
expandable.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
17 function expandable()
18 {
19  global $DIC;
20  $f = $DIC->ui()->factory();
21  $renderer = $DIC->ui()->renderer();
22 
23  $data = [
24  ['label' => 'root', 'children' => [
25  ['label' => '1', 'children' => [
26  ['label' => '1.1', 'children' => [
27  ['label' => '1.1.1', 'children' => []],
28  ['label' => '1.1.2', 'children' => []]
29  ]],
30  ['label' => '1.2', 'children' => []],
31  ['label' => '1.3', 'children' => []]
32  ]],
33  ['label' => '2', 'children' => [
34  ['label' => '2.1', 'children' => []],
35  ]],
36  ['label' => '3', 'children' => [
37  ['label' => '3.1', 'children' => [
38  ['label' => '3.1.1', 'children' => [
39  ['label' => '3.1.1.1', 'children' => []],
40  ]],
41  ]],
42 
43  ]],
44  ]]
45  ];
46 
47  $recursion = new class () implements \ILIAS\UI\Component\Tree\TreeRecursion {
48  public function getChildren($record, $environment = null): array
49  {
50  return $record['children'];
51  }
52 
53  public function build(
54  \ILIAS\UI\Component\Tree\Node\Factory $factory,
55  $record,
56  $environment = null
57  ): \ILIAS\UI\Component\Tree\Node\Node {
58  $label = $record['label'];
59  $node = $factory->simple($label);
60 
61  if (count($record['children']) === 0) {
62  $node = $node->withOnClick($environment['modal']->getShowSignal());
63  }
64 
65  if ($label === "root" || $label === "2") {
66  $node = $node->withExpanded(true);
67  }
68  if ($label === "2") {
69  $node = $node->withHighlighted(true);
70  }
71 
72  return $node;
73  }
74  };
75 
76  $image = $f->image()->responsive("assets/ui-examples/images/Image/mountains.jpg", "Image source: https://stocksnap.io, Creative Commons CC0 license");
77  $page = $f->modal()->lightboxImagePage($image, 'Mountains');
78  $modal = $f->modal()->lightbox($page);
79  $environment = [
80  'modal' => $modal
81  ];
82 
83  $tree = $f->tree()->expandable("Label", $recursion)
84  ->withEnvironment($environment)
85  ->withData($data)
86  ->withHighlightOnNodeClick(true);
87 
88  return $renderer->render([
89  $modal,
90  $tree
91  ]);
92 }
$renderer
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is how the factory for UI elements looks.
Definition: Factory.php:37
expandable()
description: > Example for rendering an expandable tree.
Definition: expandable.php:17