Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@LiliwoL
Copy link

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...

@AmyStephen
Copy link
Owner

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
Copy link
Author

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

@AmyStephen
Copy link
Owner

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
Copy link
Author

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?

@AmyStephen
Copy link
Owner

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
Copy link
Author

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 {}

@AmyStephen
Copy link
Owner

Wait. Sorry. Let me rethink this.

@LiliwoL
Copy link
Author

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...

@AmyStephen
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

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
Copy link

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

@AmyStephen
Copy link
Owner

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!

@ienev
Copy link

ienev commented Oct 17, 2012

Here is my fork https://github.com/ienev/Layout-Override-Plugin

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants