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

  • Change it from what form to what other format? Where is this view being displayed? And why would you want to do it programmatically? Why not just from the UI? – leymannx Feb 22 at 14:06

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(\Drupal\views\ViewExecutable $view) {
    kpr($view->style_plugin->options); // this is where the options a held

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

| improve this answer | |

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 Drupal\views\ViewExecutable;

* 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';
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy