ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Renderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 use LogicException;
29 
31 {
35  public function render(Component\Component $component, RendererInterface $default_renderer): string
36  {
37  $this->checkComponent($component);
38 
39  if ($component instanceof Component\Link\Standard) {
40  return $this->renderStandard($component);
41  }
42  if ($component instanceof Component\Link\Bulky) {
43  return $this->renderBulky($component, $default_renderer);
44  }
45  throw new LogicException("Cannot render: " . get_class($component));
46  }
47 
48  protected function setStandardVars(
49  string $tpl_name,
50  Component\Link\Link $component
51  ): Template {
52  $tpl = $this->getTemplate($tpl_name, true, true);
53  $action = $component->getAction();
54  $label = $component->getLabel();
55  if ($component->getOpenInNewViewport()) {
56  $tpl->touchBlock("open_in_new_viewport");
57  }
58  if (null !== $component->getContentLanguage()) {
59  $tpl->setVariable("CONTENT_LANGUAGE", $component->getContentLanguage());
60  }
61  if (null !== $component->getLanguageOfReferencedResource()) {
62  $tpl->setVariable("HREF_LANGUAGE", $component->getLanguageOfReferencedResource());
63  }
64 
65  $rel_strings = [];
66  foreach ($component->getRelationshipsToReferencedResource() as $rel) {
67  $rel_strings[] = $rel->value;
68  }
69  if (!empty($rel_strings)) {
70  $tpl->setVariable("RELS", implode(' ', $rel_strings));
71  }
72 
73  $tpl->setVariable("LABEL", $label);
74  $tpl->setVariable("HREF", $action);
75  return $tpl;
76  }
77 
78  protected function maybeRenderWithTooltip(Component\Link\Link $component, Template $tpl): string
79  {
80  $tooltip_embedding = $this->getTooltipRenderer()->maybeGetTooltipEmbedding(...$component->getHelpTopics());
81  if (! $tooltip_embedding) {
82  $id = $this->bindJavaScript($component);
83  $tpl->setVariable("ID", $id);
84  return $tpl->get();
85  }
86  $component = $component->withAdditionalOnLoadCode($tooltip_embedding[1]);
87  $tooltip_id = $this->createId();
88  $tpl->setCurrentBlock("with_aria_describedby");
89  $tpl->setVariable("ARIA_DESCRIBED_BY", $tooltip_id);
90  $tpl->parseCurrentBlock();
91  $id = $this->bindJavaScript($component);
92  $tpl->setVariable("ID", $id);
93  return $tooltip_embedding[0]($tooltip_id, $tpl->get());
94  }
95 
96  protected function renderStandard(
97  Component\Link\Standard $component
98  ): string {
99  $tpl_name = "tpl.standard.html";
100  $tpl = $this->setStandardVars($tpl_name, $component);
101  return $this->maybeRenderWithTooltip($component, $tpl);
102  }
103 
104  protected function renderBulky(
105  Component\Link\Bulky $component,
106  RendererInterface $default_renderer
107  ): string {
108  $tpl_name = "tpl.bulky.html";
109  $tpl = $this->setStandardVars($tpl_name, $component);
110  $renderer = $default_renderer->withAdditionalContext($component);
111  $tpl->setVariable("SYMBOL", $renderer->render($component->getSymbol()));
112 
113  $aria_role = $component->getAriaRole();
114  if ($aria_role != null) {
115  $tpl->setCurrentBlock("with_aria_role");
116  $tpl->setVariable("ARIA_ROLE", $aria_role);
117  $tpl->parseCurrentBlock();
118  }
119  return $this->maybeRenderWithTooltip($component, $tpl);
120  }
121 
125  protected function getComponentInterfaceName(): array
126  {
127  return [
128  Component\Link\Standard::class,
129  Component\Link\Bulky::class
130  ];
131  }
132 }
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCurrentBlock(string $name)
Set the block to work on.
setVariable(string $name, $value)
Set a variable in the current block.
getTemplate(string $name, bool $purge_unfilled_vars, bool $purge_unused_blocks)
Get template of component this renderer is made for.
get(string $block=null)
Get the rendered template or a specific block.
parseCurrentBlock()
Parse the block that is currently worked on.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.