Post by icetalker » Sat Apr 19, 2025 10:12 am

Since ocmod extension is back, I was trying to add a dynamic button on the admin footer. The change of the view "admin/view/template/common/footer.twig" just work fine, it generated a new file in "extension/ocmod" folder and have no problem in loading it in frontend. When it come to the controller “admin/controller/common/footer.php”, it also generated a file in "extension/ocmod" folder, but it looks like the controller never been able to be loaded. Then I try to change contents on “admin/controller/common/header.php” and "admin/controller/cms/comment.php" for test, code example as below:

Code: Select all

<file path="admin/controller/cms/comment.php">
        <operation>
            <search>
                <![CDATA[Opencart\Admin\Controller\Cms;]]>
            </search>
            <add position="replace">
                <![CDATA[Opencart\Admin\Controller\Extension\Ocmod\Cms;]]>
            </add>
        </operation>
	<operation>
		<search><![CDATA['text' => $this->language->get('heading_title'),]]></search>
		<add position="replace"><![CDATA['text' => 'Test Title',]]></add>
	</operation>
</file>
 <file path="admin/controller/common/header.php">
        <operation>
            <search>
                <![CDATA[Opencart\Admin\Controller\Common;]]>
            </search>
            <add position="replace">
                <![CDATA[Opencart\Admin\Controller\Extension\Ocmod\Common;]]>
            </add>
        </operation>
	<operation>
		<search><![CDATA['name' => $result['name'],]]></search>
		<add position="replace"><![CDATA['name' => 'Test User',]]></add>
	</operation>
</file>
It works fine on "cms/comment.php", but didn't work on "commons/header.php". Does that mean files on "common" folder could not be loaded by just change "***.ocmod.xml"? I am not really fimilar with the event system, anyone could help out? Thank you!
my version is 4.1.0.3

Newbie

Posts

Joined
Fri Sep 18, 2020 5:00 pm

Post by grgr » Fri Apr 25, 2025 6:24 pm

OCMOD currently only works on the index() function for controller files (and not at all for model files?), so that is proably why you got the results you did.

Already reported and much discussed. Hopefully will be fixed, but events work perfectly well for most needs.

-
Image Image Image
VIEW ALL EXTENSIONS * EXTENSION SUPPORT * WEBSITE * CUSTOM REQUESTS


User avatar
Active Member

Posts

Joined
Mon Mar 28, 2011 4:08 pm
Location - UK

Post by icetalker » Wed Apr 30, 2025 7:11 pm

Thank you for your comment. I spent some time to learn about event system. After go through the source code, I found these controllers in "common" is loaded by a component called "loader"(i.e. Opencart\System\Engine\Loader), and there is no way to replace the controller "common\footer" with my customized footer controller by using `event->register()`. I ended up with pre-registering the "common/footer" controller befote it is called in the loader. Although it works fine in my case, I doubt if this is the right way to use the event system. Not sure if my rough understanding of the event system is correct or not. Correct me if there is any better way of doing it.

My example code are as below:

Code: Select all

//extension/my_extension_namespace/admin/module/my_extension.php
class MyExtension extends \Opencart\System\Engine\Controller {
	...
	public function install(): void {
		if ($this->user->hasPermission('modify', 'extension/module')) {
			
			$startup_data = [
				'code'        => 'my_code',
				'description' => '',
				'action'      => 'admin/extension/my_extension_namespace/startup/my_class',
				'status'      => 1,
				'sort_order'  => 2
			];

			$this->load->model('setting/startup');

			$this->model_setting_startup->addStartup($startup_data);
		}
	}
	...
}

//extension/my_extension_namespace/admin/controller/startup/my_class.php
class MyClass extends \Opencart\System\Engine\Controller {
	public function index(): void {
		
		$key = 'fallback_controller_' . str_replace('/', '_', 'common/footer');

		$object = new \Opencart\Admin\Controller\Extension\MyExtensionNamespace\Common\Footer($this->registry);
		
		$this->registry->set($key, $object);
	}
grgr wrote:
Fri Apr 25, 2025 6:24 pm
OCMOD currently only works on the index() function for controller files (and not at all for model files?), so that is proably why you got the results you did.

Already reported and much discussed. Hopefully will be fixed, but events work perfectly well for most needs.

Newbie

Posts

Joined
Fri Sep 18, 2020 5:00 pm

Post by grgr » Thu May 01, 2025 7:16 am

Seems a bit extreme for a simple event.

Add an event to t DB:

Code: Select all

/*	
Name: 		admin_header_default_store_name
Trigger:	admin/view/common/header/before
Action:		extension/EXTENSION_FOLDER/module/EXTENSION_FILE.adminHeader
Order:		1
Purpose:	Changes the name of the default store in the dropdown menu
*/
And then the function is simply:

Code: Select all

public function adminHeader(&$route, &$data){
	$data['stores'][0] = [
		'name' => 'Different Store Name',
	];		
}

-
Image Image Image
VIEW ALL EXTENSIONS * EXTENSION SUPPORT * WEBSITE * CUSTOM REQUESTS


User avatar
Active Member

Posts

Joined
Mon Mar 28, 2011 4:08 pm
Location - UK

Post by icetalker » Thu May 01, 2025 9:25 pm

@grgr I appreciate your help. These example codes did help me better understand the event system. And it make everything sense. Have a nice day!

Newbie

Posts

Joined
Fri Sep 18, 2020 5:00 pm
Who is online

Users browsing this forum: No registered users and 2 guests