Jump to content

Selector for the value of a dropdown built with a PageArray


buddy
 Share

Recommended Posts

Hi all,

I've created a dropdown field on one of my templates by using the usual PageArray method, and now I'd like to select pages with it.

The pages that populate the dropdown field have a type and a single value. (Title: Red, color_code: 0xf00)

My problem is that it seems that the only way to select is by page id, which seems ugly, because I have to do something like this:

$pageIdForColor = [
"0x00f" => 2067,
"0x0f0" => 2068,
"0xf00" => 2069
];

$page = $wire->pages->get("parent=/boxes/, color=" . $pageIdForColor["0x00f"]);

Since all of my colors are already defined as pages, it would be great if there were a simple way to select by the color_code instead of having to re-create the mapping.

I'm imagining something like this:

$page = $wire->pages->get("parent=/boxes/, color->color_code=0x00f");

Anyone have any thoughts on how to simplify this? One idea I have is to just drop the PageArray method and code up a custom FieldType with these values fixed in it, since they don't need to be edited very often.

Thanks!

Link to comment
Share on other sites

Hey Buddy

I would be insterested what the "usual" way is? :)

Decodesiffering your post...

I think you can simply first query the color pages. It will give you a PageArray with the color pages, with that you can then use directly to find the pages with these colors selected through a page field.

// get all pages with at least 1 of these colors, this will return a page array, if you echo it you'll get a list of IDs
// here "color" is the text field with the color string
$pagesForColor = $pages->find('color=0x00f|0x0f0|0xf00');

// we find all pages that have at least one of the colors selected
// here "color_select" would be the page field
$result = $pages->find("parent=/boxes/, color_select=$pagesForColor");

It's that what you're looking for?

PS: Don't use $page or $pages variables for the results of queries. YOu may overwrite the template system variable with that name.

  • Like 1
Link to comment
Share on other sites

Thanks Soma,

Your suggestion is exactly what I was looking for, and it works perfectly.

(And to answer your other questions, the "usual" way was what I saw described in some other forum posts, and no, I'm not working with templates.)

Link to comment
Share on other sites

I'm not sure what you mean by the pageArray method, but if what you mean is the Page FieldType, you should be able to do this:

$color = $pages->get("color_code=0x00f");
$page = $wire->pages->get("parent=/boxes/, color=$color");

edit: oh, didn't see the previous posts... they appeared while I was writing

  • Like 2
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...