Jump to content

How to set template or parent restrictions through API for page-field?


Recommended Posts

Posted

I am creating page-field through API, and I want to set it to allow only certain template and parent. How to achieve this?

Posted

Here are the fields you can set with a Page field:

<?php

$f = $fields->get("some_page_field"); 

// InputfieldPage settings
$f->parent_id = 123; // ID of allowed parent page
$f->template_id = 4; // ID of allowed template
$f->findPagesCode = 'return $page->parent->children();'; // alternate code to use for included pages
$f->labelFieldName = 'title'; // name of field to use as label (default is title)
$f->inputfield = 'InputfieldSelect'; // class name of inputfield you want to delegate selection to

// FieldtypePage settings
$f->derefAsPage = FieldtypePage::derefAsPageArray; // Multiple pages (PageArray)
$f->derefAsPage = FieldtypePage::derefAsPageOrFalse; // Single page (Page) or boolean false when none selected
$f->derefAsPage = FieldtypePage::derefAsPageOrNullPage; // Single page (Page) or empty page (NullPage) when non selected

The Inputfield settings only affect what is displayed in the admin (i.e. for locating what pages it should display as selectable).

  • Like 3
  • 3 years later...
Posted

Reviving an old topic but which hook should I use for changing this behaviour with a module. When I use this code in admin.php it works perfectly but I could manage to use it with hooks.

$field = $fields->get("product_group");
$field->findPagesCode = 'return $page->rootParent->child("name=product-groups")->children();';
Posted

UPDATE: I saw this post from apeisa after posting this. ProcessPageEdit::buildForm worked like a charm.

public function init() {

        $this->addHookAfter("ProcessPageEdit::buildForm", $this, 'setProductGroup');
    }

    public function setProductGroup(HookEvent $event)
    {
        $form = $event->return;
        $field = $form->get('product_group');
        if($field) {

            $field->findPagesCode = 'return $page->rootParent->child("name=product-groups")->children();';
        }

        $event->return = $form;
    }

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
  • Recently Browsing   0 members

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