Гость
18 Июнь 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.

Последние изменения: 11 Авг 2014


Sergey
Total posts: 13,748
02 Июль 2014 10:56

I see. This is too complicateed for me. But rather than javascript I do not see how you can populate those fields without hacking cobalt core.


billvv
Total posts: 55
02 Июль 2014 15:14

So it is not possible at the submittal form level to store a field value?

(because aside from running a bunch of code that doesn't seem to affect what Cobalt is doing there, that is what we're trying to do).


Sergey
Total posts: 13,748
02 Июль 2014 23:51

I added event. So you can create a mint group plugin for that. Please read more here.

http://docs.mintjoomla.com/en/cobalt/cobalt-events/

Use onBeforeArticleSaved event and push data into $_POST.

This trigger will be available with update later today.


billvv
Total posts: 55
03 Июль 2014 17:16

Well, thanks for creating that event for us, Sergey.

Is there a Joomla plugin that I can look at that uses cobalt-events? I'm pretty sure I can figure out how to make a Joomla plugin, and that I can create php code to do the calculations. I assume that we simply pass parameters to the function within the php code to save the results of the calculation. See comments below:

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

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

class plgMintPluginname extends JPlugin
{
// Is this where we put our php calc code?
// $isnew = false;
// $record[46] = $vin, etc to load record array?
// I assume I don't need to worry about $section and $type. 

    public function onBeforeArticleSaved($isnew, $record, $section, $type)
    {
    }
}

billvv
Total posts: 55
03 Июль 2014 17:37

I think i answered some of my own questions .......

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

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

class plgMintPluginname extends JPlugin
{
    public function onAfterArticleSaved($isnew, $record, $fields, $section, $type)

    {

    if($isnew)
        {
             // calc code here
            $record[46] = $vin;
        }
    }
} 

Sergey
Total posts: 13,748
04 Июль 2014 00:25

In example is onAfterArticleSaved but you need onBeforeArticleSaved! There is no fields parameter.

public function onAfterArticleSaved($isnew, $record, $section, $type)
{

    // How to het VIN
    $vin = $record['fields'][46];

    // See what is in 
    var_dump($record); exit;

    // Inject data into fields
    $_POST['jform']['fields'][12] = $model;
}

billvv
Total posts: 55
04 Июль 2014 02:19

So, how's this look? I assume that I need to make the calculated fields hidden. Also, since the composite Title is composed from the values calculated from the vin calculation, is there any way to make sure that the vin is never changed after it is entered the first time. I suppose I could do an 'else' (i.e. is NOT '$isnew) that corrects the vin by reverse calculation.

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

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

class plgMintPluginname extends JPlugin
{
    public function onBeforeArticleSaved($isnew, $record, $section, $type)
    {
         if($isnew)
         {
         //Get VIN from Ssubmittal form
         $vin = $record['fields'][46]
         //Calc other fields here
         // Model of car calculated as $model
         // Country of car calculated as $country
         // Inject data into fields 
        $_POST['jform']['fields'][12] = $model;
        $_POST['jform']['fields'][13] = $country;  
        }
    }
}

Sergey
Total posts: 13,748
07 Июль 2014 01:47

If VIN is changed fields also will change. But if you do not want this field to be changed after first submission, you should disalllow field editing in field parameters.


billvv
Total posts: 55
12 Июль 2014 20:05

Hi Sergey, I have created the Plugin and installed it successfully per Joomla, but I'm not seeing any evidence that it has run either before or after I saved it. (not surprising on my first try).

I used the code

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

to set up the calcs by acquiring the vin, presuming that I could get the vin# once it was entered in the form.

When I ran the 'Post ' routine and entered the VIN and saved the record. The other fields that were supposed to change did not change.

        $_POST['jform']['fields'][41] = $year_model;
        $_POST['jform']['fields'][39] = $market;
        $_POST['jform']['fields'][2] = $production_number_and_special;
        $_POST['jform']['fields'][39] = $market;  

Derrick has updated Cobalt just yesterday to version 8.565. I see that the event update was 8.555; does that one need to be installed first, or will just the later update be OK? I suspect it's OK.

Is there a way that I can verify that my plugin code is being executed? I tried an echo statement but no change. I suspect that it is not being executed. I do see the .php and xml files in 'plugins/mint'.

Can I assign my plugin to a specific Section in the backend, or does it need to be coded somehow?

In my second attempt, I used the html and php code used previously to acquire the vin and do a calc in another program.

<html>
<head>
    <title>928 REGISTRY</title>
</head>
<body>
    <form method="post">
        <label>Enter VIN:</label>
        <input type="text" name="vin" id="vin" placeholder="...">
        <input type="submit" id="submit here">
    </form>        
</body>
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {    
    $vin = $_POST['vin'];
    $vin=trim($vin);    
         $_POST['jform']['fields'][36] = $vin;

With the same (non) result. I don't think the plugin is being called.

Should either of these methods to acqure the vin work. or is there a third way that I am obviously not aware of?

Thanks again!


Sergey
Total posts: 13,748
13 Июль 2014 12:20

billvv Derrick has updated Cobalt just yesterday to version 8.565. I see that the event update was 8.555; does that one need to be installed first, or will just the later update be OK? I suspect it's OK.

It will be there always not from version 8.555 and above.

billvv Is there a way that I can verify that my plugin code is being executed? I tried an echo statement but no change

Because theer is redirect after save. So when you echo also exit script. Like

var_dump($_POST);
exit;

billvv Can I assign my plugin to a specific Section in the backend, or does it need to be coded somehow?

$app = JFactory::getApplication();
if($app->isAdmin() || $app->input->getInt('section_id') != 5)
{
    return;
}

billvv With the same (non) result. I don't think the plugin is being called.

Did you check plugin is published?


billvv
Total posts: 55
13 Июль 2014 20:21

Did you check plugin is published?

Duh.... It is now! Thanks and sorry.

It will be there always not from version 8.555 and above.

So... if I update to 8.565

Publlished plugin and now trying to call a simple program......

Posted a new article with a couple other fields filled in and when I save and close the new article it has the fields I entered, but does not have field 41 filled in. Any suggestions?


billvv
Total posts: 55
13 Июль 2014 21:49

Hope this looks better. I do not see field 41 text after I post a new article.

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

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

class plgMintPluginname extends JPlugin
{
        public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
        if($isnew)
         {
        $year_model = "Test Year_Model Field";
     $_POST['jform']['fields'][41] = $year_model;       
     }
//end public function
}

//end class
}
?>

Sergey
Total posts: 13,748
14 Июль 2014 01:13
  1. in onBeforeArticleSaved only put echo 1; exit; and try to save and see if it is triggered at all.
  2. If it is then see $isnew if it is true.

billvv
Total posts: 55
15 Июль 2014 21:26

The article saves with the message "item successfully submitted" and returns to the article list. I'm guessing that's not good.

Here's my code

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

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

class plgMintPluginname extends JPlugin
{
        public function onBeforeArticleSaved($isnew, $record, $section, $type)
        {
      echo 1;  
      exit;   

//end public function
    }

//end class
}


billvv
Total posts: 55
15 Июль 2014 21:31

By the way, The first time I revised the code and saved it, I had an extra '}' at the bottom. Cobalt would not run until I disabled the plugin. (meaning I could not save articles in the frontend and could not access the backend.) Then I found the extra '}', removed it, enabled the plugin, and Cobalt came back.


Sergey
Total posts: 13,748
16 Июль 2014 11:13

plgMintPluginname is an example or your name of the plugin pluginname?


billvv
Total posts: 55
16 Июль 2014 16:34

No, my plugin name is:

Plugin.JPG


billvv
Total posts: 55
17 Июль 2014 18:00

Should I change the name of my plugin?


Sergey
Total posts: 13,748
17 Июль 2014 23:50

No You should change the name of your class to plgMintVinpreload.


billvv
Total posts: 55
18 Июль 2014 01:53

Duh.....

You have great patience, Sergey.

Работает на Cobalt