Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Accessing Controller from Popup (on load)

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: 2062 Views
  • Gene
    Avatar
    Community Member
    41 Posts

    Accessing Controller from Popup (on load) Link to this post

    I know you can access the ID of the controller in your DataObject (within a Popup) in the OnBeforeWrite method...

    protected function onBeforeWrite() {
    $entryID = $this->record['ctf[sourceID]'];
    // or use $_POST
    }

    but I am interested in accessing the controller on load...

    public function populateDefaults() {
    // get controller here
    }

    Do you know of anyway to do that without having to write some javascript to get the ID from the hidden input field in the popup?

    G

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Accessing Controller from Popup (on load) Link to this post

    Hi UncleCheese,

    I took a look through the code and figured out what I would need to get the parent controller. In DataObjectManager_Popup would it be possible to alter this line...

    $this->dataObject->getRequirementsForPopup();

    to

    $this->dataObject->getRequirementsForPopup($controller);

    so that in my DataObject I can access the controller calling the popup with...

    public function getRequirementsForPopup($dom) {
       $parentController = $dom->controller;
       $this->myField = $parentController->defaultValueForField;
    }

    Gene

  • UncleCheese
    Avatar
    4085 Posts

    Re: Accessing Controller from Popup (on load) Link to this post

    That seems kind of kludgy.. can I see how you're implementing that code in your DataObject class?

    What does Controller::curr() return in that function? It should give you an instance of the DOM that owns the DataObject.

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Accessing Controller from Popup (on load) Link to this post

    Interesting, Controller::curr() gets me my InvoiceAdmin object (which extends LeftAndMain).

    Basically what I'm trying to do is I have a Company DataObject...

    class Company extends DataObject {
       
       static $db = array(
          'Title'    => 'Varchar(100)',
          'Address1'   => 'Varchar(255)',
       );
       
       static $has_many = array(
          'Invoices'    => 'Invoice',
       );

       public function getCMSFields() {
          //...
          $invoices = new DataObjectManager(
             $this,
             'Invoices',
             'Invoice',
             array(
             ),
             'getCMSFields_forPopup'
          );
          //...
       }
    }

    When the popup is called I want to populate the Company's address as the default value into the Invoice Popup...

    class Invoice extends DataObject {
       
       static $db = array(
          // To
          'ToTitle'       => 'Varchar(100)',
          'ToAddress1'    => 'Varchar(255)',
       );
       
       public function populateDefaults() {
          // Can't get controller here
          // Controller:curr() returns an InvoiceAdmin object
       }
       
       public function getRequirementsForPopup($dom) {
          // Other option is to get controller here
          // Controller:curr() returns an InvoiceAdmin object
          // I want the invoice address to default to the Company's address
          /*
          $company = $dom->controller;
          if ($company) {
             $this->ToTitle       = $company->Title;
             $this->ToAddress1    = $company->Address1;
          }
          */
       }
       
    }

    Can you think of any other way to get access to my Company object before the popup is instantiated?

    Thanks,

    Gene

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Accessing Controller from Popup (on load) Link to this post

    Well, I figured out how to get the page calling the popup without having to hack up DOM, yay . In populateDefaults or (getRequirementsForPopup) you can get the current controller with Controller::current() (as UncleCheese mentioned). That got me my admin page. In my case that was InvoiceAdmin which inherited from LeftAndMain. From there you can call 'currentPage' which will get you the page calling the popup.

    public function getRequirementsForPopup() {
          $adminController = Controller::curr();
          $page = $adminController->currentPage();
    }

    Still seems pretty kludgy, but it works. Thanks for the help UncleCheese!

    G

    2062 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.