i would like to get the month of my etbase item. In the database there is the crdate. I would like to use this and only get the month. My value is always empty.
/**
* @var string
*/
protected $month = '';
/**
* Returns the month
*
* @return string $month
*/
public function getMonth()
{
return $this->month;
}
/**
* Sets the month
*
* @param string $month
* @return void
*/
public function setMonth(string $month)
{
$month = 'dsds';
$this->month = $month;
}
In my TCA if added this:
'month' => [
'config' => [
'type' => 'passthrough',
],
],
How can i get the month of the item? I would like to group it in my fluid template.
EDIT:
Ive tried it with a ViewHelper
<?php
namespace Ext\XY\ViewHelpers;
/* *
* This script belongs to the FLOW3 package "Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
* General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with the script. *
* If not, see http://www.gnu.org/licenses/lgpl.html *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\Variables\VariableExtractor;
use TYPO3Fluid\Fluid\Core\ViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* Grouped loop view helper for Datetime values.
* Loops through the specified values
*
* = Examples =
*
* <code title="Simple">
* // $items = array(
* // array('name' => 'apple', 'start' => DateTimeObject[2011-10-13 00:15:00]),
* // array('name' => 'orange', 'start' => DateTimeObject[2011-12-01 00:10:00]),
* // array('name' => 'banana', 'start' => DateTimeObject[2008-05-24 00:40:00])
* // );
* <a:groupedForDateTime each="{items}" as="itemsByYear" groupBy="start" format="Y" groupKey="year">
* {year -> f:format.date(format: 'Y')}
* <f:for each="{itemsByYear}" as="item">
* {item.name}
* </f:for>
* </a:groupedForDateTime>
* </code>
*
* Output:
* 2011
* apple
* orange
* 2010
* banana
*
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
* @api
*/
class GroupedForDateTimeViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('each', 'array', 'The array or \SplObjectStorage to iterated over', true);
$this->registerArgument('as', 'string', 'The name of the iteration variable', true);
$this->registerArgument('groupBy', 'string', 'Group by this property', true);
$this->registerArgument('format', 'string', 'The format for the datetime', false);
$this->registerArgument('groupKey', 'string', 'The name of the variable to store the current group', false, 'groupKey');
$this->registerArgument('dateTimeKey', 'string', 'dateTimeKey The name of the variable to store the current datetime', false);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$each = $arguments['each'];
$as = $arguments['as'];
$groupBy = $arguments['groupBy'];
$groupKey = $arguments['groupKey'];
$format = $arguments['format'];
$dateTimeKey = $arguments['dateTimeKey'];
$output = '';
if ($each === null) {
return '';
}
if (is_object($each)) {
if (!$each instanceof \Traversable) {
throw new ViewHelper\Exception('GroupedForViewHelper only supports arrays and objects implementing \Traversable interface', 1253108907);
}
$each = iterator_to_array($each);
}
$groups = static::groupElements($each, $groupBy, $format);
$templateVariableContainer = $renderingContext->getVariableProvider();
foreach ($groups['values'] as $currentGroupIndex => $group) {
$templateVariableContainer->add($groupKey, $groups['keys'][$currentGroupIndex]);
$templateVariableContainer->add($as, $group);
$output .= $renderChildrenClosure();
$templateVariableContainer->remove($groupKey);
$templateVariableContainer->remove($as);
}
return $output;
}
/**
* Groups the given array by the specified groupBy property.
*
* @param array $elements The array / traversable object to be grouped
* @param string $groupBy Group by this property
* @param string $format The format for the datetime
* @return array The grouped array in the form array('keys' => array('key1' => [key1value], 'key2' => [key2value], ...), 'values' => array('key1' => array([key1value] => [element1]), ...), ...)
* @throws ViewHelper\Exception
*/
protected static function groupElements(array $elements, $groupBy, $format)
{
$extractor = new VariableExtractor();
$groups = ['keys' => [], 'values' => []];
foreach ($elements as $key => $value) {
if (is_array($value)) {
$currentGroupIndex = isset($value[$groupBy]) ? $value[$groupBy] : null;
} elseif (is_object($value)) {
$currentGroupIndex = $extractor->getByPath($value, $groupBy);
} else {
throw new ViewHelper\Exception('GroupedForViewHelper only supports multi-dimensional arrays and objects', 1253120365);
}
if (strpos($format, '%') !== false) {
$formatedDatetime = strftime($format, $currentGroupIndex->format('U'));
} else {
$formatedDatetime = $currentGroupIndex->format($format);
}
$groups['dateTimeKeys'][$formatedDatetime] = $currentGroupIndex;
if (strpos($format, '%') !== false) {
$currentGroupIndex = strftime($format, $currentGroupIndex->format('U'));
} else {
$currentGroupIndex = $currentGroupIndex->format($format);
}
$currentGroupKeyValue = $currentGroupIndex;
if ($currentGroupIndex instanceof \DateTime) {
//$currentGroupIndex = $currentGroupIndex->format(\DateTime::RFC850);
$formatedDatetime = $currentGroupIndex->format($format);
$groups['dateTimeKeys'][$formatedDatetime] = $currentGroupIndex;
} elseif (is_object($currentGroupIndex)) {
$currentGroupIndex = spl_object_hash($currentGroupIndex);
}
$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;
$groups['values'][$currentGroupIndex][$key] = $value;
}
return $groups;
}
}
and this in the fluid template
<a:groupedForDateTime each="{items}" as="itemsByYear" groupBy="start" format="Y" groupKey="year">
<a:groupedForDateTime each="{itemsByYear}" as="itemsByMonth" groupBy="start" format="m" dateTimeKey="month">
<div class="yearMonth clearfix">
<div class="timeframe" data-behavior="FixTimeframe">
{month -> f:format.date(format: 'F Y')}
</div>
<f:for each="{itemsByMonth}" as="item">
<div class="item">
<p class="startEnd">
<a:format.duration start="{item.start}" end="{item.end}"/>
</p>
<h2>{item.name}</h2>
<div class="text">
<f:format.html>{item.bodyText}</f:format.html>
</div>
</div>
</f:for>
</div>
</a:groupedForDateTime>
</a:groupedForDateTime>
then ill get this error:
Fluid parse error in template partial_Archive/ListItems_6877ade17ed77be0a2f8660fe2717844ab3400c3, line 25 at character 30. Error: The ViewHelper "<a:format.duration>" could not be resolved. Based on your spelling, the system would load the class "Ext\XY\ViewHelpers\Format\DurationViewHelper", however this class does not exist. (error code 1407060572). Template source chunk: <a:format.duration start="{item.start}" end="{item.end}"/>