• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Simple example of modifying form data?

chris.paschen

Chris Paschen
I've reviewed the wiki article about PHP plugins for forms:
http://fabrikar.com/forums/index.php?wiki/php-form-plugin/

And I've also searched and reviewed a lot of other forum posts.

I've also spent significant time testing various different syntax to get and save data, as well as trying to do it at various points in the 'process script' timeline.

However, I'm now a bit more confused than ever on how to just make this work.

Here's the task ...

Form Plugin - PHP
PHP File identified
Get a couple of pieces of data from the form and then pass them to a function that I have created.
Do some manipulating of the data (calculations, etc.)
Then update several fields of the form from within the function.

Questions ...

1. At what point should I do this (onBeforeProcess, onBeforeStore, etc.)?
2. What syntax is proper (at the point provided above) for:
get data value
set data value


NOTE: One of the fields is an upload field, and I need to determine if it has a file uploaded (a simple 'not empty' process.

(If I ever wrap my head around the various get/set syntax I might even volunteer to update that wiki page with some clarifications about the use-cases. It's a bit confusing with all the versions and all the points in the process that they are called.)

Thanks for helping clear this up.
 
I should have clarified in question #2 - what syntax to use within the external PHP file/function that can get and set values.
 
On further testing (with a fileUpload element of 'ked_books_books___Upload_pdf_format') I found that ..

Using onBeforeStore
The syntax
Code:
$uploadPDF = $formModel->formDataWithTableName['ked_books_books___Upload_pdf_format'];
will return the proper results (i.e. the path to the uploaded file).

However, when I call the function from within the PHP file and use the same syntax I get NULL (and not even any file type).
And if I use
Code:
$uploadPDF = $formModel->getElementData('ked_books_books___Upload_pdf_format',true);
within the PHP function I get
0 Call to a member function getElementData() on null

And if I call
Code:
$formModel->updateFormData('sys_initial_upload_completed', 1);
within the php function I get:
0 Call to a member function updateFormData() on null

So I guess all I need to know is exactly what format/syntax to use within this PHP file function call that will allow me to get and set data within the function.
 
The way I usually do it if I want to call code from a file is I wrap the code in the file up in a function ...

Code:
function doMyThing($formModel) {
   ...
}

... then call that from the code box ...

Code:
doMyThing($formModel);

As for which hook to use ... onBeforeProcess runs before we upload files. Then onBeforeStore runs after uploading, but before processing to the database.

The onAfterProcess runs after data has been saved to the database, so modifying the formData at that point has no effect. Also note that after processing, the formData array keys will have had the tablename___ prefix removed. That's why we have the formDataWithTableName, for anything that needs to refernce full element names, during onAfterProcess. If you are running before processing, just us formData.

You can either use the functions, $formModel->getFormData() or setElementData(), or you can operate on the $formModel->formData array directly.

NOTE about file uploads. They are a bit tricky, because onBeforeProcess the file path won't be in the element's formData, regardless of whether a file has already been uploaded (editing an existing form), or a nbew one is being uploaded. After upload, during onBeforeStore, the file path will be in the element's formData, again regardless of whether a file was uploaded, or whether a file had already been uploaded.

The only way you can tell if a file is being uploaded on this submission is by looking in $_FILES[].

-- hugh
 
AHHHHH .... it was a problem with the way to call the function (which I was already doing).

I wasn't passing $formModel (I guess I just thought that the model was already available somehow ... not sure how ... maybe I though it could read my mind :)

now $formModel->formData is working like it should (almost ... still trying to debug this a bit).

Thanks!
 
Ah, OK. Yup, if you don't wrap you code in a function, so it just runs without calling anything from the code box, then $formModel will be in scope. But if you wrap it in a function, you'll need to pass $formModel in as an arg.

-- hugh
 
OK THAT explains it. I had another php plugin entry that was running just fine, but that one was a stand-alone piece of code (without a function call) and it was working fine.

I'm definitely flagging this as a key topic to remember.

Is this (scope of the PHP and $formModel) mentioned anywhere in the wiki? I don't remember seeing it - but I was getting a bit blurry eyed with all this :)
 
Well, it's not really a Fabrik thing, it's a PHP thing. Within a function, nothing outside that function is in scope. We have to assume that if people are writing PHP code, they either have some basic knowledge of PHP, or are prepared to go read some PHP docs / tutorials. :)

-- hugh
 
Well, of course I should know that ... but you're now expecting me to THINK as well as code?? :)
If I were coding directly I would have (probably) remembered that ... but when I start working inside of other extensions I sometimes forget the obvious things :)
Thanks for pointing that out, and for the quick help.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top