Skip to content

Instantly share code, notes, and snippets.

@fiedsch
Created October 14, 2024 13:29
Show Gist options
  • Save fiedsch/ce48b760af2aa50d63f180c890062b20 to your computer and use it in GitHub Desktop.
Save fiedsch/ce48b760af2aa50d63f180c890062b20 to your computer and use it in GitHub Desktop.
Contao: Pick and display individual events
<?php
# contao/config/config.php
use App\ModulePickedEventlist;
$GLOBALS['FE_MOD']['events']['pickedeventlist'] = ModulePickedEventlist::class;
<?php
# contao/dca/tl_module.php
use Contao\CoreBundle\DataContainer\PaletteManipulator;
//$GLOBALS['TL_DCA']['tl_module']['palettes']['pickedeventlist'] = '{title_legend},name,headline,type;{config_legend},cal_calendar,cal_noSpan,cal_format,cal_featured,cal_order,cal_readerModule,cal_limit,perPage,cal_ignoreDynamic,cal_hideRunning;{template_legend:hide},cal_template,customTpl;{image_legend:hide},imgSize;{protected_legend:hide},protected;{expert_legend:hide},cssID;{events_legend},pickedevents';
$GLOBALS['TL_DCA']['tl_module']['palettes']['pickedeventlist'] = $GLOBALS['TL_DCA']['tl_module']['palettes']['eventlist'];
PaletteManipulator::create()
->removeField('cal_calendar', 'config_legend')
->removeField('cal_format', 'config_legend')
->removeField('cal_featured', 'config_legend')
->removeField('cal_order', 'config_legend')
->removeField('cal_ignoreDynamic', 'config_legend')
->removeField('cal_hideRunning', 'config_legend')
->removeField('cal_limit', 'config_legend')
->addLegend('events_legend', 'title_legend', PaletteManipulator::POSITION_AFTER)
->addField('pickedevents', 'events_legend', PaletteManipulator::POSITION_APPEND)
->applyToPalette('pickedeventlist', 'tl_module')
;
$GLOBALS['TL_DCA']['tl_module']['fields']['pickedevents'] = [
'label' => &$GLOBALS['TL_LANG']['tl_module']['pickedevents'],
'inputType' => 'picker',
'eval' => [
'multiple' => true,
'isSortable' => true,
],
'sql' => [
'type' => 'blob',
'notnull' => false,
],
'foreignKey' => 'tl_calendar_events.id',
'relation' => [
'type' => 'hasMany',
'load' => 'lazy',
'table' => 'tl_calendar_events',
],
];
<?php
# contao/languages/de/tl_module.php
$GLOBALS['TL_LANG']['FMD']['pickedeventlist'] = [
'Ausgewählte Events',
'Module zur Darstellung ausgewählter Events'
];
$GLOBALS['TL_LANG']['tl_module']['events_legend'] = 'Events';
$GLOBALS['TL_LANG']['tl_module']['pickedevents'] = ['Anzuzeigende Events', 'Im Modul anzuzeigende Events auswählen und nach Bedarf sortieren.'];
<?php
# src/ModulePickedEventlist.php
namespace App;
use Contao\BackendTemplate;
use Contao\CalendarEventsModel;
use Contao\ModuleEventlist;
use Contao\StringUtil;
use Contao\System;
class ModulePickedEventlist extends ModuleEventlist
{
private array $pickedEventsIds;
private array $pickedEventsCalendarIds;
public function generate()
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . ($GLOBALS['TL_LANG']['FMD']['pickedeventlist'][0] ?? 'pickedeventlist'). ' ###';
//$objTemplate->wildcard = '### PickedEventList ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes', 'table'=>'tl_module', 'act'=>'edit', 'id'=>$this->id)));
return $objTemplate->parse();
}
$this->setData();
return parent::generate();
}
// protected function compile()
// {
// return parent::compile();
// }
protected function setData(): void
{
$this->pickedEventsIds = StringUtil::deserialize($this->pickedevents);
foreach ($this->pickedEventsIds as $eventId) {
$event = CalendarEventsModel::findById($eventId);
$this->pickedEventsCalendarIds[] = $event->pid;
}
$this->pickedEventsCalendarIds = array_unique($this->pickedEventsCalendarIds);
$this->cal_calendar = $this->pickedEventsCalendarIds;
$this->cal_format = 'cal_all'; // 'Alle Events'
$this->cal_featured = 'all_items'; // 'Alle Events anzeigen'
$this->cal_order = 'ascending';
$this->cal_ignoreDynamic = true; // URL-Parameter ignorieren
$this->cal_hideRunning = false;
$this->cal_limit = 0;
}
}
@fiedsch
Copy link
Author

fiedsch commented Oct 16, 2024

"Of course" the module code (as above) is missing the

protected function getAllEvents($arrCalendars, $intStart, $intEnd, $blnFeatured = null)
    {
        $eventIds = StringUtil::deserialize($this->pickedevents);
        foreach ($eventIds as $eventId) {
            $event = CalendarEventsModel::findById($eventId);
            $this->addEvent($event, $event->startTime, $event->endTime, $intStart, $intEnd, $event->pid);
        }

        return $this->arrEvents;
    }

method to restrict the events to the picked ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment