Combining contenttypes in setcontent

A forum for Bolt related questions / issues

Combining contenttypes in setcontent

Postby Dorien » Thu Sep 18, 2014 1:20 pm

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
Dorien
 
Posts: 21
Joined: Mon Nov 11, 2013 3:34 pm

Re: Combining contenttypes in setcontent

Postby Dorien » Thu Sep 18, 2014 1:42 pm

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 %}
Dorien
 
Posts: 21
Joined: Mon Nov 11, 2013 3:34 pm

Re: Combining contenttypes in setcontent

Postby jhice » Thu Sep 18, 2014 1:47 pm

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 ;)
jhice
 
Posts: 11
Joined: Wed Sep 17, 2014 12:01 pm

Re: Combining contenttypes in setcontent

Postby Dorien » Thu Sep 18, 2014 1:51 pm

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.
Dorien
 
Posts: 21
Joined: Mon Nov 11, 2013 3:34 pm

Re: Combining contenttypes in setcontent

Postby jhice » Thu Sep 18, 2014 2:04 pm

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 ^^
jhice
 
Posts: 11
Joined: Wed Sep 17, 2014 12:01 pm

Re: Combining contenttypes in setcontent

Postby Dorien » Thu Sep 18, 2014 2:22 pm

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;
    }
Dorien
 
Posts: 21
Joined: Mon Nov 11, 2013 3:34 pm


Return to Bolt Discussion

Who is online

Users browsing this forum: No registered users and 1 guest