For a project I need to use the content representations to fetch a json file. But in order to avoid a big load of every data in that json, I want to be able to filter its data by a tag.
I saw in the documentation, the possibility to fetch a specific pagination:
fetch('/blog.json/page:2')
so I was wondering if that would be possible to do it with any tags like:
Thanks for your quick reply. I see, that makes sense, I didn’t have time to try properly yet, just a nasty manual test, but no luck yet, I will try to provide a clean source code tomorrow.
I could run another test now, I’m close to achieving what I want but there is still a problem with the output of the JSON and the URL.
So my posts have a structural data, inside there are dates (starting date and ending date), I’m using the last date as a ‘date indicator’. This allow me to have every posts shown in an ordered year (until 2016).
<?php
// current year
$current_year = date('Y');
$first_year = (int)$current_year - 1;
$last_year = 2016;
for ($i = $first_year; $i >= $last_year; $i--) {
// get all yearly posts
$past_collection = $page
->children()
->filter(function ($page) use ($i) {
$seance_date = $page->practical_information()->toStructure()->last()->date_start();
return $seance_date->toDate('%Y') == $i;
});
// Sorting the posts by date
$sorted_collection = $past_collection->sortBy(function ($page) {
return $page->practical_information()->toStructure()->last()->date_start()->toDate();
}, 'asc');
$json = [];
$json['data'] = [];
$json['year'] = $i;
foreach ($sorted_collection as $article) {
$json['data'][] = array(
'month' => (string)$article->practical_information()->toStructure()->last()->date_start()->toDate('%B'),
'url' => (string)$article->url(),
'title' => (string)$article->title(),
'image' => (string)$article->images()->filterBy('template', 'gallery')->first()
);
}
echo json_encode($json);
}
The problem is that the JSON output has then multiple root elements and the URL ‘…/films.json/year:2019’ or any other year returns the same.
Actually I wanted first to try without the tag, thus the above code. But from what I understood if I want to use something else than “/page(:num)” or “/tag(:any)” I would have to add some new routes to the config.php. Which is becoming a bit too complicated.
Thank you for the more elegant chaining method, regarding the last(), I made the structure field mandatory so there will always be a value there.
Regarding the controllers, so in theory if I want to have any other parameter filter in the URL I could simply pass it with the controller method without altering any route in the config.php?