Score:0

Dynamically add field to Views result row

us flag

i use views_data_export to export a list of nodes to excel. These nodes of type "proposition" have many paragraphs fields, with multiple values.

By default, views_data_export export all paragraphs values in only one cell per field. I want to split these multiple values in multiple columns. One value per column.

I've written a hook_views_pre_render function :

function ehess_propositions_4_views_pre_render(Drupal\views\ViewExecutable $view) {

    if ($view->id() == 'propositions' && $view->current_display == 'export_xls') {
            foreach ($view->result as $key => $value) {
                var_dump($value);
                $index = 0;
                #var_dump($value->_entity);
                if ($value->_entity->hasField('field_participants')) {
                        foreach ($value->_entity->get('field_participants') as $key => $participant) {
                                $index++;
                                #$view->result[$key]->_entity->set("participant_${index}", $participant);
                                $value->_entity->set("participant_${index}", $participant);
                        }
                }
                var_dump($value);

        }
    }
    var_dump($view);
    
}

With no luck!

Have I to declare my new fields in a hook_views_data_alter function?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.