5

Is it possible to override the com_users save function?

This is what I tried so far...

<?php
echo JPATH_SITE.'/components/com_users/models/profile.php';
require_once JPATH_SITE.'/components/com_users/models/profile.php';

class NbrealUsersModelProfile extends UsersModelProfile{
    public function save($data){
        die("it works");
    }
}
?>

I added this on the template index file..

1

1 Answer 1

3

This docs page should help you with this:

https://docs.joomla.org/How_to_override_the_component_mvc_from_the_Joomla!_core

NOTE: this does not work on its own, instead you need to create a plugin for your overridden controller:

There may be times when you need to override the Joomla! MVC Component to add new functionality or to completely customise the component. The MVC component can be completely overridden by using the same class names that are used in the component. This is achieved by loading a system plugin before the component is dispatched by the application.

You can create your own plugin to do this.

class plgSystemComContentOverride extends JPlugin {
  public function __construct(&$subject, $config = array()) {
     parent::__construct($subject, $config);
 }

 public function onAfterRoute() {
     $app = JFactory::getApplication();
     if('com_content' == JRequest::getCMD('option') && !$app->isAdmin()) {
         require_once(dirname(__FILE__) . '/comcontentoverride/my_content_controller.php');
     }
 } 

}

in your case you need to modify it to something like

class plgSystemComContentOverride extends JPlugin {
  public function __construct(&$subject, $config = array()) {
     parent::__construct($subject, $config);
 }

 public function onAfterRoute() {
     $app = JFactory::getApplication();
     if('com_user' == JRequest::getCMD('option') && !$app->isAdmin()) {
         require_once(dirname(__FILE__) . '/../../templates/{template_name}/code/com_users/profile.php');
     }
 } 
}
4
  • Can you give me a working example, including the file you used to make the override? I need to override the profile.php model, function save()
    – miguelmpn
    Commented Sep 14, 2015 at 10:29
  • I have this code on the folder /templates/{template_name}/code/com_users/profile.php <?php defined('_JEXEC') or die; use Joomla\Registry\Registry; class UsersModelProfile extends JModelForm{ protected $data; public function save($data){ die; } }
    – miguelmpn
    Commented Sep 15, 2015 at 9:08
  • I also tried to require_once the original file and extending the class...
    – miguelmpn
    Commented Sep 15, 2015 at 9:09
  • You need to add and import a new plugin. See my expanded answer
    – jdog
    Commented Sep 16, 2015 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.