Ad

Access Partial's Components In PHP Section In OctoberCMS

- 1 answer

I need to access and modify a component that I am loading in a partial in its own PHP section.

Usually I load components in pages and I can access them using $this->page->components['exampleComponent'] in the PHP section. But when the component is loaded in a partial then it is not there.

I have tried onStart and onEnd to test if its due to October's lifecycle but they are never added to page components.

How can I access it then?

description = "Sample Partial"

[someComponent exampleComponent]
code = "header-menu"

==
<?php
function onStart(){

   // Need to access partial's component here
   $exampleComponent = ??? 
}
?>
==
Ad

Answer

You can use controller object for that as It manages partial stack.

In docs they mentioned that partial has different life-cycle then pages so may be that is the reason it's components are not in page object

There is convenient method in controller findComponentByName('component-name') to search component from page or partial

So you can use below code to get your component.

description = "Sample Partial"

[someComponent exampleComponent]
code = "header-menu"

==
<?php
function onStart(){
   // Need to access partial's component here 
   $exampleComponent = $this->controller->findComponentByName('exampleComponent');
   //           USE controller obj ^ and this method ^
}
?>

It should work.

If any doubt please comment.

Ad
source: stackoverflow.com
Ad