layout override by plugin

376 views
Skip to first unread message

Ofer Cohen

unread,
Mar 21, 2012, 7:28:10 AM3/21/12
to joomla-dev-general

Hey All

Is there any way that I can override layout of view from plugin?

Thanks

-- 
Ofer Cohen
TP2 - The new way to manage your modules

Hannes Papenberg

unread,
Mar 21, 2012, 7:38:45 AM3/21/12
to joomla-de...@googlegroups.com
I tried to get that in with this pull request, but it was denied:
https://github.com/joomla/joomla-platform/pull/421

Hannes

Am 21.03.2012 12:28, schrieb Ofer Cohen:
>
> Hey All
>
> Is there any way that I can override layout of view from plugin?
>
> Thanks
>
> --
> Ofer Cohen

> TP2 <http://extensions.joomla.org/extensions/style-a-design/modules-management/19884> - The new way to manage your modules
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.
> To post to this group, send an email to
> joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to
> joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.

Michael Babker

unread,
Mar 21, 2012, 8:18:22 AM3/21/12
to joomla-de...@googlegroups.com
As Hannes mentioned, it's not possible using the API.  In my own plugin, I've added a method to check for overrides following the same logic as component views based on some code in a JoomlaWorks plugin I use.  You can see it at https://github.com/mbabker/Yet-Another-Social-Plugin/blob/master/yetanothersocial.php#L308.

Ofer Cohen

unread,
Mar 21, 2012, 8:57:29 AM3/21/12
to joomla-de...@googlegroups.com
On 03/21/2012 01:38 PM, Hannes Papenberg wrote:
I tried to get that in with this pull request, but it was denied:
https://github.com/joomla/joomla-platform/pull/421

Hannes


I've added comment in the pull request.



On 03/21/2012 02:18 PM, Michael Babker wrote:
As Hannes mentioned, it's not possible using the API.  In my own plugin, I've added a method to check for overrides following the same logic as component views based on some code in a JoomlaWorks plugin I use.  You can see it at https://github.com/mbabker/Yet-Another-Social-Plugin/blob/master/yetanothersocial.php#L308.
I'm not overriding content component, but admin component.

Amy Stephen

unread,
Mar 21, 2012, 10:57:07 AM3/21/12
to joomla-de...@googlegroups.com
Ofer -

In a system plugin, you can pretty much override any core view -- or, better yet, extend it. Then, you can override the layout as you wish.

Hannes -

To be fair, I didn't see the benefit of your approach, either. If a developer has to create a plugin to change the layout (as was the case with your solution) -- then, really, that's doable today by extending or overriding the view at runtime. I honestly don't believe anyone is singling out your code to reject it but rather that there is broad acceptance now that the entire MVC needs rethinking.

There are many ways to override, extend the MVC today - it's not that big of a deal although this approach limits who can do it (you must have development skill and awareness of the J! API). That is not going to help the frontend dev who most likely isn't a strong PHP coder. Moving towards a parameter driven layout and shared layouts (or REAL views) in the future will be much better.

Hannes Papenberg

unread,
Mar 21, 2012, 3:04:23 PM3/21/12
to joomla-de...@googlegroups.com
Amy,
Ofer did not ask for a way to override a core view, but a simple and
reliable way to change the chosen layout. There are several reasons why
I wouldn't want to override a complete view, starting with the need to
update my overriden view when the original view is updated. What are you
going to do if you have different extensions that want to override the
same view?

Regardless of that, the code that I proposed allows for A LOT more than
just "overriding" a view. And even though I agree with the notion that
the MVC most likely needs to be revamped, I first have to see the code
before I stop bugging everyone with these small improvements. The
changes to JView were ready in time for Joomla 1.7 and thus could
already be used by lots of people for at least 2 major releases. There
isn't a lot more than good intentions regarding a new MVC as far as I
know. And even if there were, that still leaves a few thousand legacy
extensions out there using the old code. So as long as we are talking
about varporware, please don't dismiss my code as "obsolete soon anyway".

Hannes

> TP2 <http://extensions.joomla.org/extensions/style-a-design/modules-management/19884> - The new way to manage your modules


>
> --
> You received this message because you are subscribed to the Google
> Groups "Joomla! General Development" group.

> To view this discussion on the web, visit
> https://groups.google.com/d/msg/joomla-dev-general/-/_lf6hgcXaKoJ.

Ofer Cohen

unread,
Mar 21, 2012, 5:46:25 PM3/21/12
to joomla-de...@googlegroups.com

Hey Amy

Thanks for your reply. It took half a day to find how to override view (layout can't be override) and here is how I did it:
    public function onAfterRoute() {
        $app = JFactory::getApplication();
        if (!$app->isAdmin() || JRequest::getString('option') != 'com_banners'
            || JRequest::getString('view') != 'banner') {//check if we're on the right view
            return;
        }
        jimport('joomla.application.component.controller');
        $params = array(
            'base_path' => JPATH_BASE . '/components/' . JRequest::getString('option'),
            'view_path' => dirname(__FILE__),
        );
        $b = JController::getInstance('Banners', $params);
        $b->addViewPath(dirname(__FILE__) . '/views');
    }

This will load the plugin view before the component view. Because it's loading first, it will use the first that load. The loading done via getInstance.

I copy the view banner from admin com_banners to the plugin views folder and then override it.

Hope it will help someone one day. I'll put it in the docs soon. What is the best place in the wiki to put it?

Thanks

Ofer Cohen

--

Amy Stephen

unread,
Mar 21, 2012, 6:59:17 PM3/21/12
to joomla-de...@googlegroups.com
Interesting approach, Ofer! I'm going to look more closely at that.

After work, I threw a little sample plugin out that accomplishes the same thing, but differently. (And with more code - your solution is very elegant.)

https://github.com/AmyStephen/Layout-Override-Plugin

My example fires a System Plugin listening to the onAfterInitialise event.

https://github.com/AmyStephen/Layout-Override-Plugin/blob/master/plugins/system/layout/layout.php

-- sets criteria to identify which view you want to override;
-- it changes the JRequest values for View and Layout;
-- registers the new View.

All the new View does is:
- extend the normal view (and model) (So, Hannes, core updates are not an issue);
- The view loads a path for the layout.

https://github.com/AmyStephen/Layout-Override-Plugin/blob/master/plugins/system/layout/com_content/views/article/ContentViewArticle2.php

So - Hannes - my point is - since developers have to create a system plugin and write most of that code to use your solution, anyway, why complicate the View logic? It's not like everyone will automatically know what to do -- it's a new method that isn't like anything else we do anywhere else -- and learning the option is even available is tricky.

This option is the normal way developers override core (or extension) classes regardless of what they are trying to override. Ofer now has this skill - and he's going to pass it on, just like the others of us did when we first learned it.

You may disagree. That's fine.  But that's also the point. We can't all have Joomla core the way we personally want it -- but if we can learn how to override and extend it -- then, we are not stopped from doing our work.

I did not want Ofer to think that he couldn't do his job unless the project took your patch -- you should share the solution people can use today -- not just your rejected idea. That way people understand the real picture. Obviously he and I both demonstrated two ways of getting the job done.

Do I think this solution is ideal? No. This should be parameter driven. No plugins. Part of the package. But your patch doesn't do that either, so, I'm just not a fan, nothing personal.

Ofer Cohen

unread,
Mar 22, 2012, 10:47:58 AM3/22/12
to joomla-de...@googlegroups.com

Hey Amy

Thanks for the example. I added it to the wiki page http://docs.joomla.org/How_to_override_the_output_from_the_Joomla!_core#Further_tips (as a link).

The difference between my approach to yours is that mine override the view and not extends it like you did (but perhaps I can get over that too. I didn't try yet). In addition I didn't need to "touch" the model.

Amy, you make my day :-)

Ofer Cohen
Reply all
Reply to author
Forward
0 new messages