Jump to content
Markus (Blue Tomato)

Show field only when page status is published

Recommended Posts

Hi,

Do anyone know how to show only a field, if the page is published? I tried `status=published` but this did not work. The field keep invisible also when the page is published.

Share this post


Link to post
Share on other sites

I am not sure I quite understand your problem. But maybe this API reference can help? If not, feel free to explain yourself a little better🤓. Source code would be the best.

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...