Allow configuring default arguments if missing in request

This provides huge flexibility as TypoScript can define the actual
default.
It no longer is hard coded into source code (still falling back to old
defaults).
This commit is contained in:
Daniel Siepmann 2021-11-23 15:16:15 +01:00
parent 1e322668fa
commit cbc24ce88e
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
3 changed files with 67 additions and 7 deletions

View file

@ -21,6 +21,7 @@ namespace WerkraumMedia\Calendar\Controller\Frontend;
* 02110-1301, USA.
*/
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
@ -41,10 +42,17 @@ class CalendarController extends ActionController
*/
private $foreignDataFactory;
/**
* @var TypoScriptService
*/
private $typoScriptService;
public function __construct(
ForeignDataFactory $foreignDataFactory
ForeignDataFactory $foreignDataFactory,
TypoScriptService $typoScriptService
) {
$this->foreignDataFactory = $foreignDataFactory;
$this->typoScriptService = $typoScriptService;
}
public function initializeAction()
@ -61,7 +69,7 @@ class CalendarController extends ActionController
if ($this->request->hasArgument('year') === false) {
$this->request->setArguments([
'year' => [
'year' => date('Y'),
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
@ -86,8 +94,8 @@ class CalendarController extends ActionController
if ($this->request->hasArgument('month') === false) {
$this->request->setArguments([
'month' => [
'month' => date('m'),
'year' => date('Y'),
'month' => $this->getDefaultArgumentValue('month'),
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
@ -112,8 +120,8 @@ class CalendarController extends ActionController
if ($this->request->hasArgument('week') === false) {
$this->request->setArguments([
'week' => [
'week' => date('W'),
'year' => date('Y'),
'week' => $this->getDefaultArgumentValue('week'),
'year' => $this->getDefaultArgumentValue('year'),
],
]);
}
@ -138,7 +146,7 @@ class CalendarController extends ActionController
if ($this->request->hasArgument('day') === false) {
$this->request->setArguments([
'day' => [
'day' => date('Y-m-d'),
'day' => $this->getDefaultArgumentValue('day'),
],
]);
}
@ -172,4 +180,36 @@ class CalendarController extends ActionController
$this->eventDispatcher->dispatch($event);
$this->view->assignMultiple($event->getVariables());
}
/**
* Checks for TypoScript and transforms TypoScript into expected value.
* Allows to define defaults other than "now" for arguments.
*
* @return int|\DateTimeImmutable
*/
private function getDefaultArgumentValue(string $argumentName)
{
$arguments = $this->typoScriptService->convertPlainArrayToTypoScriptArray(
$this->settings['arguments'] ?? []
);
$fallbackValues = [
'year' => date('Y'),
'month' => date('m'),
'week' => date('W'),
'day' => date('Y-m-d'),
];
$value = $this->configurationManager->getContentObject()->stdWrapValue(
$argumentName,
$arguments,
$fallbackValues[$argumentName]
);
if ($argumentName === 'day') {
return new \DateTimeImmutable($value);
}
return (int) $value;
}
}

View file

@ -85,5 +85,6 @@ class Day
$this->foreignData = GeneralUtility::makeInstance(ForeignDataFactory::class)
->getData($this);
$this->initialized = true;
}
}

View file

@ -9,3 +9,22 @@ Provides:
Each day can have foreign data created by a factory.
That way extensions or TYPO3 instances can add further data to each day.
Configuration
-------------
Allows to configure default values for arguments if not provided in current request.
Each argument is configured below TypoScript settings namespace `arguments`, e.g.::
tx_calendar_example {
settings {
arguments {
year {
strtotime = midnight first day of -1 year
strftime = %Y
}
}
}
}
Supported arguments are: `year`, `month`, `week` and `day`.