Only show articles by current user in category view using plugin

by jalbasri   Last Updated November 19, 2017 00:10 AM

Hi I'm trying to write a content plugin that will only show articles written by the currently logged in user in the category view.

Using the onContentPrepare and onContentBeforeDisplay triggers I can Identify the currently logged in user and whether the article belongs to them quite simply.

The next step Not displaying the article is much trickier and I have not been able to figure it out.

So, is there a way to easily NOT show an article using these plugin triggers?

Sure, here's the code:

function onContentBeforeDisplay($context, &$article, &$params, $limit=0)
     {

            $name = JFactory::getUser()->name;
            $author = $article->author;
            if ($author != $name) {
                /*  ?   */
            }

            // var_dump($article);
            // var_dump($params);
            // var_dump($context);
            return "";
    }


Answers 3


You do not need a plugin for that. Just override a category view and put your check there.

For example for the Blog view override blog.php and around line 60 make your check:

if ($item->created_by == JFactory::getUser()->id) :
    echo $this->loadTemplate('item');
}
endif;

That is of course just an example, but it shows the power of overriding. Hope this will help.

Dmitry Rekun
Dmitry Rekun
June 09, 2014 10:05 AM

The plugin alone cannot solve to problem but it can be used to add a field with the currently logged in user to the article parameters and in the view where the category list is generated a simple if statement is required to prevent adding articles by other users.

The plugin code:

function onContentBeforeDisplay($context, &$article, &$params, $limit=0)
     {
            $name = JFactory::getUser()->name;
            $article->logged_in = $name;
            // var_dump($params);
            return "";
     }

I tried $params->set('logged_in', $name); but it wouldn't work in the view so I had to add it to the $article parameter.

The view to edit is joomla/components/com_content/views/category/tmpl/default_article.php

underneath this line:

<?php foreach ($this->items as $i => $article) : ?>

add

<?php if ($this->items[$i]->author == $this->items[$i]->logged_in) : ?>

close the if statement at the bottom and that's it now the table will only show articles authored by the current user.

Edit:

Thanks to Dimitri's answer I now see you should be able to access the user in the view files directly without the plugin at all and you should use an override instead of directly modify the view files.

jalbasri
jalbasri
June 09, 2014 11:44 AM

hello I will need your help on this one. I will need that the article on a category list is show for author of article only and not by other author or user. But can't follow you since am not a developer. Van you tell me what to replace in blog.php ?

Thank

Lowell Sambadoo
Lowell Sambadoo
November 18, 2017 23:37 PM

Related Questions


controlling content viewing by user type and region

Updated November 10, 2017 16:10 PM

How can include the component view in plugins

Updated July 06, 2015 13:04 PM