Jump to content

Show field only when page status is published


Recommended Posts

@Markus (Blue Tomato) the showIf only works with fields and there values. AFAIK, the published / unpublished value cannot be used as condition for showIf directly in the template editor.

But you may define a hook and set the visibility of that field within the hook. Here is example code as a starting point. You can use this in site/ready.php

$wire->addHookAfter('ProcessPageEdit::buildForm', function (HookEvent $event) {
    
	// make sure we're editing a page and not a user
    if($event->process != 'ProcessPageEdit') return;

    // get page and form
    $page = $event->object->getPage();
    $form = $event->return;

	// optionally check for matching template(s) with a condition
	if('mydesiredtemplatename' != $page->template->name) return;
	
	// get the field and conditionally set the visibility
	$form->get('yourfieldname')->collapsed = $page->isUnpublished() ? Inputfield::collapsedHidden : Inputfield::collapsedNo;

    $event->return = $form;

    return;
}

 

  • Like 5
  • 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...