Score:0

Print total value in footer but unable to modify footer value using hook_views_pre_render()

bm flag

Using below code to modify footer value in hook_views_pre_render() but it's not working

function hook_views_pre_render(ViewExecutable $view) {
  foreach($view->result as $result) {
    $totalamt = $result->_entity->amount->value + $totalamt;
  }
  $options = array(
      'id' => 'area_1',
      'table' => 'views',
      'field' => 'area_text_custom',
      'relationship' => 'none',
      'group_type' => 'none',
      'admin_label' => '',
      'empty' => TRUE,
      'tokenize' => FALSE,
      'content' => 'Total is:'. $totalamt,
      'plugin_id' => 'text_custom',
    );
    $view->setHandler('page_1', 'footer', 'area_1', $options);
}
id flag
With apologies if this is a silly question: Is `hook_views_pre_render` the actual name of the function or did you replace “hook” with the module name?
Mario Steinitz avatar
id flag
Apart from that, three suggestions. (1) When creating new hooks or altering the names of existing hooks, always clear your caches. (2) Drupal coding standards, which means short array syntax and two space indentations. If your site is meant to live for a while, a uniformly formatted code base makes it easier to maintain. (3) Check out this module: https://www.drupal.org/project/views_aggregator - Sometimes you don't have to reinvent the wheel.
bm flag
It works when we use https://www.drupal.org/project/views_aggregator thanks @Mario Steinitz
Score:1
us flag

If that is a copy of your code, you need to replace the hook word in the function name hook_views_pre_render with your module name.

EG. Your module is called mymodule

hook_views_pre_render(ViewExecutable $view) => mymodule_views_pre_render(ViewExecutable $view)

https://api.drupal.org/api/drupal/core%21core.api.php/group/hooks

bm flag
Replaced hook with module name eg: web_module_views_pre_render(ViewExecutable $view) Still footer value is not changing.
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.