Jump to content

Recommended Posts

Posted

Hello dear Processwirers,

quick question, I always struggle with the ->has() API when it comes to page reference fields. Which syntax is correct and reliable? Cause I never know if I coincidentally get false positives or false negatives or if I'm actually doing it right.

if ($somePageField->has($somePage)
if ($somePageField->has($somePage->id)
if ($somePageField->has("id=$somePage->id")

and does it make a difference whether the page field allows multiple page selection or not AND does it make a difference if the related page is a user page? I mean can I do this:

if ($somePageField->has("id=$this->user->id")

Thanks for help!

Posted

I am assuming you are talking about something like

<?php

$page->pageReferenceField->has(...)

If so, then it depends on how you have setup the field. Your assumption is correct that there is a difference depending on if the field allows for single or multiple values.

For single values, you are actually talking to a Page and are calling it's has() method.
For multiple values, you are actually talking to a PageArray and are calling it's has() method.

It does not make a difference if you are looking for User though. Apart from that their properties are different from a Page's.

If you have setup your field to return a single value, has() will look for page properties, as in https://processwire.com/api/ref/wire-data/has/

And if you're setup for multiple values, has() will look for pages matching a selector, as in https://processwire.com/api/ref/wire-array/has/

I think based on the examples you will want to look at multiple values. Only there can you pass a Page and see if it's actually in the PageArray.

  • Like 3

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
  • Recently Browsing   0 members

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