This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

planetadeleste
planetadeleste

I have the Users Model extended and I need to add an ajaxHandler method to Users Controller.

Plugin.php

public function boot() {
    UserModel::extend(function(UserModel $model){
        $model->hasOne['paypal'] = ['\PlanetaDelEste\PayPal\Models\User'];
    });

    UserModel::deleting(function($user) {
        $user->paypal->delete();
    });

    UserController::extendFormFields(function($form, $model, $context){

        if(!$model instanceof UserModel)
            return;

        if(!$model->exists)
            return;

        PaypalUser::getFromUser($model);

        $form->addTabFields([
            'paypal[transactions]' => [
                'tab' => 'planetadeleste.paypal::lang.tab.transactions',
                'type' => 'partial',
                'path' => '$/planetadeleste/paypal/models/user/_transaction_list.htm'
            ]
            // more fields...
        ]);
    });
}

_transaction_list.htm

<h2>
    <i class="fa fa-cc-paypal"></i> <?= e(trans('planetadeleste.paypal::lang.tab.transactions')) ?>
    <a href="nojavascript...//" class="btn btn-default pull-right" data-request="onUpdateList"><?= e(trans('planetadeleste.paypal::lang.form.update_list')) ?></a>
</h2>

I don't know how to add the update_onUpdateList method to $/rainlab/user/controllers/Users.php Controller. Any help?

thanks

Last updated

Scott
Scott

I've never tried this with a controller before, but this might do what you need...

public function boot()
{
    UsersController::extend(function($controller) {
        $controller->addDynamicMethod('update_onUpdateList', function() {
            // do stuff
        });
    });
}

Last updated

planetadeleste
planetadeleste

Amazing @Scott !!!! works great!!!

thanks, you save me of many development hours...

maxDubovsky
maxDubovsky

Thanks, saved my time too

andrepo18380
andrepo18380

Awesome @Scott

Just found out this works for the model as well: User::extend(function($model) { $model->addDynamicMethod('hasReceivedRecommendation', function() use($model) { return false; }); });

MarvinDurot
MarvinDurot

You can also create a custom behavior for your ajax handlers and implement it in the extended controller.

  • Create the custom behavior.
class MyCustomBehavior extends ControllerBehavior
{
  public function onUpdateList() { ... }
}
  • Implement the custom behavior in the controller.
UsersController::extend(function ($controller) {
  $controller->implements[] =
   'Author\PluginName\Behaviors\YourCustomBehavior';
});

Last updated

alberich205218618
alberich205218618

@Scott HERO! and thx u @MarvinDurot for your contribution too, both of you has saved my life! :D

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.