⚠ 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

SETTING CUSTOM FIELDS IN READ PAGE AND RUN CALLBACK ON IT



fede72bari

fede72bari
  • profile picture
  • Member

Posted 22 July 2020 - 15:44 PM

I need to add some custom fields in the view page and obviusly populate them with a callback function for the read mode. I have not found any reference to functions for adding fields in read page in grocery crud documentation; just a reference in a very old topic here mentioning the function set_read_fields that i cannot find after 5 years from that forum topic in the decumentation. Anyway, even if the function gives no errors, when the callback function is called i got a lot of errors; here the log 

 

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2939
ERROR - 2020-07-22 17:26:28 --> Severity: Warning --> Creating default object from empty value /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2950
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2953
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined property: stdClass::$display_as /home/phar263d/public_html/adminPanel/assets/grocery_crud/themes/bootstrap-v4/views/read.php 39

 

 

This is the code i used

$this->grocery_crud->add_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');
        $this->grocery_crud->edit_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');  
        $this->grocery_crud->set_read_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_cate...gory','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled','officies_location');

[...]

$this->grocery_crud->callback_read_field('officies_location',array($this,'_callback_officies_location'));

The callback function is fine (tested); tracking the error i found out that it happens when the rendering function is called

$output = $this->grocery_crud->render();

Any ideas? Thanks.

 

 

 


fede72bari

fede72bari
  • profile picture
  • Member

Posted 22 July 2020 - 16:30 PM

you guys it seems that the new field should be added in $field_types array because it is needed the display_as variable. It seems a real bug. Could somebody answer?


fede72bari

fede72bari
  • profile picture
  • Member

Posted 23 July 2020 - 09:00 AM

in get_field_types(){..} it seems missing a block like these

 

if(!empty($this->add_fields))
   foreach($this->add_fields as $field_object)
   {       ....
   }

if(!empty($this->edit_fields))
   foreach($this->edit_fields as $field_object)
   {      ...
    }

for the read_fields so that these are never added to the main data structure $this->field_types. Please could somebody check this?

 


fede72bari

fede72bari
  • profile picture
  • Member

Posted 23 July 2020 - 09:54 AM

UPDATE       

 

I have partially fixed the problem modifying the core controller adding this code

 if(!empty($this->read_fields))            
      foreach($this->read_fields as $field_object)
            {
                $field_name = isset($field_object->field_name) ? $field_object->field_name : $field_object;


                if(!isset($types[$field_name]))//Doesn't exist in the database? Create it for the CRUD
                {
                    $extras = false;
                    if($this->change_field_type !== null && isset($this->change_field_type[$field_name]))
                    {
                        $field_type = $this->change_field_type[$field_name];
                        $extras     =  $field_type->extras;
                    }


                    $field_info = (object)array(
                        'name' => $field_name,
                        'crud_type' => $this->change_field_type !== null && isset($this->change_field_type[$field_name]) ?
                                            $this->change_field_type[$field_name]->type :
                                            'string',
                        'display_as' => isset($this->display_as[$field_name]) ?
                                                $this->display_as[$field_name] :
                                                ucfirst(str_replace("_"," ",$field_name)),
                        'required'    => isset($this->required_fields) && !empty($this->required_fields) && in_array($field_name,$this->required_fields) ? true : false,
                        
                        'extras'    => $extras
                    );


                    $types[$field_name] = $field_info;
                }
            }

to the funcion get_field_types(). But please consider that there could other things to be better fixed for whch i have not a clear vision due to the fact that I cannot master the whole logic of Grocery Crud library code:

 

1. at the time get_field_types() is called it seems that $this->required_fields is not yet existing so that i get the error 

in_array() expects parameter 2 to be array, null given /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 220

 That is why I have added the control 

isset($this->required_fields) &&

  This allows at skipping the error, but still there could be a problem with the rendering sequence: maybe the first operation should be the creation of all the arrays and data structures even if null, and only after that get_field_types() should be called.

 

2. PLEASE NOTE THAT 

   a. in foreach($this->edit_fields as $field_object) it has been forgotten the control 

!empty($this->required_fields) && 

    that is present in the other similar for loops.

 

   b. the control 

isset($this->required_fields) &&

   should be added to all the other for loops because it is a latent bug.

 

3. I saw in other part of the core code that there is this control

 

elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'readonly')
{
//This empty if statement is to make sure that a readonly field will never inserted/updated
}

but my modification do not set the crud type to 'read_only' and this should be investigated; here a structure print adding a read custom field "officies_location"

    [officies_location] => stdClass Object
        (
            [name] => officies_location
            [crud_type] => string
            [display_as] => Officies location
            [required] => 
            [extras] => 
        )