Page 1 of 1

Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 1:20 pm
by Dorien
Hi!

I have a contenttype news and blog. Those two i would like to combine in a list (ordered by date) on the homepage. But the setcontent only gives a array for 1 contenttype. How could i combine two contenttypes in setcontent?

~ Dorien

Re: Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 1:42 pm
by Dorien
Boooyaaaah! We did it!

Code: Select all
{% setcontent newsItems = "nieuws-item/latest/3" %}
{% setcontent blogItems = "blog-item/latest/3" %}
{% set newsBlogItems = newsItems %}
{% set newsBlogItems = newsBlogItems|merge(blogItems) %}

{% for newsBlogItem in newsBlogItems %}
    {{newsBlogItem.title}}<Br>
{% endfor %}

Re: Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 1:47 pm
by jhice
Great :)

I was typing a reply to use the Bolt search, while couldn't remember where I see this concept of merging...
It's in the Twig filters :
http://twig.sensiolabs.org/doc/filters/merge.html

Thanks for finding it ;)

Re: Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 1:51 pm
by Dorien
Or at least now i have a combined array, but still has to ordered on date. And this is a tricky part. Since the sort filter does not order on date.

Re: Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 2:04 pm
by jhice
The setcontent can order by date : https://docs.bolt.cm/content-fetching (Ordering the results).
But when merging your results with Twig you will mix different orderings...

I dont know if you should do a Twig extension or do a custom query with Doctrine from Bolt :)
Or something else ^^

Re: Combining contenttypes in setcontent

PostPosted: Thu Sep 18, 2014 2:22 pm
by Dorien
We did it!

We have added a twig filter in the src/Bolt/TwigExtension.php. But now we have edited the source code of bolt, so we get in trouble when updating. Is there a special place for our own twig extensions?

Code: Select all
new \Twig_SimpleFilter('datesort', array($this, 'datesort'))

   public function datesort($array)
    {
        usort($array, function ($a, $b) {
            $first = new \DateTime($a->values['datepublish']);
            $second = new \DateTime($b->values['datepublish']);

            if ($first < $second) {
                return 1;
            } elseif ($first > $second) {
                return -1;
            }

            return 0;
        });

        return $array;
    }