1

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 "";
    }
  • Please show you code that you currently have. Surely you can simply use an if > else statement in this sort of scenario – Lodder Jun 8 '14 at 17:20
  • What do you want to happen exactly, do you want to show a "Not Authorised" message or do you want to send them somewhere else? – Craig Jun 8 '14 at 22:13
  • I should have been more clear. I want to remove them from the "Category List" or "Blog" view. i.e. Not show them at all. I am arriving at the conclusion that a content plugin may not be capable of doing this alone. In fact setting $article=null; does not stop it from being displayed in the list. Maybe a plugin that adds a parameter to the article for example "do_not_show" and front-end hack at the point where the "Category List" table is generated we could check that if "do_not_show" flag is set and not display it. I'm digging through the Joomla code to find that point but I'm very new to J! – jalbasri Jun 8 '14 at 22:29
  • 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 Nov 18 '17 at 23:37
4

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.

1

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.

  • If my answer is solving your question, then please accept it :) Thanks! – Dmitry Rekun Jun 9 '14 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.