⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callbackAddField has no information about the field.



ryan-bb

ryan-bb
  • profile picture
  • Member

Posted 27 June 2019 - 11:03 AM

Hi,

 

I am in the process of upgrading my instance of grocery CRUD to the enterprise version and have noticed some missing functionality in the field callbacks which is holding me back.

 

When calling callback_field, you passed through the following parameters ($fieldValue, $primaryKey, $fieldInfo = null, $fieldValues = null). $fieldInfo had important information about the field including name, required etc. that I was using to form a new field.

 

Now in the new enterprise version, I need to call callbackAddField to achieve the same thing. This however, passes no information through about the field and as far as I can tell there is no way to get this information from the $crud instance.

 

Here is an example of one of the fields I am creating for a custom datetime picker (I have stripped out some of the logic to make it clearer) :

public function fieldDatePicker($fieldValue = null, $primaryKeyValue = null, $rowData = null): string
{
    $readOnly = $rowData->crud_type == 'readonly' ? ' readonly' : '';
    $required = $rowData->required ? ' required' : '';

    // More logic stripped out for this example

    return '<input type="text" class="bsdatepicker form-control" autocomplete="off" value="' . $fieldValue . '" name="' . $rowData->name . '"' . $readOnly . $required . '>';
}
$this->crud->callbackAddField($field, [$this->callbacks, 'fieldDatePicker']);

I understand that I can use "function () use ($field) {" to pass additional data through to the callback, however it does not line up with the data already set by Grocery CRUD so seems like a step in the wrong direction.

 

Is there any way to get information about a field to pass through to the callback?

 

Thanks


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 29 June 2019 - 08:27 AM

Hi,

 

I am in the process of upgrading my instance of grocery CRUD to the enterprise version and have noticed some missing functionality in the field callbacks which is holding me back.

 

When calling callback_field, you passed through the following parameters ($fieldValue, $primaryKey, $fieldInfo = null, $fieldValues = null). $fieldInfo had important information about the field including name, required etc. that I was using to form a new field.

 

Now in the new enterprise version, I need to call callbackAddField to achieve the same thing. This however, passes no information through about the field and as far as I can tell there is no way to get this information from the $crud instance.

 

Here is an example of one of the fields I am creating for a custom datetime picker (I have stripped out some of the logic to make it clearer) :

public function fieldDatePicker($fieldValue = null, $primaryKeyValue = null, $rowData = null): string
{
    $readOnly = $rowData->crud_type == 'readonly' ? ' readonly' : '';
    $required = $rowData->required ? ' required' : '';

    // More logic stripped out for this example

    return '<input type="text" class="bsdatepicker form-control" autocomplete="off" value="' . $fieldValue . '" name="' . $rowData->name . '"' . $readOnly . $required . '>';
}
$this->crud->callbackAddField($field, [$this->callbacks, 'fieldDatePicker']);

I understand that I can use "function () use ($field) {" to pass additional data through to the callback, however it does not line up with the data already set by Grocery CRUD so seems like a step in the wrong direction.

 

Is there any way to get information about a field to pass through to the callback?

 

Thanks

 

Hello @ryan-bb,

 

Actually this is probably something that I've missed and it seems also a good idea to pass any information we need to the field. In fact it was so good as an idea that I've started working on it. You can see below the diff change (as per version 2.7.5): https://gist.github.com/scoumbourdis/77956759bf25800f4580bb9475807954

 

so the final function getFieldTypesAddForm will look like this (at: src/GroceryCrud/Core/State/StateAbstract.php):

 

public function getFieldTypesAddForm() {
        $fieldTypesAddForm = $this->gCrud->getFieldTypesAddForm();
        $callbackAddFields  = $this->gCrud->getCallbackAddFields();
        $fieldTypes = $this->getFieldTypes(false);

        foreach ($callbackAddFields as $fieldName => $callback) {
            $fieldType = array_key_exists($fieldName, $fieldTypes) ? $fieldTypes[$fieldName] : null;

            $fieldTypeModel = new ModelFieldType();
            $fieldTypeModel->dataType = GroceryCrud::FIELD_TYPE_BACKEND_CALLBACK;
            $fieldTypeModel->defaultValue = $callback($fieldType, $fieldName);

            $fieldTypesAddForm[$fieldName] = $fieldTypeModel;
        }

        return $fieldTypesAddForm;
    }

The src depends in which framework you are using Grocery CRUD Enterprise. For example in Codeigniter framework the src folder is basically the folder: 

application/libraries/GroceryCrudEnterprise/

I've added also as a task so it will be implemented on the next version (2.7.6): https://trello.com/c/oFVSPc3F/30-gc-404-callbackaddfield-has-no-information-about-the-field

 

If you simply apply the diff change you will see that this is working with the following example:
 

$crud->callbackAddField('contact_last_name', function ($fieldType, $fieldName) {
    /** $fieldType will look like this:
     GroceryCrud\Core\Model\ModelFieldType Object
    (
        [isNullable] => 
        [dataType] => varchar
        [defaultValue] => 
        [permittedValues] => 
        [options] => stdClass Object
            (
                [maxLength] => 50
            )
        [isRequired] => 
        [isReadOnly] => 
        [isSearchable] => 1
    )
     *
     * 
     */

    return '<input class="form-control" name="' . $fieldName . '" type="text" value="">';
});

ryan-bb

ryan-bb
  • profile picture
  • Member

Posted 04 July 2019 - 15:27 PM

Hi Johnny,

 

Thanks for your response. That's great news that your working on the feature already!

 

We're using composer to install GC so I'll wait for the release.

 

Thanks for you hard work


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 09 July 2019 - 10:13 AM

Hi Johnny,

 

Thanks for your response. That's great news that your working on the feature already!

 

We're using composer to install GC so I'll wait for the release.

 

Thanks for you hard work

 

Hello @ryan-bb,

 

I am glad to inform you that this version is now released (2.7.7). I've sent you an email as well with the updated version :)

 

In case you are intretested you can download it straight from here: https://www.grocerycrud.com/users/enterprise_latest_version

I've also updated the documentation here: https://www.grocerycrud.com/enterprise/api-and-function-list/callbackAddField/

 

In case you have any questions or issues please do let me know.

Regards

Johnny