fsi-open / datagrid-bundle

Main purpose of this bundle is to register FSi DataGrid Component service and twig datagrid rendering functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mapped field value not available in action contents using YAML

dnagirl opened this issue · comments

I am using YAML to config my grid. I have an action which toggles the 'pause' state of a row. The icon of the action button has to vary depending on the current 'pause' state. Here is the current config of the grid:

columns:
  id:
    type: number
    options:
      label: Identity
  description:
    type: text
  actionStatus:
    type: text
  isNotPaused:
    type: boolean
    options:
      label: Ready
  pausetoggle:
    type: action
    options:
      label: ''
      field_mapping: [pauseIcon, id]
      actions:
        toggle:
          route_name: task_pausetoggle
          parameters_field_mapping: {'id':id}
          url_attr:
            class: 'btn btn-default btn-sm'
            title: 'toggle pause state'
          content: '<i class="%pauseIcon%"></i>'

I have tried the following variations for the action content:

content: '<i class="%pauseIcon%"></i>'
content: '<i class="pauseIcon"></i>'
content: '<i class="value.pauseIcon"></i>'
content: '<i class="%value.pauseIcon%"></i>'

and some others. All output the literal string rather than the interpolated one. What is the correct syntax? Or do I have to use a different method?

@dnagirl if you want to use source values in content of some action you have two options:

  1. Use some closure in content option, but it's obviously impossible in YML file, only in pure php using $datagrid->addColumn()
  2. Create template for your datagrid (https://github.com/fsi-open/datagrid-bundle/blob/master/Resources/docs/templating.md) and block named according to this scheme: datagrid_{grid_name}_column_type_action_cell_action_{action_name}

I intended to use the templating option. The issue was that I couldn't figure out how to get my source value into the template. DataGridExtension ::datagridColumnActionCellActionWidget(), which is called when outputting blocks for 'action' types expects source variables to be in the $fieldMappingValues argument. In the bundle's default datagrid.html.twig , $fieldMappingValues is set to action.field_mapping_values. So far, so good. But there is no configuration variable called field_mapping_values.

It turns out that action.field_mapping_values is populated from the additional_parameters config variable. additional_parameters is defined as "array, additional parameters values for route not related with field_mapping" -- not where one would think to find mapped values. In addition, using additional_parameters adds them to the url as well as making them available to the action template.

I think it would be good to separate the 2 functions of additional_parameters into 2 separate variables. I also think that the variables should be given more descriptive and consistent names. I suggest extra_url_args and field_mapping_values.