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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...