TransWikia.com

Change can I programmatically change the format of a view?

Drupal Answers Asked by user78498 on December 16, 2020

How can I programmatically change the format of a view using hook_views_pre_render in Drupal 8 or some another method?

enter image description here

2 Answers

If someone is looking to change the field view mode then I have managed this something like below code.

I'm using a taxonomy term view and inside term name field I have chosen Rendered Entity formatter and default view mode. In some case I want to change the view mode so that I can show another design. I made one condition like below

use use DrupalviewsViewExecutable;

/**
* Implements hook_views_post_build().
*/
function ss_children_views_post_build(ViewExecutable $view) {
  if ($view->id() == 'related_theme') {
    $request = Drupal::request()->cookies->get('ss_children');
    if ($request == 'children') {
      $view->field['field_related_theme']->options['settings']['view_mode'] = 'children_icon';
    }
  }
}

Answered by Yogesh Kushwaha on December 16, 2020

You can change the options of a set view format using hook_views_pre_render

An example of this would be:

function HOOK_views_pre_render(DrupalviewsViewExecutable $view) {
    kpr($view->style_plugin->options); // this is where the options a held
}

Not sure if this is exactly what you were after though.

Answered by Eli Stone on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP