SHARE
TWEET

Untitled

a guest Aug 25th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. use DNADesign\Elemental\Extensions\ElementalPageExtension;
  4. use DNADesign\Elemental\Models\BaseElement;
  5. use SilverStripe\Admin\ModelAdmin;
  6. use SilverStripe\Control\Director;
  7. use SilverStripe\ORM\DataObject;
  8.  
  9. class TestObject extends DataObject
  10. {
  11.  
  12.     private static $table_name = 'TestObject';
  13.     private static $extensions = [ElementalPageExtension::class];
  14.  
  15.     public function CMSEditLink() {
  16.         $link = 'admin/test-model-admin/TestObject/EditForm/field/TestObject/item' . $this->ID;
  17.         return Director::absoluteURL($link);
  18.     }
  19.  
  20.     public function Link($action = null)
  21.     {
  22.         return '';
  23.     }
  24.  
  25. }
  26.  
  27. class TestObjectModelAdmin extends ModelAdmin
  28. {
  29.     private static $managed_models = [TestObject::class];
  30.     private static $url_segment = 'test-model-admin';
  31. }
  32.  
  33. class CallToActionBlock extends BaseElement
  34. {
  35.     private static $db = [
  36.         'CallToActionLink' => 'Link',
  37.     ];
  38.  
  39.     private static $table_name = 'CallToActionBlock';
  40.  
  41.     public function getType()
  42.     {
  43.         return 'Call to action block';
  44.     }
  45.  
  46.     /**
  47.      * For the frontend, return a parsed set of data for use in templates
  48.      *
  49.      * @return ArrayData|null
  50.      */
  51.     public function CallToActionLink()
  52.     {
  53.         return $this->decodeLinkData($this->getField('CallToActionLink'));
  54.     }
  55.  
  56.     /**
  57.      * Given a set of JSON data, decode it, attach the relevant Page object and return as ArrayData
  58.      *
  59.      * @param string $linkJson
  60.      * @return ArrayData|null
  61.      */
  62.     protected function decodeLinkData($linkJson)
  63.     {
  64.         if (!$linkJson || $linkJson === 'null') {
  65.             return;
  66.         }
  67.  
  68.         $data = ArrayData::create(json_decode($linkJson));
  69.  
  70.         // Link page, if selected
  71.         if ($data->PageID) {
  72.             $data->setField('Page', self::get_by_id(SiteTree::class, $data->PageID));
  73.         }
  74.  
  75.         return $data;
  76.     }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top