Jump to content

Pageimage subfields selector issue


kunago
 Share

Recommended Posts

I have a template which has a date_from and date_to fields, and an image field with subfields of date_from and date_to. I am trying to create this type of a selector:

$pages_list = $pages->find('template=this_template, date_from<=today, date_to>=today, image.count>0, image.date_from<=today, image.date_to>=today');

I believe this is self-explanatory. While the date_from and date_to of the template work fine for filtering, I am unable to access and work with the Pageimage subfields. Selectors don't support subfields of Pageimage. That is a first issue. I tried I believe all combinations documented with no success. Subselectors also don't seem to make any difference.

I also tried to get at least the unformatted value of the subfield but I am unable to find a way to retrieve it, because while "getUnformatted" works fine with $page, it does not work with Pageimage.

The aim is to retrieve all images of the template "this_template" where date_from and date_to filter out both, pages and images at the same time. I thing this might not be feasible in a single step but not even the subselectors seem to work fine.

 

And a bonus question - how to create an OR-type selector that would do the following?

$pages->find('template=this_template, date_from= OR date_from<=today')

I used the keyword OR that demonstrates what I am after.

 

Thanks for any help.

Link to comment
Share on other sites

Selectors support custom fields for images.

So, my first guess is that there's something about the logic of the selector or how it interacts with the data that is causing a problem.

One thing to watch out for is that 'today' is, I believe, specifically 00:00:00 today – so, for example, '<=today' might be better as '<tomorrow', or you could create your own variable containing the particular time you want.

Regarding your bonus question, I think the last point above may lead to the result you want.

$pages->find('template=this_template, date_from<=tomorrow')
Link to comment
Share on other sites

20 minutes ago, BillH said:

Selectors support custom fields for images.

So, my first guess is that there's something about the logic of the selector or how it interacts with the data that is causing a problem.

I tried various ways to access data but the result is the following:

image.thumb.png.fcf2fbf5afd1bffc3bfba16408f49285.png

I think the error message is quite clear about what is going on. Even though I am not accesing the Pageimage object but the fields associated to it, it always throws the error. This is the full selector:

$list = $pages->find('template=news-action, image_kiosk.count>0, date_from_show<=today, date_to>=today, image_kiosk.date_from<=today, image_kiosk.date_to>=today');

The structure of the template is this:

image.png.b147cffc238d91d58874877f096f6ac5.png

and the Pageimage subfields are these:

image.png.258633328577f606a0c9c9a86ed2c4d9.png

They are of the same type as those used in the template. Although the logic of the selector seems to be fine to me, it always throws an error.

 

24 minutes ago, BillH said:

Regarding your bonus question, I think the last point above may lead to the result you want.

$pages->find('template=this_template, date_from<=tomorrow')

Thank you, but actually I need to filter all pages of a specific template where either date_from is empty or is earlier than today. It's easy to do it with AND:

date_from=, date_from<=today

but that makes no sense. I also tried

$pages->find('template=this_template, (date_from=), (date_from<=today)')

however that does not seem work, although it seems to be close to what I am looking for.

Link to comment
Share on other sites

It does seem that comparison operators don't work with image custom fields. I've just tried it with a text field and, like you, get a message saying the operator is not supported.

I don't know why this is. Perhaps someone else can explain...

A rather laborious workaround might be to create separate index fields that are automatically maintained with a hook on saving the article. If no better solution is suggested, let me know if you need help with this.

On the bonus question, your version using OR-groups looks as if it should work, so I'm not sure what the issue is – though you might want to try 0 instead of the empty string.

Link to comment
Share on other sites

I coded a solution to my issue, in case someone needed it. No matter what I tried, I could not do it the selector way because as mentioned, subfields of Pageimage don't seem to be directly accessible. Even if they were, I would still need to make some adjustments, because date_from and date_to belonging to a picture can be either a date or can be empty, which is also a valid value. As the getUnformatted does not seem to work for Pageimage either, I had to do some crazy things to get the job done.

At the beginning I needed to get the format of the output date of the fields date_from and date_to. Those are different for different languages and since dateOutputFormat property of the field does not accept language parameter, I had to do some custom detection using a fuction

function language_id () {
  $language = wire('languages')->getLanguage();
  if ($language->name == 'default') {
    return '';
  } else {
    return strval($language->id);
  }
}

and then extract the output format of the active language.

$format_date_from = $fields->get('date_from')->get('dateOutputFormat' . language_id());
$format_date_to   = $fields->get('date_to')->get('dateOutputFormat' . language_id());

Then I filtered out the pages in question where I was searching for images:

$today = $datetime->strtotime("today");

$filtered = $pages->find('template=this_template, image.count>0, date_from_show<=today, date_to>=today');
foreach ($filtered as $page) {
  $image           = $page->image;
  $image_date_from = !empty($image->date_from) ? $datetime->stringToTimestamp($image->date_from, $format_date_from) <= $today : true;
  $image_date_to   = !empty($image->date_to)   ? $datetime->stringToTimestamp($image->date_to, $format_date_to)     >= $today : true;
  
  if ($image_date_from && $image_date_to) {
    ...
  }
}

That is a cumbersome way but seems to do what it is supposed to do.

Could someone think of a way to make it easier? Am I missing some function that could do it more efficiently? I mean anything is more efficient but it does not seem to work any other way.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...