Jump to content

How do we run a selector and statement saved as a variable?


Zahari M.
 Share

Recommended Posts

Hi gents!

Apologies if the topic to this question does not use the correct terms. But here goes...

Lets say we have a parent page. This parent page has lots of child pages.

In the template file for this parent page, I write this:

$posts = page()->children('limit=10');

This works great.
Now the question about saving this as a variable...
I have created a text field called myquery and attached it to the parent pages template.
Inside this text field, I have entered the whole selector and statement like so into the text field:

$page()->children('limit=10')

Now what I am trying to do in my template file is this:
 

$q = $page->myquery;

$posts = $q;

Doing this gives me this error, Fatal Error: Uncaught TypeError: Argument 1 passed to ProcessWire\ukPosts() must be an instance of ProcessWire\PageArray, string given.

If I wanted to lets say change the number of children from a text field in my template, I have managed to do this by way of passing a variable into the statement. So I could use a variable for the value for limit. This works.

But the reason I want to try and save the whole statement and selector is so that we can run a query on this page and it can fetch pages from anywhere in our site.

In a way, I do understand the error in that I am making posts now equal a  string. But how can I echo out an entire query statement from a field and get it to be "run"?

Hope the question makes sense!

Cheers

Link to comment
Share on other sites

16 minutes ago, Zahari M. said:

In a way, I do understand the error in that I am making posts now equal a  string. But how can I echo out an entire query statement from a field and get it to be "run"?

There are ways to do that (search for eval()), but really I'd quite strongly urge you to do it any other way. Evaling code is a quite unsafe operation unless you're very thorough with what you pass into it and it also makes your code more scattered, more difficult to debug and stuff like that. Much rather look for a way to encode the query you want to do in data instead of in code. E.g. processwire has the FieldtypeSelector, which allows you to use a quite flexible interface to build up selectors and it simply gives you an selector string to query pages by.

Link to comment
Share on other sites

Hey LostKobrakai!

Thanks for the replies!!

I did look there initially before asking but there were quite a few uninstalled ones. Hence I thought better ask you first.

I did find it and it's called Selector and it says... Build a page finding selector visually.

Very interesting options to build the selector. I will try and figure it out from here...

Cheers!

 

Link to comment
Share on other sites

Hello psy

Maybe it's a personal thing, but a hi or hello goes a long way when a total stranger addresses you directly... some might possibly consider it rude and confrontational

I shall be happy to honor your request and no longer assume that ProcessWire devs are gents.

If there are any other non males here that may have taken offence, my apologies to you all too.

 

  • Like 2
Link to comment
Share on other sites

 
Thank you both for your help gents.
 
For future searches & seekers that might stumble upon here...
 
For those of you gents, ladies ( and any others ) that are wanting to be able to , from the admin side, create custom queries to select specific sets of pages, such that you can easily determine what pages are listed via a specific "listing" page, then the Selector fieldtype is definitely your friend!
 
Rough Installation Guide
Go to admin > modules > core.  Scroll to the Fieldtype section. Locate and install "Selector" fieldtype.
 
Create a new Selector field. I named mine 'selector'. Upon creating it, it suggests that you give it a starting value like template=product. In my case... I ended up not giving it any value. Leaving that initial suggested template value out means we have the option of subsequently selecting our template later, which gives us more flexibility in  use.
 
Go to your template in admin and add the selector field.
 
Go to your template file and do something like below. Thus, we can, as LostKobrakai suggests, start building a selector that ultimately creates the required variable string and safely feeds this into our $pages->find() selector...
 
$posts = $pages->find("$page->selector");

Go to a page that has this field and using the selector field's UI, start building your selector! I have tried attaching a screen shot of what it looks like. What I was trying to do earlier can be seen in my Query text field as mentioned in my original question. I have now removed this Query text field and just use the Selector field.

As an aside, I am using a Page Reference field called opts_article displayed in the form of multiple checkboxes. What I am doing here is using these checkboxes as a means of preventing certain pages from being listed.  For example, you can see here that my Selector is listing all pages that has the article-page template. You can also see the limit of 10 pages. But one of my favourite blog posts from Ryan talked about efficient use of PW fields,see here: Efficient Use of Fields. And so by creatively using a single Page Reference field, we can create, on the fly many checkboxes easily.

I mention this because using this Selector field, it enables one to effortlessly select and factor in the desired logical purpose of your checkbox when building your selector.

I'm sure this is all basic stuff for many of you here, but I'm also sure it can help dummies like me too ?

 

728516659_Selector1.thumb.jpg.88d0568ed8afc9419bf4fd31e05432ff.jpg

 

 

 

 

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

  • Recently Browsing   0 members

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