Jump to content

Is it possible to show a page table only if there are items in it?


Juergen
 Share

Recommended Posts

Hello @ all,

I have tried to create the following scenario:

The page table field should be hidden in the (parent) template, if there are no items in it.

If items (child pages) were created, the page table field should be visible in the (parent) template.

Inputfield dependencies seem to be the wrong way because the inputfield depency is depending on the value of another field. As far as I know it is not possible in this case to check if the inputfield (page table) itself is empty or not.

Another possible way would be to hook into page render:

$this->addHookBefore('PageRender::renderPage', $this, 'rendereventpagetable');

    public function rendereventpagetable($event)
    {
        $page = $event->arguments[0]; 
     //hide page table if there are no events in it   
      if (($page->template == "events") AND ($page->children == "0")) {
        //SET the status of the pagetablefield to hidden but how?
      }
    }

Does anyone know how to set the status of a field to hidden or maybe how to add a css class to hide the field?

Best regards

Link to comment
Share on other sites

Sorry but I cannot get it to work. I have tried it with this hook function.

    public function rendereventpagetable(HookEvent $event)
    {
        // get the table field
        $table = $event->object;
        // make sure this is our field
        if ($table->name !== "singleeventtable")
            return;
        if (count($table->attr("value")) == 0) {
            $this->message('Noch keine Termine erstellt');
            $table->collapse = "hidden";
        }
    }

It shows me the message "Noch keine Termine erstellt" (in English: No events created), but the page table is still visible.

Can you point me into the right direction?

Link to comment
Share on other sites

No this doesnt work either. I have also tried to add a specific css class to the page table field in this way:

 $table->addClass('myClassName');

But this will also be ignored. It seems that the page table field is a little bit different like other inputfields. :(

Link to comment
Share on other sites

I have tried it in these ways:

$table->collapsed = Inputfield::collapsedHidden;

$table->collapse = Inputfield::collapsedHidden;

$table->collapsed = "hidden";

$table->collapse = "hidden";

For example this works:

$table->label = "test";

The label will be replaced by the word "test".

Link to comment
Share on other sites

If you can set the visibility to hidden in the field's settings then the above change should work as well, but maybe you need to hook somewhere else, so that the render() method won't actually be called for that field (because it's hidden).

Link to comment
Share on other sites

Have you tried hooking ProcessPageEdit::buildFormContent? Like this;

$pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
    $wrapper = $event->return;
    $wrapper->YOUR_FIELD->collapsed = Inputfield::collapsedHidden;
});

If that works, you can add conditional logic to only apply it as needed.  I usually apply this kind of hook in site/ready.php.

  • Like 2
Link to comment
Share on other sites

Hello netcarver,

your code as standalone works!!

I have tried to combine it:

  $this->addHookAfter("InputfieldPageTable::render", $this, "rendereventpagetable");


   public function rendereventpagetable(HookEvent $event)
    {
        // get the table field
        $table = $event->object;        
        // make sure this is our field
        if ($table->name !== "singleeventtable")
            return;
        if (count($table->attr("value")) == 0) {
            $this->error('Achtung! Sie haben noch keine Termine erstellt');
            //START            
            $this->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
            $wrapper = $event->return;
            $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden;
            });
            //END
        }
    } 

But in this case it shows me the message, but the page table field is still visible

Link to comment
Share on other sites

Now that's some hookception :D

$pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
  $wrapper = $event->return;
  if(!$wrapper->has('singleeventtable')) return;

  if (count($wrapper->singleeventtable->attr("value")) == 0) {
    $this->error('Achtung! Sie haben noch keine Termine erstellt');
    $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden;
  }
});
  • Like 4
Link to comment
Share on other sites

This works!!!

Here is the complete code for others who are interested in:

//initialize the function in the module
public function init(){
$this->addHookAfter("ProcessPageEdit::buildFormContent", $this, "renderpagetable");
}

public function rendepagetable($event){
 $wrapper = $event->return;
 $page = $event->object->getPage();
  if(!$wrapper->has('singleeventtable')) return;
  $itemnumber = count($page->children); 
  if ($itemnumber == 0) {
    $this->message('There are no items in the page table field');
    $wrapper->singleeventtable->collapsed = Inputfield::collapsedHidden;
  } else {
    $this->message("Number of items:{$itemnumber}");
  }
}
}

"singleeventtable" is the name of my page table field - replace it with your own page table field name.

Special thanks to netcarver and LostKobraKai for helping to find a working solution.

  • Like 3
Link to comment
Share on other sites

  • 7 months later...
  • 1 month later...

If you use the init.php introduced in PW 2.6.7 you can add this piece of code to the init. php:

// show pagetable field only if there are items in it  
$pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
    $wrapper = $event->return;
    if($wrapper->has('NAMEOFYOURPAGETABLEFIELD')){
    if ($event->object->getPage()->numChildren > 0) return;
    $wrapper->NAMEOFYOURPAGETABLEFIELD->collapsed = Inputfield::collapsedHidden;
    }    
});

 

  • Like 2
Link to comment
Share on other sites

// show pagetable field only if there are items in it  
$pages->addHookAfter("ProcessPageEdit::buildFormContent", function($event) {
    $wrapper = $event->return;
    if (!$wrapper->has('NAMEOFYOURPAGETABLEFIELD')) return;
    if ($event->object->getPage()->NAMEOFYOURPAGETABLEFIELD->count) return;
    $wrapper->NAMEOFYOURPAGETABLEFIELD->collapsed = Inputfield::collapsedHidden;
});

This is more reliable as it does also work if page table entries are not stored as children or if there are other children for the page besides the pagetable ones.

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