Guest
18 Jun 2014 02:17

I want to run some php code to pre-load some fields before allowing the user to complete the article form. I can do it by running custom php code that first asks the user to enter some information, then stores that info in a 'holding' sql file, and finally links to a new record screen (by copying the link on the article list screen). Then, I've modified the text templates to pre-load their values from this 'holding' sql file.

The problem is that the article list also has a link that allows the user to enter a new article which then bypasses my pre-load code.

How can I either... * Remove the ability for a user to enter a new article from the article list button ("+ Post

here") * Locate a place within Cobalt code to place my custom code. I've tried placing the code in the Article List Template, for example, and while I can load up the 'holding' file, the text value have already been pre-loaded from the previous new article.

Last Modified: 11 Aug 2014


Sergey
Total posts: 13,748
18 Jul 2014 03:07

You can add JED review if you appriciate.


billvv
Total posts: 55
19 Jul 2014 02:55

Hi Sergey, I've tried a few variations on the code, and what I've found is: This displays the VIN:

<?php
defined('_JEXEC') or die('Restricted access');

require_once JPATH_ROOT . '/components/com_cobalt/api.php';

class plgMintVinpreload extends JPlugin
{
        public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
            if($isnew)
            {

$vin = $record['fields'][36];
echo $vin;
exit;

// end if isnew
            }
//end public function
        }
//end class
}

This does not (i.e, the section code returns even tho the section id is 6.)

<?php
defined('_JEXEC') or die('Restricted access');

require_once JPATH_ROOT . '/components/com_cobalt/api.php';

class plgMintVinpreload extends JPlugin
{
        public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
            if($isnew)
             {

if($app->isAdmin() || $app->input->getInt('section_id') != 6)
    {
        return;
    }

$vin = $record['fields'][36];
echo $vin;
exit;

// end if isnew
            }

//end public function
    }

//end class
}

And this does not load the strings into the saved form.

<?php
defined('_JEXEC') or die('Restricted access');

require_once JPATH_ROOT . '/components/com_cobalt/api.php';

class plgMintVinpreload extends JPlugin
{
        public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
        if($isnew)
              {
                $_POST['jform']['fields'][41] = "this is 41";
                $_POST['jform']['fields'][42] = "this is 42";
                $_POST['jform']['fields'][39] = "this is 39";
// end if isnew
              }

//end public function
        }

//end class
}

Sergey
Total posts: 13,748
21 Jul 2014 01:13

if($app->isAdmin() || $app->input->getInt('section_id') != 6) - what is $app?

You should define $app = JFactory::getApplication();

billvv And this does not load the strings into the saved form.

Fixed. I will upload update today.


billvv
Total posts: 55
21 Jul 2014 17:42

Any way to download and replace just the file(s) affected?


Sergey
Total posts: 13,748
22 Jul 2014 11:11

Why? Did you hack already something?


billvv
Total posts: 55
22 Jul 2014 14:52

Not sure what you mean, Sergey. I haven't made any changes to the Cobalt files.

If I understand correctly, you have uploaded some changes to the Cobalt files to allow the field save statement to work properly. We are running this on a Beta site and when we update the Cobalt component we need to backup the site first. I figure if we just update the files that you changed, we could save some time. But that's OK, I'll ask Derrick to backup the site and update, and then I'll continue my testing. As Derrick is not available for a couple days and I will be out of town for a week, I don't think I'll be able to try it again for a week or so.


Sergey
Total posts: 13,748
23 Jul 2014 01:45

Just install Akeba back up and you wiill be able to backup site yourself in minuts.


billvv
Total posts: 55
30 Jul 2014 00:38

OK, I'm getting close.....

Updated and fields are being saved! Now, I would like to hide the fields in the form since the module is calculating the value. Is that possible>


Sergey
Total posts: 13,748
30 Jul 2014 00:54

In the fields parameters Who can submit and Edit set special or even no one.


billvv
Total posts: 55
30 Jul 2014 03:18

Tried that. Set the Year and Model field to 'no one' but it still appears.

Capture2.JPG

And the value is not saved....

(Title is composite from calculated fields. Year and Model field is [41].

capture1.JPG


Sergey
Total posts: 13,748
30 Jul 2014 09:22

Rigth. Because Cobalt does not process fields that are not allowed.

May be as a solution to create custom form template and there just hide those fields?


pepperstreet VIP
Total posts: 3,837
30 Jul 2014 14:43

billvv I would like to hide the fields in the form since the module is calculating the value

What about using CSS display property?

Bootstrap CSS and/or Responsive utility classes

In field's backend parameters -> Form -> General Properties -> Field Class
Enter a pre-defined CSS class i.e.:

Custom & Cobalt CSS

Fields have a certain default CSS id and class:
e.g. for field with ID99 it is ID fld-99 , CLASS field-99

or you might give a custom class in field's backend parameters. e.g. MyCustomClass

#fld-99, .field-99, .MyCustomClass {
display: none; 
}

billvv
Total posts: 55
07 Aug 2014 23:10

Update: The plugin that I created now appears to be breaking my website. See the long troubleshooting saga here.

This code (note the commented out part) causes a white page with no errors to appear when calling Cobalt from the backend and frontend, and any Article from the frontend. If I comment out the class statement the pages appear normally.

<?php
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ROOT . '/components/com_cobalt/api.php';
class plgMintVinpreload extends JPlugin
{
 /*   public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
    if($isnew)
        {
     $app = JFactory::getApplication();
    if($app- >isAdmin() || $app- >input- >getInt('section_id') != 6)
    {
        return;
    }

    $vin = $record['fields'][36];

    $model = "Model";
    $production_number = "Production Number";
    $market = "Market"
      $_POST['jform']['fields'][41] = $model;
        $_POST['jform']['fields'][42] = $production_number;
        $_POST['jform']['fields'][39] = $market;

        //$_POST['jform']['fields'][3] = $year_and_model;
        //$_POST['jform']['fields'][2] = $production_number;
        //$_POST['jform']['fields'][15] = $market;

// end if isnew
//    }
//end public function
//    }
//end class
}
?>

Sergey
Total posts: 13,748
08 Aug 2014 01:01

Try this code. Copy it and paste and delete every line you have now.

<?php
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ROOT . '/components/com_cobalt/api.php';
class plgMintVinpreload extends JPlugin
{
    public function onBeforeArticleSaved($isnew, $record, $section, $type)
    {
        if($isnew)
        {
             $app = JFactory::getApplication();
            if($app- >isAdmin() || $app- >input- >getInt('section_id') != 6)
            {
                return;
            }

            $vin = $record['fields'][36];

            $model = "Model";
            $production_number = "Production Number";
            $market = "Market"
              $_POST['jform']['fields'][41] = $model;
            $_POST['jform']['fields'][42] = $production_number;
            $_POST['jform']['fields'][39] = $market;
        }
    }
}

billvv
Total posts: 55
08 Aug 2014 03:45

Substituted code and enabled plugin. Menu request for Article returned a blank page with substituted code. No errors reported.

This code, where the section test is commented out, allows selecting an Article in the frontend.

<?php
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ROOT . '/components/com_cobalt/api.php';
class plgMintVinpreload extends JPlugin
{
    public function onBeforeArticleSaved($isnew, $record, $section, $type)
    {
        if($isnew)
        {
            $app = JFactory::getApplication();
/*            if($app- >isAdmin() || $app- >input- >getInt('section_id') != 6)
            {
                return;
            }
*/
           $vin = $record['fields'][36];

            $model = "Model";
            $production_number = "Production Number";
            $market = "Market";

              $_POST['jform']['fields'][41] = $model;
            $_POST['jform']['fields'][42] = $production_number;
            $_POST['jform']['fields'][39] = $market;

        }
    }
}

And this code just displays a blank page.

<?php
defined('_JEXEC') or die('Restricted access');
require_once JPATH_ROOT . '/components/com_cobalt/api.php';
class plgMintVinpreload extends JPlugin
{
    public function onBeforeArticleSaved($isnew, $record, $section, $type)
    {
        if($isnew)
        {
            $app = JFactory::getApplication();
            if($app- >isAdmin() || $app- >input- >getInt('section_id') != 6)
            {
                return;
            }

           $vin = $record['fields'][36];

            $model = "Model";
            $production_number = "Production Number";
            $market = "Market";

              $_POST['jform']['fields'][41] = $model;
            $_POST['jform']['fields'][42] = $production_number;
            $_POST['jform']['fields'][39] = $market;

        }
    }
}

Sergey
Total posts: 13,748
08 Aug 2014 08:41

Comment the code that produce blank page line by line and see what line cause it.


billvv
Total posts: 55
08 Aug 2014 16:47

If I comment out this line, the error (white page) does not appear.

    if($app- >isAdmin() || $app- >input- >getInt('section_id') != 6)

And after many, many attempts to make errors appear, I went after the offending code. When I removed the spaces out of the "- >" code in that line I now do not get the white page. The Registry and Articles work as normal.

    if($app->isAdmin() || $app->input->getInt('section_id') != 6)

I believe the spaces were added at some point in my editing; I'm blaming it on the goofy code editor in cPanel that shows ghost characters after awhile.

So I believe the problem is solved. I suspect my site still has an error reporting issue that I'll have to track down.

Again, thank you for your patience and help.


Sergey
Total posts: 13,748
11 Aug 2014 00:26

This is good to hear. I so this empty space but I thout it is added buy our code syntax highlighter somehow.

Powered by Cobalt