Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Page extends SiteTree {
- private static $db = array(
- );
- private static $has_one = array(
- 'Photo' => 'Image'
- );
- public function getCMSFields() {
- $fields = parent::getCMSFields();
- $fields->addFieldToTab('Root.Images', $photo = UploadField::create('Photo'));
- $photo->getValidator()->setAllowedExtensions(['png','gif','jpg','jpeg']);
- $photo->setFolderName('page-headers');
- return $fields;
- }
- public function HeaderImage(){
- if($this->Photo()->exists()){
- echo "I have an image<br>";
- return $this->Photo();
- }else{
- echo "I don't have an image.<br>";
- return $this->getParentImage( $this );
- }
- }
- private function getParentImage( Page $page ){
- $parent = $page->Parent;
- if($parent->Photo()->exists()){
- return $parent->Photo();
- }elseif( $parent ){
- return $this->getParentImage( $parent );
- }
- return null;
- }
- }
- class Page_Controller extends ContentController {
- /**
- * An array of actions that can be accessed via a request. Each array element should be an action name, and the
- * permissions or conditions required to allow the user to access it.
- *
- * <code>
- * array (
- * 'action', // anyone can access this action
- * 'action' => true, // same as above
- * 'action' => 'ADMIN', // you must have ADMIN permissions to access this action
- * 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true
- * );
- * </code>
- *
- * @var array
- */
- private static $allowed_actions = array (
- );
- public function init() {
- parent::init();
- // You can include any CSS or JS required by your project here.
- // See: http://doc.silverstripe.org/framework/en/reference/requirements
- }
- }
- =======================
- <div style="width: 100%; margin: 0px auto;">
- <div style="margin: 0px auto;">
- <% if $Photo %>
- <h2>This is my photo. It's 1900X300</h2>
- $Photo.CroppedImage(1900,300)
- <% else %>
- <h2>I do not have an image.</h2>
- <% end_if %>
- <% if $HeaderImage %>
- <h2>This is my header image. It's 1900X300</h2>
- $HeaderImage.CroppedImage(1900,300)
- <% else %>
- <h2>I do not have an image.</h2>
- <% end_if %>
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement