SearchRecent postsThe Random ToolboxDrupal 7 Setting up variables for use in a template (preprocess and process functions) Rendering Drupal 7 fields (the right way) Linux Secure Copy (scp) command Restrict Free Shipping in Magento Quickbooks Text Connection Ticket Cross-Site Request Forgery (CSRF) jQuery Events: MouseOver / MouseOut vs. MouseEnter / MouseLeave Monthly archive
|
Override Field Value in Drupal Views Attachment using hook_views_pre_render()Overriding views data can typically be accomplished using the commonly used hook_preprocess_views_view(&$vars) hook. However, I ran into a situation where I was unable to override field data using the preprocess hook. While building a view to show a list of products within certain categories, I added an attachment view which rendered the taxonomy name and description. The client wanted to taxonomy name on one page to be different than the actual taxonomy name. First, I tried this <?php function MYTHEME_preprocess_views_view(&$vars) { if($vars['view']->name == 'product_category_description') { if($vars['view']->current_display == 'attachment_1'){ if($vars['view']->result[0]->taxonomy_term_data_name == 'TEXT WHICH NEEDS CHANGED') { $vars['view']->result[0]->taxonomy_term_data_name = 'THE DESIRED TEXT'; } } } } So... after a minute of wondering why my override wasn't taking effect, I realized that I needed to get into the view earlier, before the preprocess work was occuring. You can do this with hook__views_pre_render(&$view). Notice that you are working directly with the $view variable, rather than the full 'vars' array as you do in the preprocess funtion ($vars['view']). function MYTHEME_views_pre_render(&$view) { switch ($view->name) { case 'product_category_description': if($view->current_display == 'attachment_1'){ if($view->result[0]->taxonomy_term_data_name == 'TEXT WHICH NEEDS CHANGED') { $view->result[0]->taxonomy_term_data_name = 'THE DESIRED TEXT'; } } break; default: break; } } |