Panel: access the current page object from a custom field

Hi everyone,

I am creating a custom field and I need to get the current page preview url in my controller.
I tried accessing the page() object but it always gives me the home page.

Let’s take a simple example:

<?php
class CustomField extends InfoField {
  // this will display the preview url in the form
  public function __construct() {
    $this->text = 'Your page will be served at '.$currentPage->url(); // $currentPage is what I need
  }
}

Any idea?

EDIT:
I’ve made it work with this fixture:

<?php
class CustomField extends InfoField {
  // this will display the preview url in the form
  public function __construct() {
    $page = page(str_replace('views/pages/show/', '', panel()->path)); // awful, but works
    $this->text = 'Your page will be served at '.$page->url();
  }
}

I’m still looking for a proper way to do this.

1 Like

Hello !
I’m also looking for the proper way to get the current page object in a panel field.
Any idea someone ?
I will use your trick for the moment, thank noclat.

You can use $this->page() in your field’s methods.

I will not work within the above __construct() function. If you change it to

<?php
class CustomField extends InfoField {

  public function input() {
    return '<div class="text">Your page will be served at ' . $this->page()->url() . '</div>';
  }
}	

it will.