Here is my code. I’ve tried callbacks. I can group just by groupBy(insider_date) but that only grouped by date, not by year.
If you know how I can do this by year, that would be awesome. Here’s my code.
<?php
$marketingItems = $page->marketing()->toStructure();
$callback = function($marketingItems) {
return $marketingItems->insider_date('Y');
};
$getMarketingItems = $marketingItems->groupBy($callback);
//var_dump($getMarketingItems);?>
<div class="text">
<div class="grid-x grid-margin-x grid-padding-x grid-padding-y grid-margin-y small-up-1 medium-up-3 large-up-5" >
<? foreach($getMarketingItems as $group => $itemsPerGroup): ?>
<div class="newsItem box effect1 cell filter-simple-item">
<h2 class="box-header"><?php echo ucwords($group) ?></h2>
<p> <ul>
<?php foreach($itemsPerGroup as $item) : ?>
<li>
<a href="<?php e($item->url()->isNotEmpty(), $item->url(), $item->url()) ?>" <?php e($item->url()->isNotEmpty(), 'target="_blank"', '') ?> >
<?php
if ($item->url()->isNotEmpty())
echo html($item->title() . ' ' . '<i class="fa fa-external-link" aria-hidden="true"></i>');
else
echo html($item->title());
?>
</a>
</li>
<?php endforeach; ?>
</ul></p>
</div>
<?php endforeach ?>
</div>
</div>
```
texnixe
September 20, 2017, 6:02pm
2
groupBy()
does not accept a callback function, is that a typo?
Apart from that, have you tried:
$callback = function($marketingItems) {
return $marketingItems->date('Y', 'insider_date');
};
I’m trying to use groupby. Not a typo. Also, you have marketingItems->date but my yaml is insider_date: will that matter?
insider_date:
label: Date
type: date
This worked. Thanks texnixe
<?php
$marketingItems = $page->marketing()->toStructure();
$callback = function($marketingItems) {
return $marketingItems->date('Y', 'insider_date');
};
$getMarketingItems = $marketingItems->group($callback);
//var_dump($getMarketingItems);
?>
<div class="text">
<div class="grid-x grid-margin-x grid-padding-x grid-padding-y grid-margin-y small-up-1 medium-up-3 large-up-5" >
<? foreach($getMarketingItems as $group => $itemsPerGroup): ?>
<div class="newsItem box effect1 cell filter-simple-item">
<h2 class="box-header"><?php echo ucwords($group) ?></h2>
<p> <ul>
<?php foreach($itemsPerGroup as $item) : ?>
<li>
<a href="<?php e($item->url()->isNotEmpty(), $item->url(), $item->url()) ?>" <?php e($item->url()->isNotEmpty(), 'target="_blank"', '') ?> >
<?php
if ($item->url()->isNotEmpty())
echo html($item->title() . ' ' . '<i class="fa fa-external-link" aria-hidden="true"></i>');
else
echo html($item->title());
?>
</a>
</li>
<?php endforeach; ?>
</ul></p>
</div>
<?php endforeach ?>
</div>
</div>
texnixe
September 20, 2017, 6:39pm
5
Just on a side note: I wouldn’t use a plural for the function parameter/inside the function; this is always a single collection member, so I think its less confusing to use a singular here:
$callback = function($marketingItem) {
return $marketingItem->date('Y', 'insider_date');
};
One more quick question. That worked.
Any idea, how I can take
if ($item->url()->isNotEmpty() || $item->postimage()->isNotEmpty())
echo html($item->title() . ' ' . $item->insider_date('m') . ' ' . '<i class="fa fa-external-link" aria-hidden="true"></i>');
and make $item->insider_date(‘m’) and return the actual month name. Like september?
Thanks your singular suggestion worked too.
texnixe
September 20, 2017, 7:06pm
7
Have a look at strftime()
, but note that it takes a Unix timestamp as parameter, so you have to convert the string from insider_date to a unix timestamp first.
echo stftime('%B', strtotime($item->insider_date()));
(Might be necessary to use $item->insider_date()->value(), I’m never sure when this is necessary or not)