1<?php 2 3namespace ComboStrap\Meta\Field; 4 5use ComboStrap\ExceptionNotFound; 6use ComboStrap\FirstImage; 7use ComboStrap\MarkupPath; 8use ComboStrap\Meta\Api\Metadata; 9use ComboStrap\Meta\Api\MetadataImage; 10use ComboStrap\PageImageTag; 11use ComboStrap\WikiPath; 12 13/** 14 * Retrieve the featured image of the ancestor 15 * 16 * Can be used in a {@link PageImageTag} 17 */ 18class AncestorImage extends MetadataImage 19{ 20 21 22 const PROPERTY_NAME = "ancestor-image"; 23 24 public static function createFromResourcePage(MarkupPath $page): AncestorImage 25 { 26 return (new AncestorImage())->setResource($page); 27 } 28 29 static public function getDescription(): string 30 { 31 return "The featured image from the closest ancestor page"; 32 } 33 34 static public function getLabel(): string 35 { 36 return "Ancestor Image"; 37 } 38 39 public static function getName(): string 40 { 41 return self::PROPERTY_NAME; 42 } 43 44 static public function getPersistenceType(): string 45 { 46 return Metadata::DERIVED_METADATA; 47 } 48 49 50 static public function isMutable(): bool 51 { 52 return false; 53 } 54 55 public function getValue(): WikiPath 56 { 57 58 $contextPage = $this->getResource(); 59 $actual = $contextPage; 60 while (true) { 61 try { 62 $actual = $actual->getParent(); 63 } catch (ExceptionNotFound $e) { 64 break; 65 } 66 try { 67 return FeaturedImage::createFromResourcePage($actual)->getValue(); 68 } catch (ExceptionNotFound $e) { 69 // ok 70 } 71 try { 72 /** 73 * If this is a index page, 74 * the first image is generally a prominent image 75 */ 76 return FirstImage::createForPage($actual)->getValue(); 77 } catch (ExceptionNotFound $e) { 78 // ok 79 } 80 } 81 throw new ExceptionNotFound(); 82 83 } 84 85 86 static public function getDrive(): string 87 { 88 return WikiPath::MEDIA_DRIVE; 89 } 90 91 92 static public function isOnForm(): bool 93 { 94 return true; 95 } 96} 97