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

singr263546
singr263546

I can't quite figure out the recommended approach to extending a component. Take for example the Session component of the RainLab.User plugin. Assume I want to add two functions onCASLogin and onCASLogout.
I create a plugin Acme.User with a component MySession. Normally, MySession class would extend ComponentBase if I wanted a custom component. However, since I want to extend the existing component RainLab.User Session, I can have MySession extend RainLab\User\Components\Session and add the definition of the functions therein. Is this the recommended or appropriate approach ? Since ComponentBase is an extension of Extendable, should I be using the Extend function ? If so, how do I do this ? (I can't figure out from the 'Extending Plugin' docs and tutorial which simply show how to add new properties/attributes but not new functionality. ). Thanks

exec
exec

Have you managed to figure this out? I'm also thinking about extending a component and I do find it to be confusing when it comes to best practices regarding extending existing plugins.

singr263546
singr263546

I didn't get any answer to my post so I eventually went the route of extending the existing plugin component class (RainLab.User Session in my case). I didn't find any way to use the Extend function to add functionality.

juni7692
juni7692

I would like to know this to.

doichinov.kaloyan8178
gfactor
gfactor

This will be handy.

denis.rendler
denis.rendler

Hello, guys!

The only way I could find to override the RainLab.Blog\Post was to create a new plugin, add a new component and extend from the RainLab.Blog\Post. Then the only step left was to declare my component in the template. I'll do a quick tutorial later on about this.

Hope it helps.

gfactor
gfactor

@denis.rendler, I believe the focus here is really not on declaring components in a template or creating a new plugin that will extend another plugin. RainLab already has a tutorial on that, but it's focused on backend(model & controller) extension. The question here is component extension. Eg: making a post like gender $fillable & assigning rules to the post without having to touch the master plugin itself.

denis.rendler
denis.rendler

hello, @godswillkoko5441!

Thanks for the feedback, but I will have to disagree a bit :) Either I misunderstood the question or I wasn't clear enough in my description, but I don't think there is a better way of extending the code of a third-party plug-in without creating your own plug-in and component first. For example, in my case, I needed to add the URL of the post to the Post entity. The way I've done it was to create a new plugin and component which extends the RainLab.Blog\Post component and simply overrides the loadPost method and adding the necessary code to add the URL of the post the Post entity. Then I needed it to use my component in the template in order to use the overwritten method.

I hope this clarifies the issue.

gfactor
gfactor

I think we are on the same page now, your initial comment was not quit clear. How soon can you paste the example code 'cos there are lots of people that are interested in component extension.

denis.rendler
denis.rendler

Hello, @gfactor!

You can find the plugin code here: https://github.com/rendler-denis/blogextension

After you add the plugin to your install simply change the component definition to this:

[KoderHut\BlogExtension\Components\Post blogPost]
slug = "{{ :slug }}"
categoryPage = "404"
postPage = "blog/post"

I hope it helps.

AlexeiKo
AlexeiKo

extending component is the same as extending plugin - for example: use Jiri\JKShop\Components\Basket; class Shopextend extends Basket

(Basket is existing downloaded component) And later add your component to the layout.

but I found some limitations. may be i'm doing something wrong. for example - if you do $var = new Basket working good, but I cannot access basic component itself without creating new object of the class. still working on this.

Parsec
Parsec

Ok, here's what I did, which is working for me so far:

I extended the RainLab User plugin through the plugin extension. In that extension, I created my own account component with a different name than that of the User Plugin. That component does a default php way of extending another class, so I did what AlexeiKo already mentioned:

use Rainlab\User\Compontents\Account;

class AccountExt extends Account
{
    public function onAddYourOwnFunctionalityHere()
        {
            // Describe the functionality here
        }
}

Make sure to call your methods like onDoSomething or onDoSomethingElse. If you do not use the 'on' in the beginning of the name, they are not recognised as Ajax handlers. In my experience you can only have Ajax handlers inside your component class. If you want to use methods named without the 'on' prefix, you can still use them, but you need to instantiate the component class like any other php class to use it's methods (although your Ajax handlers should still be able to use methods within the component itself, like any other class)

Ilesyt
Ilesyt

neoes.8um24055 said:

Have you managed to figure this out? I'm also thinking about extending a component and I do find it to be confusing when it comes to best practices regarding extending existing plugins.

Hope this helps:

https://octobercms.com/forum/post/extending-user-plugin-with-a-function

tony8938
tony8938

You can extend a component in a similar way to extending a model or controller. E.g. To extend the RainLab Account component from my plugin, I have the following in my Plugin.php file.

use RainLab\User\Components\Account;
public function boot()
{
    Account::extend(function ($component){
        $component->addDynamicMethod('onMyNewAjaxCall', function() use ($component) {
            //do something
            $myRenderedPartial = $component->renderPartial('my_partial', ['foo' => 'bar']);
            return ['#myContainer' => $myRenderedPartial];
        });
    });
}

Last updated

tee
tee

denis.rendler said:

Hello, @gfactor!

You can find the plugin code here: https://github.com/rendler-denis/blogextension

After you add the plugin to your install simply change the component definition to this:

[KoderHut\BlogExtension\Components\Post blogPost] slug = "{{ :slug }}" categoryPage = "404" postPage = "blog/post"

I hope it helps.

This work great for me and I think for this post as well.

Last updated

1-16 of 16

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