Jump to content

[SOLVED] Page Field Select From Grand Parent of Page?


prestoav
 Share

Recommended Posts

Hi all,

I'm building a categorised FAQ section for a site with the following structure:

-FAQ Page (template = faq-parent)
-- Category 1 (template = faq-category)
-- Category 2 (template = faq-category)
etc.
-- Questions (template = faq-articles)
--- Question 1 (template = faq-article)
--- Question 2 (template = faq-article)
etc.

The 'Questions' page is a container for all the actual question pages (articles) to make admin clearer as there are likely to be 100+ question pages / articles.

I'd like to make the system so I can duplicate it elsewhere in the same website as we might have more than one FAQ section using the same templates and page structure.

Each article has a page reference field where the editor can choose one or more categories for the article to be listed under. So, to ensure flexibility, I'd like this field to only select from categories that are under the main 'FAQ' page grandparent of the actual article. This would ensure that a second FAQ section would not conflict with the first.

Setting up the page reference field for the article category I would usually set parent and template in the 'Selectable Pages' fieldset. However, setting the parent would restrict the flexibility of the system to just this FAQ section (because the parent is pre-set).

So, trying to use the 'Selector String' method I added the selector:

template=faq-category, parent=$page->parent->parent, sort=sort

But I get this error when saving an article page with a chosen category:

PagesEditor: Error saving field "Categories this question is displayed in" — Unrecognized operator: $

Also, when selecting the category on the article page, the whole page tree is displayed rather than the sub set of pages found by the selector.

Does anyone have any ideas how this might be done or will I need to leave the field selection open for any page?

All thoughts gratefully received!

 

Link to comment
Share on other sites

I think the error comes from trying to use PHP variables? 

I have approached this using the getSelectablePages hook and a couple others to enable dynamically adding them to different parents depending on their position within the tree,.

(Bear with me since I am basing this example this from a very old project I have that "cascades" options into children page fields, it might not work exactly in a copy/paste fashion)

Ok so this hook  gets the pages only under the closest faq-parent of the current page with the reference field:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    $page = $event->arguments('page');
    $event->return = $page->closest('template=faq-parent')->children('template=faq-category');
});

Now, the tricky part when doing this dynamically and adding new pages is selecting the right parent, which from some old code I have lying around has to be done sort of like this:

$wire->addHookBefore('InputfieldPage::processInputAddPages', function($event) {
    $field = $event->object;
    $page = $field->hasPage;
    $parent_id = $page->closest('template=faq-parent');
	$field->set("parent_id", $parent_id);
});

And this last step I am not super sure, but I think it is needed to enable the Add New pages part.

$wire->addHookBefore('InputfieldPage::renderAddable', function($event) {
    $field = $event->object;
    $page = $field->hasPage;
	$parent_id = $page->closest('template=faq-parent');
	$field->set("parent_id", $parent_id);
})

 

  • Like 2
Link to comment
Share on other sites

14 hours ago, prestoav said:

So, trying to use the 'Selector String' method I added the selector:

template=faq-category, parent=$page->parent->parent, sort=sort

Try instead:

template=faq-category, parent=page.parent.parent, sort=sort

 

  • Like 2
Link to comment
Share on other sites

14 hours ago, elabx said:

I think the error comes from trying to use PHP variables? 

I have approached this using the getSelectablePages hook and a couple others to enable dynamically adding them to different parents depending on their position within the tree,.

(Bear with me since I am basing this example this from a very old project I have that "cascades" options into children page fields, it might not work exactly in a copy/paste fashion)

Ok so this hook  gets the pages only under the closest faq-parent of the current page with the reference field:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    $page = $event->arguments('page');
    $event->return = $page->closest('template=faq-parent')->children('template=faq-category');
});

Now, the tricky part when doing this dynamically and adding new pages is selecting the right parent, which from some old code I have lying around has to be done sort of like this:

$wire->addHookBefore('InputfieldPage::processInputAddPages', function($event) {
    $field = $event->object;
    $page = $field->hasPage;
    $parent_id = $page->closest('template=faq-parent');
	$field->set("parent_id", $parent_id);
});

And this last step I am not super sure, but I think it is needed to enable the Add New pages part.

$wire->addHookBefore('InputfieldPage::renderAddable', function($event) {
    $field = $event->object;
    $page = $field->hasPage;
	$parent_id = $page->closest('template=faq-parent');
	$field->set("parent_id", $parent_id);
})

 

Hi @elabx,

Thanks so much for showing these examples I'm not great with page hooks yet but I'm getting there.

I'm going to look deeper into you examples and have a play!

Link to comment
Share on other sites

57 minutes ago, Robin S said:

Try instead:

template=faq-category, parent=page.parent.parent, sort=sort

 

Hi @Robin S,

Many thanks. This has cured the saving error for sure. It doesn't address the full tree being shown when you go to select a page but I'm not sure that's even possible.

Again, every day is a school day. I had seen and used the 'page.field' format in selectors before but not the 'page.page' one. I also wasn't aware that '$page' could be substituted with 'page' in these selectors so thanks for that lesson too!

G.

Link to comment
Share on other sites

6 minutes ago, prestoav said:

It doesn't address the full tree being shown when you go to select a page but I'm not sure that's even possible.

If you are currently using "Page List Select" as the inputfield type you'll want to change to different inputfield type because that inputfield doesn't support limiting selectable pages by anything other than parent. This is explained in the field settings:

2022-05-11_202843.thumb.png.42808e1e79254d2dbd747f916363082d.png

Link to comment
Share on other sites

10 minutes ago, Robin S said:

If you are currently using "Page List Select" as the inputfield type you'll want to change to different inputfield type because that inputfield doesn't support limiting selectable pages by anything other than parent. This is explained in the field settings:

2022-05-11_202843.thumb.png.42808e1e79254d2dbd747f916363082d.png

@Robin S Ah ha, nailed it!

My bad for not reading that part of the description. Now changed to checkboxes and it's working a charm.

Thanks again for your help. Seriously useful!

Link to comment
Share on other sites

  • prestoav changed the title to [SOLVED] Page Field Select From Grand Parent of Page?

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...