Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » $has_one with SiteTree links to page and doesn't show SimpleTreeDropDown

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 921 Views
  • updog
    Avatar
    Community Member
    13 Posts

    $has_one with SiteTree links to page and doesn't show SimpleTreeDropDown Link to this post

    I am building a service comparison chart that sits on a ServiceComparisonPage type. The data objects aren't necessarily linked to the page type but as there will be only one of these charts in the site, it will make it easier for the user to edit it here.

    I have managed to use the DataObjectManager to build a new tab for each dataobject and allow management of objects. The problem I am having is that I want each service object to be able to link to any page in the site however because the management of data objects is on a Page class, the LinkToID field is hidden on the pop up form and set to the page that it is contained in by default. Code below FYI.

    // ServiceComparisonPage

    <?php
    class ServiceComparisonPage extends Page {

       public static $db = array(
       );

       public static $has_one = array(
       );
       
       function getCMSFields() {
       
        $fields = parent::getCMSFields();
       
        $fields->addFieldToTab("Root.Content.Services", new DataObjectManager(
             $this,
             'Services',
             'Service',
             null,
             'getCMSFields_forPopup'
          ));
          
        return $fields;
       }

    }
    class ServiceComparisonPage_Controller extends Page_Controller {

       public static $allowed_actions = array (
       );

       public function init() {
          parent::init();

       }

       public function Services()
       {
          return DataObject::get('Service');
       }
       
       public function FeatureGroups() {
          return DataObject::get('FeatureGroup');
       }
       
    }

    // This is the Service Object

    class Service extends DataObject {
       
       static $db = array(
          'Title' => 'Text'
       );

       static $many_many = array(
          'Features' => 'Feature',
       );
       
       static $has_one = array(
          'LinkTo' => 'SiteTree'
       );
       
       public function getCMSFields_forPopup()
       {
          return new FieldSet(
             new TextField('Title'),
             new SimpleTreeDropdownField(
                'LinkToID',
                'Please select the page that this service links to...'
             )
          );
       }
       
    }

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: $has_one with SiteTree links to page and doesn't show SimpleTreeDropDown Link to this post

    This problem is caused because ComplexTableField (that DOM enhances) tries to be 'clever' and automatically guesses that the has_one SiteTree relationship is meant to link between the object and the page you're managing the object on.

    From what you've said, the objects arent linked to specific page instances, so you want to turn this feature off. So you'll need to do something like this:

    ...
    $dom = new DataObjectManager(
    $this,
    'Services',
    'Service',
    null,
    'getCMSFields_forPopup'
    );
    $dom->setRelationAutoSetting(false);
    $fields->addFieldToTab('Root.Content.Services',$dom);
    ...

    Hope that sorts this out for you.

  • updog
    Avatar
    Community Member
    13 Posts
    921 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.