Thursday, March 10, 2022

[FIXED] Wrong table displayField being baked even though a name getter contained in entity

Issue

This is the current displayField being set in my table:

$this->setDisplayField('id');

I need it to be:

$this->setDisplayField('name');

However, my database table has bilingual fields instead, such as name_en and name_fr, where I use a getter to populate name in the entity:

protected $_virtual = ['name'];

protected function _getName()
{
    if (Configure::read('wetkit.lang') == 'fr'){
        return $this->_properties['name_fr'];
    } else {
        return $this->_properties['name_en'];
    }
}
  • Should the baked table, with the virtual field in the entity, have displayField set to name during baking?

  • Perhaps the getter isn't available during the table bake process to determine the displayField and defaults to id?

  • Do I have to modify the table.twig file to output accordingly, depending on my structure having name_en and name_fr?


Solution

Ended up modifying the table.twig file:

{%- set setBilingualDisplayField = false %}
{%- for bf in bilingualFields %}
    {%- if bf.name == 'name' %}
        {%- set setBilingualDisplayField = true %}
    {%- endif %}    
{%- endfor %}
{% if setBilingualDisplayField %}
        $this->setDisplayField('name');
{% else %}
        $this->setDisplayField('{{ displayField }}');
{% endif %}


Answered By - TechFanDan

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.