ILIAS  trunk Revision v11.0_alpha-1851-ga8564da6fed
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
31 {
35  public function render(Component\Component $component, RendererInterface $default_renderer): string
36  {
37  if ($component instanceof Component\Dropdown\Dropdown) {
38  return $this->renderDropdown($component, $default_renderer);
39  }
40 
41  $this->cannotHandleComponent($component);
42  }
43 
44  protected function renderDropdown(Dropdown $component, RendererInterface $default_renderer): string
45  {
46  // get template
47  $tpl_name = "tpl.standard.html";
48  $tpl = $this->getTemplate($tpl_name, true, true);
49 
50  // render items
51  $items = $component->getItems();
52  if (is_array($items) && count($items) == 0) {
53  return "";
54  }
55  $this->renderItems($items, $tpl, $default_renderer);
56 
57  // render trigger button
58  $label = $component->getLabel();
59  if ($label !== null) {
60  $tpl->setVariable("LABEL", $component->getLabel());
61  } else {
62  $tpl->setVariable("LABEL", "");
63  }
64 
65  // ensure that a) a separate aria label may be provided and
66  // b) that an empty label and empty aria-label will use the "actions" fallback
67  if ($component->getLabel() == "" || $component->getAriaLabel() != "") {
68  $aria_label = ($component->getAriaLabel() != "")
69  ? $component->getAriaLabel()
70  : $this->txt("actions");
71  $tpl->setCurrentBlock("aria_label");
72  $tpl->setVariable("ARIA_LABEL", $aria_label);
73  $tpl->parseCurrentBlock();
74  }
75 
76  $component = $component->withAdditionalOnLoadCode(
77  fn($id) =>
78  "il.UI.dropdown.init(document.getElementById(\"$id\"));"
79  );
80 
81  $this->renderId($component, $tpl);
82 
83  return $tpl->get();
84  }
85 
86  protected function renderItems(array $items, Template $tpl, RendererInterface $default_renderer): void
87  {
88  foreach ($items as $item) {
89  $tpl->setCurrentBlock("item");
90  $tpl->setVariable("ITEM", $default_renderer->render($item));
91  $tpl->parseCurrentBlock();
92  }
93  }
94 
95 
96  protected function renderId(
97  JavaScriptBindable $component,
98  Template $tpl
99  ): void {
100  $id = $this->bindJavaScript($component);
101  if ($id === null) {
102  $id = $this->createId();
103  }
104  $tpl->setVariable("ID", $id);
105  $tpl->setVariable("ID_MENU", $id . "_menu");
106 
107  }
108 
112  public function registerResources(ResourceRegistry $registry): void
113  {
114  parent::registerResources($registry);
115  $registry->register('assets/js/dropdown.js');
116  }
117 }
Registry for resources required by rendered output like Javascript or CSS.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.
Definition: Renderer.php:112
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
txt(string $id)
Get a text from the language file.
renderDropdown(Dropdown $component, RendererInterface $default_renderer)
Definition: Renderer.php:44
setCurrentBlock(string $name)
Set the block to work on.
setVariable(string $name, $value)
Set a variable in the current block.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
render(Component\Component $component, RendererInterface $default_renderer)
Definition: Renderer.php:35
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
cannotHandleComponent(Component $component)
This method MUST be called by derived component renderers, if.
renderId(JavaScriptBindable $component, Template $tpl)
Definition: Renderer.php:96
register(string $name)
Add a dependency.
parseCurrentBlock()
Parse the block that is currently worked on.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderItems(array $items, Template $tpl, RendererInterface $default_renderer)
Definition: Renderer.php:86
This implements commonalities between different types of Dropdowns.
Definition: Dropdown.php:35
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.