Advertisement
heuert

DataObject and SiteTree or Page and custom CMS fields

Jul 11th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. Hi,
  2.  
  3. I would like to know how to best go about adding a relatively simple has_one relationship. I have a class extending DataObject, lets call it EditedAuthor. It has 2 simple Text data fields.
  4. So, the code looks like this (at the moment):
  5.  
  6. <?php
  7. class EditedAuthor extends DataObject {
  8.  
  9.     public static $db = array(
  10.         'AuthorTitle' => 'Text',
  11.         'AuthorCaption' => 'Text',
  12.     );
  13.     //...
  14.     public function getCMSFields() {
  15.         $fields = new FieldGroup();
  16.         $fields->push(TextareaField::create('AuthorTitle', 'Author title', $this->AuthorTitle));
  17.         $fields->push(TextareaField::create('AuthorCaption', 'Author caption', $this->AuthorCaption));
  18.         return $fields;
  19.     }
  20. }
  21. ?>
  22.  
  23. I have a custom Page Type extending SiteTree (actually our custom Page which extends SiteTree).
  24.  
  25. In that I have a has_one relationship to EditedAuthor:
  26.  
  27. <?php
  28. class InformationPage extends Page {
  29.  
  30.     // ...
  31.     static $has_one = array(
  32.         'Contributors' => 'EditedAuthor',
  33.     );
  34.     // ...
  35.     public function getCMSFields() {
  36.         // ...
  37.         if (Permission::check('ADMIN')) {
  38.             $authorsEditField = singleton('EditedAuthor')->getCMSFields();
  39.             $fields->addFieldsToTab('Root.Main', $authorsEditField);
  40.         }
  41.     }
  42.     // ...
  43. }
  44. ?>
  45.  
  46. I couldn't find in the docs how to best do this. The requirement is that I have a DataObject that is not a page type but can be added to a page type because I may want to re-use it later.
  47. Also, this EditedAuthor should be editable only by admins and then save to the DB.
  48. What is the way to do this?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement