How to use this plugin to override the frontend com_content category view, blog layout #1

Open
LiliwoL opened this Issue May 15, 2012 · 14 comments

Comments

Projects
None yet
3 participants

LiliwoL commented May 15, 2012

Hi,

and thanks for that plugin, i tried to write my own to override view.html.php, and i didn't succeed.

So i installed yours, but it is limited to administration side.

Could you tell how to use it for frontend side and to another view (category) ?

I changed layout.php as:
function onAfterInitialise(){
var_dump(JFactory::getApplication()->input->get('option', '', 'cmd'));

    /** Set criteria for the View you want to override */
    if (JFactory::getApplication()->input->get('option', '', 'cmd') == 'com_content'
        && JFactory::getApplication()->input->get('view', '', 'cmd') == 'category'
        && JFactory::getApplication()->input->get('layout', '', 'cmd') == 'blog'
    ) {

        /** Override JRequest View and Layout - register the view (example in the layout/com_content folder) */
        JRequest::setVar('view', 'article2');
        JRequest::setVar('layout', 'blog2');
        JLoader::register('ContentViewArticle2', __DIR__ . '/com_content/views/article/ContentViewArticle2.php');
    }

}
}

But the var_dump never displays anything...

Owner

AmyStephen commented May 15, 2012

On quick look (at work right now), the following is not an array or object, so you should be able to just echo the contents.

Meaning, change this:
var_dump(JFactory::getApplication()->input->get('option', '', 'cmd'));

To this:
echo JFactory::getApplication()->input->get('option', '', 'cmd');

LiliwoL commented May 15, 2012

Well, neither ver_dump, neither echo is executed.

My question is most about onafterInitalise callback. Is it the right to use even in front end?

Forgive my poor english...
Thanks for your quick answer

Owner

AmyStephen commented May 15, 2012

Looked at the code, again.

The line that you need to change is
https://github.com/AmyStephen/Layout-Override-Plugin/blob/master/plugins/system/layout/layout.php#L20

Change "administrator" to "site" to use this approach on the frontend.

LiliwoL commented May 16, 2012

Thanks that's it.
changing administrator to site make the plugin works in frontend.

Now i've got a 500 error because display is not found.
Is ther another way to organize the views folder in fronted side of com_content?

Do you have an idea?

Owner

AmyStephen commented May 16, 2012

Yea, don't be changing the folder structure. That would be no fun, at all. =)

Not sure where "display" is coming in -- can you explain what you are trying to do? That will help me better advise you.

LiliwoL commented May 16, 2012

Ok,
My aim is to override the view.html.php of the category com_content in order to be able to display the articles whithin subcategories.
As i don't want to hack joomla core files, i found your plugin solution interresting.

So, i installed your plugin and i changed code like that:

// Overide Frontend View
if (JFactory::getApplication()->getName() == 'site'
&& JFactory::getApplication()->input->get('option', '', 'cmd') == 'com_content'
&& JFactory::getApplication()->input->get('view', '', 'cmd') == 'category'
&& JFactory::getApplication()->input->get('layout', '', 'cmd') == 'blog'
) {

        /** Override JRequest View and Layout - register the view (example in the layout/com_content folder) */
        JRequest::setVar('view', 'category2');
        JRequest::setVar('layout', 'blog2');
        JLoader::register('ContentViewCategory2', __DIR__ . '/com_content/views/category/ContentViewCategory2.php');
    }

And the folder structure:
layout
-- com_content
-- -- views
-- -- -- article (your folder unchanged)
-- -- -- category
-- -- -- -- tmpl
-- -- -- -- -- blog2.php
-- -- -- -- -- index.html
-- -- -- -- ContentViewCategory2.php
-- -- -- -- index.html

And i've got this error while calling this page:
/index.php?option=com_content&view=category&layout=blog&id=30&Itemid=289
Error 500 - (Not found display) Affichage introuvable [name, type, prefix] : category2, html, contentView

In ContentViewCategory2.php file:

/** Override this View */
JLoader::register('ContentViewCategory', JPATH_BASE . '/components/com_content/views/category/view.html.php');

/** Override this Model */
JLoader::register('ContentModelCategory', JPATH_BASE . '/components/com_content/models/category.php');

/**
* Extend the real View
*/
class ContentViewCategory2 extends ContentViewCategory
{
/**
* Display the view
*/
public function display($tpl = null)
{
    /** Put your view into this folder - or change the path */
    $this->addTemplatePath(__DIR__ . '/tmpl/');
    parent::display($tpl);
}
}
/**
* Extend the real model
*/
class ContentModelCategory2 extends ContentModelCategory {}
Owner

AmyStephen commented May 16, 2012

Wait. Sorry. Let me rethink this.

LiliwoL commented May 16, 2012

Ok, so if i name my class as the class which iam overriding, there is no extends then!
i can't write it:
class ContentViewCategory extends ContentViewCategory{

And my JLoader seems to call the file whithin the plugin folder
JLoader::register('ContentViewCategory', DIR . '/com_content/views/category/ContentViewCategory.php');

But you're right, if i set
JRequest::setVar('view', 'category');
It works, but no overriding...

Owner

AmyStephen commented May 16, 2012

Yea, I answered too quickly - removed my comment but not before you read it, apparently. Put those class names back to 2 the way you had them.

You are right -- this is probably your problem:
JLoader::register('ContentViewCategory', DIR . '/com_content/views/category/ContentViewCategory.php');

Maybe try this:
JLoader::register('ContentViewCategory2', JPATH_COMPONENT_SITE . '/views/category/ContentViewCategory2.php');

LiliwoL commented May 17, 2012

Thanks Amy, but i think that the folder "category" whithin plugin views folder isn't read at all.

Neither its ContentViewCategory.php file.
So once again:
plugins
-- system
-- -- layout
-- -- -- views
-- -- -- -- category
-- -- -- -- -- ContentViewCategory2.php
-- -- -- -- -- tmpl
-- -- -- -- -- -- blog.php

In the layout.php file:

// Overide Frontend View
if (JFactory::getApplication()->getName() == 'site'
&& JFactory::getApplication()->input->get('option', '', 'cmd') == 'com_content'
&& JFactory::getApplication()->input->get('view', '', 'cmd') == 'category'
&& JFactory::getApplication()->input->get('layout', '', 'cmd') == 'blog'
) {

    /** Override JRequest View and Layout - register the view (example in the layout/com_content folder) */
    // JRequest::setVar('view', 'category2'); // Returns a 500 error even if the view folder is named category2
            JRequest::setVar('view', 'category');  // Loads normal category view
    JRequest::setVar('layout', 'blog');
    JLoader::register('ContentViewCategory', JPATH_COMPONENT_SITE . '/views/category2/ContentViewCategory2.php');
    }

I'm a bit lost now.
This plugin is built to shortcut the call of Category Class & Model.
Apparently, it doesn't!

Could you explain what JLoader: :register stands for?

Thanks for your help. I noticed that you build a fork of Joomla which seems interresting, when i'll have time, i'll test it.
My aim is to make what i want from Joomla Core.
I'd like to build a plugin which allows simply to hack the core functions, and allows to overide any view.html.php (through parameters of the layout plugin...)

LiliwoL commented May 17, 2012

It's me again.
I looked at the JLoader::register documentation.
And i tried to require_once better than to use JLoader::register.
JLoader::register('ContentViewCategory', DIR . '/com_content/views/category/ContentViewArticle2.php');
changed into
require_once(dirname(FILE) . DS . 'com_content' . DS . 'views' . DS . 'category' . DS . 'view.html.php');

Then, the view.html.php is really called.

But if in this view.html.php i use (as you do in ContentViewArticle2.php):
JLoader::register ( the joomla core category class)
then it loads the core class and don't care the following of overriding i'm writing...

To conclude, i think that JLoader::register loads and executes the class, then it's impossible to overide by this way.
It seems we've got to Jloader::register the overriden class before the core one!

ienev commented Oct 16, 2012

Hi Amy and Liliwol,

by using Amy's plugin I've succeeded to override the com_content archive front end view. Basically what I wanted to achieve was a blog-like monthly archive for articles from a specific category (combined with a module to display months). This was not possible with the stock com_content as the archive model selects only archived articles (state=2). I've overridden the view and model and changed the ContentModelArchive model to select published articles (state=1) and within a given category (category_id=x). It is a really small modification to Amy's plugin but I thought you would like to take a look, especially Liliwol, who had issue with modifying the plugin.

I will shortly fork the project and apply my changes.
And thanks for the plugin Amy!

Cheers
Iskar

Owner

AmyStephen commented Oct 16, 2012

Please leave a link to your fork so that folks know where to look. I'm not actively developing in Joomla at this time.

Thanks much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment