Skip to content

Commit 90fa144

Browse files
committed
Render the date picker icon the way Contao does
The template assembled the tag by hand, with the size fixed at 20 pixels and an inline "vertical-align:-6px" tuned to it. Anything that resizes the icon put it out of line, and an inline style is beyond the reach of a stylesheet. Going through the image event instead brings along what was missing: the size comes from the file, a dark companion is looked for, the tooltip finds its target, the icon carries a label, and the pointer says it can be clicked.
1 parent 28cf7e0 commit 90fa144

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/Contao/View/Contao2BackendView/ContaoWidgetManager.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
use ContaoCommunityAlliance\DcGeneral\Exception\DcGeneralRuntimeException;
5050
use ContaoCommunityAlliance\DcGeneral\InputProviderInterface;
5151
use ContaoCommunityAlliance\DcGeneral\SessionStorageInterface;
52+
use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
53+
use ContaoCommunityAlliance\Contao\Bindings\Events\Image\GenerateHtmlEvent;
5254
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5355
use Symfony\Contracts\Translation\TranslatorInterface;
5456

@@ -549,6 +551,7 @@ public function renderWidget($property, $ignoreErrors = false, ?PropertyValueBag
549551
->set('widget', $isHideInput ? null : $widget->parse())
550552
->set('hasErrors', $isHideInput ? null : $widget->hasErrors())
551553
->set('strDatepicker', $isHideInput ? null : $this->getDatePicker($propInfo->getExtra(), $widget))
554+
->set('datepickerIcon', $this->renderDatePickerIcon($widget->id))
552555
// We used the var blnUpdate before.
553556
->set('blnUpdate', false)
554557
->set('strHelp', $isHideInput ? '' : $this->generateHelpText($property, $widget))
@@ -753,4 +756,30 @@ protected function getDatePicker(array $propExtra, Widget $widget)
753756

754757
return '';
755758
}
759+
760+
/**
761+
* Render the icon that opens the date picker.
762+
*
763+
* Through the image event rather than assembled by hand: only there does Contao get to look
764+
* for a "--dark" companion and take the size from the file, and only there does the tooltip
765+
* find its target. Contao core renders the very same icon this way.
766+
*
767+
* @param string $widgetId The id of the widget the picker belongs to.
768+
*
769+
* @return string
770+
*/
771+
private function renderDatePickerIcon(string $widgetId): string
772+
{
773+
$dispatcher = $this->getEnvironment()->getEventDispatcher();
774+
assert($dispatcher instanceof EventDispatcherInterface);
775+
776+
$event = new GenerateHtmlEvent(
777+
'assets/datepicker/images/icon.svg',
778+
$this->translator->trans('MSC.datepicker', [], 'contao_default'),
779+
'id="toggle_' . $widgetId . '" style="cursor:pointer" data-contao--tooltips-target="tooltip"'
780+
);
781+
$dispatcher->dispatch($event, ContaoEvents::IMAGE_GET_HTML);
782+
783+
return $event->getHtml() ?? '';
784+
}
756785
}

src/Resources/contao/templates/dcbe_general_field.html5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ assert($translator instanceof \Symfony\Contracts\Translation\TranslatorInterface
1616
<?php else: ?>
1717
<?= $this->widget ?>
1818
<?php if($this->strDatepicker): ?>
19-
<img src="/assets/datepicker/images/icon.svg" width="20" height="20" id="toggle_<?= $this->strId ?>" style="vertical-align:-6px;">
19+
<?= $this->datepickerIcon ?>
2020
<?php /* Run inline like Contao core does - the input exists at this point and a ready-event would not fire again on Turbo navigations. */ ?>
2121
<script><?= $this->strDatepicker; ?></script>
2222
<?php endif; ?>

0 commit comments

Comments
 (0)