Jump to content

Extending the Page class - How to access fields?


Recommended Posts

I'm trying out extending the Page class to add methods to pages of a particular template. I've discovered that when I try to use a magic method to access one of my fields from a method within the class (i.e. $this->title) it returns nothing. I am used to being able to call $event->object->title from within a hook, so I expected this to be even simpler when using a custom class. I can output $this->id okay, but not any of my custom fields. What am I missing here?

Here is my code:

<?php
class PodcastEpisode extends Page {

	public function getFileUrl() {
		return $this->podcast_file ? $this->podcast_file->httpUrl : $this->podcast_file_url;
	}

	public function getFileSize() {
		return $this->podcast_file ? $this->podcast_file->filesize : $this->podcast_file_size;
	}

	public function getFileExt() {
		return $this->podcast_file ? $this->podcast_file->ext : pathinfo($this->podcast_file_url, PATHINFO_EXTENSION);
	}

	public function getFileType() {
		$fileType = '';

		$fileTypes = array(
			'audio/mpeg' => 'mp3',
			'audio/x-m4a' => 'm4a',
			'video/mp4' => 'mp4',
			'video/x-m4v' => 'm4v',
			'video/quicktime' => 'mov',
			'application/pdf' => 'pdf',
			'document/x-epub' => 'epub'
		);

		foreach($fileTypes as $key => $value) {
			if(strcasecmp($value, $this->getfileExt()) == 0) { //If the value matches the file extension
				$fileType = $key; //Set $fileType to the corresponding File Type string from the $fileTypes array
				break;
			}
		}

		return $fileType;
	}
}
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...