Using a custom model method from $page

Hello,

I have a question: I have a custom model Article derived from Page, which defines getCoverImage() function:

public function getCoverImage() {
  if ($this->coverImage() != '')
    return $this->image($this->coverImage());
  return false;
}

How can I use the $page property in the global template to call this function?

I want to show the image in the facebook open graph tags in the header of the page: in header.php snippet for a particular article:

<?php if ($page->getCoverImage()): ?>
<meta property="og:image" content="<?php echo $page->getCoverImage()->url() ?>" />
<?php endif ?>

Thanks

Don’t know if this is the best way to handle this, but should be safe:

<?php if(method_exists($page , 'getCoverImage' ) && $image = $page->getCoverImage()): ?>
  <meta property="og:image" content="<?php echo $image->url() ?>" />
<?php endif ?>

Would that really work if the method is defined on Article class derived from Page? Or should I define this function on the Page model instead somehow?

You can define a custom page method in a plugin to make it available for different templates as described here.

Yes, it will work. But as @flokosiol suggested, you can define a custom page method instead of a page model. You can stick with the page model if you don’t need that method anywhere else.

Thanks guys, this helps!