lloop Posted July 22, 2014 Share Posted July 22, 2014 I'm writing a module that generates random pages with random content. I have adopted the interface that PW uses for selectable pages in a inputfieldPage edit. That is ... the ability to choose a parent page and/or a template. Down the line I will incorporate the selector and php code inputs. I can't get my brain around a way to find templates that allow another certain template as a child. I would then find the pages and use one as a randomly selected parent page. So the question is ... is it possible to use the template object childTemplates property, somehow in a selector? Being an array, it doesn't seem so. But I'm hoping maybe I'm overlooking something, or approaching it from the wrong angle. Don't really want to foreach through templates or anything like that. Link to comment Share on other sites More sharing options...
adrian Posted July 22, 2014 Share Posted July 22, 2014 I know you specifically asked for a way using a selector and that you didn't want to foreach through the templates, but I don't think that is possible and no-one has responded with anything better yet, so $tid = $templates->get("template_name")->id; $validTemplates = array(); // this array will be populated with templates that have the required parentTemplates settings foreach($templates as $t){ if(in_array($tid, $t->parentTemplates)) $validTemplates[] = $t->name; } Not ideal I know, but it seems to processes quickly and I think it gets you what you need, although I am guessing you probably knew how to do this already 1 Link to comment Share on other sites More sharing options...
lloop Posted July 23, 2014 Author Share Posted July 23, 2014 Thanks adrian, If only for confirming that I am not overlooking something simple. Ultimately, it will be installed during site dev only, so a little slow is not so much of a problem. But it's also a big learning experience for me, so I'm also looking for the most solid approach. Link to comment Share on other sites More sharing options...
adrian Posted July 23, 2014 Share Posted July 23, 2014 Reading through your question again and putting aside the parentTemplatea setting altogether, would it not be possible to just find a random parent of the page in question. I think maybe I don't still really understand what you are trying to do, but is this any help? $page->parents->getRandom(); That will get one random parent of the current page, but of course you could do this for any base page. Sorry if this is not at all related - definitely having trouble deciphering exactly what your needs are - maybe an example of your page tree structure and the pages you are trying to identify would be helpful. Link to comment Share on other sites More sharing options...
lloop Posted July 23, 2014 Author Share Posted July 23, 2014 OK More specifics. I think my main problem is that I am starting from the scenario that only a template is chosen. So if I had a tree that represented a catalog of furniture. With each level of the tree having it's own template. (page_title - template_name) Catalog - catalog Furniture - item_head Chairs - item_subhead Mahogany Chair - item_info Tables - item_subhead Oak Table - item_info Glassware - item_head Vases - item_subhead Bowls - item_subhead Crystal Bowl - item_info Painting and Sculpture - item_head Now if I wanted to generate random pages with the item_info template, I would be looking for an array of pages that have the item_info template as a possible child. So starting from a selected template I am looking for an array of templates that have the possibility of children with my chosen template. The foreach loop works, so for now I am going with that. I threw in a todo to try to directly access the db with a pdo statement eventually. I have to research how to effectively search the json encoding in the data column in the templates table. Not sure if that's good to do with a mysql text search or not. I was thinking at the beginning of this thread that maybe .... because the PW selectors do work with mysql searches (and this is something I am not very strong with, so forgive my ignorance) that maybe it could dip into the json encoded array in the db data column as a selector. And also maybe my tree structure is just fundamentally wrong. and just to add an oops ... my first post asked about parentTemplates when I am actually using childTemplates. Link to comment Share on other sites More sharing options...
SiNNuT Posted July 23, 2014 Share Posted July 23, 2014 I've modified the topic title and your first post to reflect the fact that it is about the $childTemplates property of a template. You would want to do something like this? $templateID = item_info->id; // id of the item_info template $templates->find("childTemplates=$templateID") // find templates that have $templateID in childTemplates array I don't think this is possible. Not entirely sure though. But what is wrong with foreach-ing the templates? This would not be too costly i think. Link to comment Share on other sites More sharing options...
lloop Posted July 23, 2014 Author Share Posted July 23, 2014 Yeah , for now I will go with the foreach. The question essentially applies to both childTemplates and parentTemplates, no? I was just wondering if it was possible to use the selectors like that. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now