Jump to content

How to check if each page reference checkbox is checked?


SwimToWin
 Share

Recommended Posts

It's incredible what you can do with ProcessWire. I did hit a limitation on my side though... πŸ˜‰ and would like to ask for some support:

Problem: I'm using a Page Reference Checkboxes field that gets it's Options from a few Pages; Page field value type is set to "Multiple pages PageArray".

Screenshot:

image.png.668b37df2f942992613b9e864b65e390.png

What I want: to check if each checkbox is checked or not (without using a foreach loop).

What I tried:

var_dump( $page->config->has(1898));

var_dump( $page->config->getValues()); // (WireArray is output, I don't know how to use that.)
var_dump( $page->config->getValues(1898)->id ); // Warning:  Attempt to read property "id" on array 

var_dump( $page->config->get(1898) ); // NULL
var_dump( $page->config->get(1898)->id ); // Warning:  Attempt to read property "id" on null 

var_dump( $page->config->first()->id ); // int(1898) ... better, but how to get the next value?

var_dump( $page->config[1898] ); // bool(false)

var_dump( $page->config->find(1898) );
var_dump( $page->config->find(1898)->id );

var_dump( $page->config(1898) ); NULL (checkbox is set)

var_dump( $page->config->1898 ); // unexpected integer "1898", expecting identifier or variable or "{" or "$"

var_dump( $page->config->find(1898)->id ); // Fatal Error: Uncaught Error: Call to a member function find() on null

var_dump( $page->config->has(1898)); // Fatal Error: Uncaught Error: Call to a member function has() on null 

$a = $page->config->getValues();
echo isset($a->1898->id) ? 1 : 0;

$a = $page->config->getValues();
echo isset($a->1898->id) ? 1 : 0; // Parse Error: syntax error, unexpected integer "1898", expecting identifier or variable or "{" or "$" 
echo isset($a[1898]->id) ? 1 : 0; // 0 (but checkbox is set?)
echo isset($a[1898]['id']) ? 1 : 0; // 0 (but checkbox is set?)

var_dump( $page->config->eq('foo')->id ); // int(1898)
var_dump( $page->config->eq('foo') ); // wirearray
var_dump( $page->config->has('foo')); // bool(true)

// Is this a bug? eq() returns same Page ID for two names. 
echo $page->config->eq('foo'); // 1898 (correct)
echo $page->config->eq('bar'); // 1898 (incorrect, the ID is 1899.

... and thanks for always being a helpful, constructive, and knowledgeable community! Make a great day! πŸ™‚

Link to comment
Share on other sites

Hello @SwimToWin,

if you use has(), you have to use a selector instead of an integer:

$page->config->has("id=1898"); // returns true

If you use find(), you get an PageArray as return:

$page->config->find(1898); // returns PageArray
count($page->config->find(1898)); // returns 1

Regards, Andreas

  • Like 1
Link to comment
Share on other sites

Solution: has('name') does the job, that's also self-explanatory .. compared to using IDs, as I did in my tests above.

var_dump( $page->config->has('foo') ); // bool(true)
  • Like 1
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

Γ—
Γ—
  • Create New...