how to call a plugin after user login in joomla 3.0 - joomla

I am trying to auto create jomsocial albums using a plugin after adding the jomsocial library in plugin in the event function onUserLogin. onUserLogin if i am trying to fetch the $my = JFactory::getUser(); is returs null value. SO the jomsocial library also act same with user values.

Can't be sure without any of you code to look at, but the Joomla user plugin uses this when it starts:
public function onUserLogin($user, $options = array())
{
$instance = $this->_getUser($user, $options);

Related

Call Custom function in joomla plugin by url.

I have created a system plugin in joomla and created a custom method that is mymethod(). Now i want to call this method via ajax. I have tried link but it will create new ajax plugin but i want to call system plugin custom method not create new plugin.
You can use the plugin system event onAfterInitialise().
Use this url for ajax: index.php?type=mymethod
This leads to:
function onAfterInitialise() {
$jinput = JFactory::getApplication()->input;
if($jinput->get('type')=='mymethod') {
// your code here
}
}
The link is ok. You just need to change the folder name from ajax to system. Prior to joomla 3.4 it was mandatory to place your plugin in ajax folder but now you can place in any folder. Your code will look like this
JPluginHelper::importPlugin('system');
$plugin = ucfirst($input->get('plugin'));
$dispatcher = JEventDispatcher::getInstance();
try
{
$results = $dispatcher->trigger('myMethod' . $plugin);
}
catch (Exception $e)
{
$results = $e;
}
Follow rest instruction as given there.

Joomla 2.5 onAfterInitialise event not being triggered

I'm trying to write a single-signon extension so that our MediaWiki users with the correct permissions don't need to log in to our Joomla 2.5. I can't get it to work, because the onAfterInitialise event won't trigger (neither does onAfterRoute or onAfterDispatch if I try to use those instead). I know the extension is actually running because the onUserAuthentication event is triggering and logging me in as my test user.
Below is my code with the two events, the first won't trigger and execute the die() statement, the second triggers after login and unconditionally authenticates me properly.
Is there something I'm missing here like that one extension can't use two different categories of events or something?
class plgAuthenticationMwSSO extends JPlugin {
function __construct( &$subject, $config ) {
parent::__construct( $subject, $config );
}
public function onAfterInitialise() {
die('testing');
}
public function onUserAuthenticate( $creds, $opt, &$response ) {
$response->username = 'Foo';
$response->fullname = 'Foo Bar';
$response->email = 'foo#bar.baz';
$response->status = JAuthentication::STATUS_SUCCESS;
$response->error_message = '';
}
}
You need to put the onAfterInitialise in a system plugin. As a parallel example, notice how the Remember plugin is a system plugin and then the Cookie plugin is an authentication one. System plugins are checked very early in the stack and are checked on every page load. Authentication plugins are checked when authentication starts and are specifically loaded as a group at certain times. Since you have an authentication plugin, it is not triggered at the right time to respond to the system events that you are looking for.

Is it possible to temporarily disable event in Laravel?

I have the following code in 'saved' model event:
Session::flash('info', 'Data has been saved.')`
so everytime the model is saved I can have a flash message to inform users. The problem is, sometimes I just need to update a field like 'status' or increment a 'counter' and I don't need a flash message for this. So, is it possible to temporarily disable triggering the model event? Or is there any Eloquent method like $model->save() that doesn't trigger 'saved' event?
Solution for Laravel 8.x and 9.x
With Laravel 8 it became even easier, just use saveQuietly method:
$user = User::find(1);
$user->name = 'John';
$user->saveQuietly();
Laravel 8.x docs
Laravel 9.x docs
Solution for Laravel 7.x, 8.x and 9.x
On Laravel 7 (or 8 or 9) wrap your code that throws events as per below:
$user = User::withoutEvents(function () use () {
$user = User::find(1);
$user->name = 'John';
$user->save();
return $user;
});
Laravel 7.x docs
Laravel 8.x docs
Laravel 9.x docs
Solution for Laravel versions from 5.7 to 6.x
The following solution works from the Laravel version 5.7 to 6.x, for older versions check the second part of the answer.
In your model add the following function:
public function saveWithoutEvents(array $options=[])
{
return static::withoutEvents(function() use ($options) {
return $this->save($options);
});
}
Then to save without events proceed as follow:
$user = User::find(1);
$user->name = 'John';
$user->saveWithoutEvents();
For more info check the Laravel 6.x documentation
Solution for Laravel 5.6 and older versions.
In Laravel 5.6 (and previous versions) you can disable and enable again the event observer:
// getting the dispatcher instance (needed to enable again the event observer later on)
$dispatcher = YourModel::getEventDispatcher();
// disabling the events
YourModel::unsetEventDispatcher();
// perform the operation you want
$yourInstance->save();
// enabling the event dispatcher
YourModel::setEventDispatcher($dispatcher);
For more info check the Laravel 5.5 documentation
There is a nice solution, from Taylor's Twitter page:
Add this method to your base model, or if you don't have one, create a trait, or add it to your current model
public function saveQuietly(array $options = [])
{
return static::withoutEvents(function () use ($options) {
return $this->save($options);
});
}
Then in you code, whenever you need to save your model without events get fired, just use this:
$model->foo = 'foo';
$model->bar = 'bar';
$model->saveQuietly();
Very elegant and simple :)
Call the model Object then call unsetEventDispatcher
after that you can do whatever you want without worrying about Event triggering
like this one:
$IncidentModel = new Incident;
$IncidentModel->unsetEventDispatcher();
$incident = $IncidentModel->create($data);
To answer the question for anyone who ends up here looking for the solution, you can disable model listeners on an instance with the unsetEventDispatcher() method:
$flight = App\Flight::create(['name' => 'Flight 10']);
$flight->unsetEventDispatcher();
$flight->save(); // Listeners won't be triggered
In laravel 8.x :
Saving A Single Model Without Events
Sometimes you may wish to "save" a given model without raising any events. You may accomplish this using the saveQuietly method:
$user = User::findOrFail(1);
$user->name = 'Victoria Faith';
$user->saveQuietly();
See Laravel docs
In laravel 7.x you can do that as easy as
use App\User;
$user = User::withoutEvents(function () {
User::findOrFail(1)->delete();
return User::find(2);
});
See more in Laravel 7.x Muting Events documentation
You shouldnt be mixing session flashing with model events - it is not the responsibility of the model to notify the session when something happens.
It would be better for your controller to call the session flash when it saves the model.
This way you have control over when to actually display the message - thus fixing your problem.
Although it's a long time since the question, I've found a way to turn off all events at once. In my case, I'm making a migration script, but I don't want any event to be fired (either from Eloquent or any other).
The thing is to get all the events from the Event class and remove them with the forget method.
Inside my command:
$events = Event::getRawListeners();
foreach ($events as $event_name => $closure) {
Event::forget($event_name);
}
The only thing that worked for me was using the trait WithoutEvents. This will be executed inside the setUp method and does prevent any dispatch you have added to the code.

My custom plugin is not being called after installation in joomla

I created a custom plugin and installed in joomla 2.5. It must be called when user logins.
My plugin has following code..
class plgUserZencartlogin extends JPlugin
{
public function __construct(& $subject, $config){
parent::__construct($subject, $config);
$this->loadLanguage();
}
public function onUserLogin($user, $options = array())
{
//here is my custom code..
}
}
Everything was worked fine upto installation. But is not getting triggered. I've gone through "onUserLogin" event, where it is shown that, it must be triggered automatically when user logins. But nothing was paid off for my hardwork. Thanks in advance..

Linking to the new Joomla 2.5 Controller

As many of you know the controller in Joomla 2.5 changed from
// Create the controller
$classname = 'mycomponentController'.$controller;
$controller = new $classname( );
// Perform the Request task
$controller->execute( JRequest::getVar('task'));
// Redirect if set by the controller
$controller->redirect();
to something along the lines of
// Get an instance of the controller prefixed by the component
$controller = JController::getInstance('mycomponent');
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
// Redirect if set by the controller
$controller->redirect();
Now in Joomla 1.5 as well as by using the table you could run a task by running the link
index.php?option=com_mycomponent&controller=specificcontroller&task=randomtask
However this style of link doesn't work with the new controller - does anyone know how to format this link in Joomla 2.5 if you're using the new controller?
You can combine task and controller so that it'll call the task of the specified controller.These will be .(dot) seperated. try this-
index.php?index.php?option=com_mycomponent&view=viewname&task=specificcontroller.randomtask
Read More - http://docs.joomla.org/JController_and_its_subclass_usage_overview

Resources