Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Accessing Controller from Popup (on load)


Reply


5 Posts   2208 Views

Avatar
Gene

Community Member, 41 Posts

24 April 2010 at 6:29am

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

Avatar
Gene

Community Member, 41 Posts

29 April 2010 at 7:13pm

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

Avatar
UncleCheese

Forum Moderator, 4096 Posts

30 April 2010 at 2:26am

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.

Avatar
Gene

Community Member, 41 Posts

30 April 2010 at 3:51am

Edited: 30/04/2010 4:03am

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

Avatar
Gene

Community Member, 41 Posts

2 May 2010 at 9:35am

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