1

I have written a simple plugin to include custom content against Joomla articles. Here's the code:

defined ( '_JEXEC' ) or die ( 'Restricted access' );

class plgContentHomegrid extends JPlugin {

    protected $autoloadLanguage = true;

    function onContentPrepareForm($form, $data) {

        $app = JFactory::getApplication();
        $option = $app->input->get('option');

        switch($option) {

            case 'com_content':
                if ($app->isAdmin()) {

                    JForm::addFormPath(__DIR__ . '/forms');

                        $form->loadFile('homegrid', false);

                }

                return true;

        }

        return true;

    }

}

This works fine, the additional fields are displayed as a separate tab in admin, data saves correctly and everything's perfect. So far so good...however, I only want to display the additional tab on the home page. So, I amended the code so the additional form is only displayed for the home page:

 if( $data->featured == 1 ){

    $form->loadFile('homegrid', false);

} 

The form now only displays on the home page admin screen as needed BUT the data is not saved.

This is partially explained by this issue, but I'm not clear how I can amend the code so that the form is conditionally displayed AND saved.

1

EDITED: (Seems this thread is a DIY one...) Got it....

$input = JFactory::getApplication()->input;
$formData = new JInput($input->get('jform', '', 'array'));

if( $data->featured == 1 || $formData->getInt('featured', 0) == 1){

   $form->loadFile('homegrid', false);

} 

Does the trick.

  • I would use Joomla's JInput, not $_POST – Lodder Mar 29 '15 at 14:44
  • Thanks for your response. How exactly do I access the data using JInput? Tried multiple approaches but none worked. – sb65 Mar 31 '15 at 9:34
  • I would suggest asking a new question, and show some code, so it will be easier to assist you – Lodder Mar 31 '15 at 9:47
  • Well, the code is above. The question relates to the original problem and it seems to me it would not be sensible to split the answer into two. I don't see how to access the Jinput object to access the posted data.How do I access the 'featured' posted data from JInput? – sb65 Apr 1 '15 at 9:57
  • As mentioned before, this is a separate topic, so please ask a new question ;) – Lodder Apr 1 '15 at 10:11
1

You already answered your question by yourself. This is just an answer to show you how to get the data with JInput:

$input = JFactory::getApplication()->input;

if ($data->featured == 1 || $input->getInt('featured') == 1)
{
   $form->loadFile('homegrid', false);
} 

or

$input = JFactory::getApplication()->input;

if ($data->featured == 1 || $input->getInt('Itemid') == 100)
{
   $form->loadFile('homegrid', false);
}

(where "100" is your menu item ID of the home menu item)

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.