Jump to content

Show a field in admin only if the page you are editing has children


cb2004
 Share

Recommended Posts

this in your /site/ready.php

<?php
$wire->addHookAfter("ProcessPageEdit::buildForm", function(HookEvent $event){
    $process = $event->object;
    $form = $event->return;
    $page = $process->getPage();

    // remove field when actual page has no children
    if($page->numChildren == 0) {
        $form->remove($form->getChildByName('your_field_name'));
    }
});
  • Like 2
Link to comment
Share on other sites

hi cb2004,

glad it worked.

1) google

https://www.google.at/search?q=site:processwire.com+hooks&gws_rd=cr&ei=IzeMVuCqMYSnU7jZuIAK

2) the helloworld module

https://github.com/ryancramerdesign/ProcessWire/blob/master/site-default/modules/Helloworld.module

3) blog (especially for my suggested way of using ready.php)

https://processwire.com/blog/posts/processwire-2.6.7-core-updates-and-more/

i recommend reading the blog regularly - that's a great place to learn :)

4) go through the modules directory and see how others do it. start with simple modules and then go further. and one day you will have to dive into the "wire" folder ;)

have fun :)

Link to comment
Share on other sites

Yeah I jumped into the blog post shortly after and came up with the following:

<?php

$pages->addHookAfter("ProcessPageEdit::buildForm", function($event) {
	$form = $event->return;
	$page = $event->object;

	if ($page->template->id == 47 && $page->numChildren == 0) {
		$form->remove($form->getChildByName("rte_basic_2"));
	}
});
  • Like 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...