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