4

I'm currently working on a custom field, where I manually add a checkbox to a file upload field. PDF Upload field

In onContentBeforeSave I want to read this checkbox to decide what to do with the file upload. I figured out to get the checkbox with

$jinput->post->getArray()['jform']['com_fields'][$fieldname.'_choverride']

It works, but I thought it's a pretty ugly call.

Is there a nicer, better way to do this?

Edit: Other solutions

Solution 2

$choveride_res = $postData->getArray(array(
            'jform' => array(
                'com_fields' => array(
                    $fieldData["fieldName"] . '_choverride' => 'string'
                )
            )
        ));
$res = $choveride_res['jform']['com_fields'][$fieldData["fieldName"] . '_choverride'];

Solution 3

$postData  = JFactory::getApplication()->input->post;
$jform     = new JRegistry($postData->get('jform', null, 'array'));
$comFields = new JRegistry($jform->get('com_fields', null, 'array'));
$choveride = $comFields->get($fieldData["fieldName"] . '_choverride', '', 'string');
6
  • Please read Harald's advice on this topic. You should be implementing the available filtering techniques on the user input data. Also, of course, the manual: docs.joomla.org/Retrieving_request_data_using_JInput Commented May 6, 2019 at 12:15
  • @mickmackusa I did read those articles, but the solutions are not very pretty too. See my edited article with solution 2 and 3. Did I miss a solution? Atm I would prefer solution 2.
    – svanschu
    Commented May 6, 2019 at 21:42
  • You should probably be using data passed to onContentBeforeSave rather than data from input. Since the plugin is meant to be usable across multiple components, form control (jform) should not be hardcoded as it is configurable.
    – Sharky
    Commented May 7, 2019 at 5:33
  • Then @Sharky it would be nice of you to tell me how to pass it there.
    – svanschu
    Commented May 7, 2019 at 6:43
  • I'm not particularly familiar with com_fields but this would be done in onCustomFieldsPrepareDom. Both inputs (upload and override checkbox) should be a part of the XML form definition (currently the checkbox only appears in HTML markup). One way is to use subform (like Fields - Repeatable) or similar approach.
    – Sharky
    Commented May 8, 2019 at 7:01

1 Answer 1

1

I agree that your "Solution 2" is better because it implements a more readable syntax.

I might be a little too nitpicky because I have been spending time volunteering at CodeReview, but perhaps you could clarify your variable naming. Your abbreviated naming is probably very clear to you, but it will make your code more readable for other developers if you avoid shortening your variable names. $choveride_res = "checkbox override result" and $res = "result" Mind you, I have seen far worse/ambiguous variable naming, I just think there is some benefit to be had from using a few more characters.

Maybe something like:

$filteredPostData = $postData->getArray(array(
            'jform' => array(
                'com_fields' => array(
                    $fieldData["fieldName"] . '_choverride' => 'string'
                )
            )
        ));
$checkboxValue = $filteredPostData['jform']['com_fields'][$fieldData["fieldName"] . '_choverride'];

Ultimately, you've found your solution, I just wanted to give you confirmation and some food for thought.

1
  • You're right. The short names came from playing around with the solutions. But if you don't do it always the right way, playing code stays in production ^^
    – svanschu
    Commented May 7, 2019 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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