Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Alter a view. Edit view result programmatically

By editor , 30 September, 2016
Question

How to Edit a view result programmatically ?

Example : Change title

function MYMODULE_views_pre_render(&$view) {
  if ($view->name == 'view_myviewname') {

    // Set the view title.
    $view->setTitle('New title');
    $result = $view->result;
    // Set the ro title
    foreach ($result as $i => $row) {
      $view->_entity->set('title', 'New titile for row');
      // set / anter field value
      $row->_entity->set('field_name', "new value");    
    }
  }
}

Change a field value/render (Example):
function hook_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];

  $field = $variables['field'];
  $row = $variables['row'];
  if ($view->storage->id() == 'view_name' &&
  $view->current_display == 'view_display_id' &&
  $field->field == 'field_name') {
    $variables['output'] = 'News output';
    // Example of inline styling
    $value = $field->getValue($row);
    $markup = [
      '#type' => 'inline_template',
      '#template' => '{{ yourvar }} {{ yourhtml | raw }}',
      '#context' => [
        'yourvar' => $value,
        'yourhtml' => '<span style="color:red;">Your HTML</span>',
      ],
    ];
    $variables['output'] = $markup;
  }
}

 

CAUTION : Following example is not working for Drupal 9 :

function MYMODULE_views_pre_render(&$view) {
  if ($view->name == 'view_myviewname') {
    $result = $view->result;
    foreach ($result as $i => $row) {

      $view->result[$i]->field_field_myfieldtext[0]['rendered']['#markup'] = "The new text";
    }
  }
}

Tags

  • Drupal 8
  • Code
  • Views
  • Drupal 9
  • Add new comment

Comments5

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

hess (not verified)

10 years 1 month ago

how can i edit a view?

How can i edit a view? I mean where should i add this program? I am really new to D8. trying to learn it.

  • Reply

editor

10 years 1 month ago

In reply to how can i edit a view? by hess (not verified)

Create a new Module and Edit your view

Hello,
To use this hook (like any other), you must create a custom module. Follow this tutorial to know how to create a module.
Create a simple module to use Drupal 8 Hook System

  • Reply

Ruchira (not verified)

9 years 1 month ago

If view having empty result…

If view having empty result then no debug working in views_pre_render(&$view). So waht was another solution to handle no result message programmatically. 

  • Reply

Michael (not verified)

5 years 4 months ago

This is the reason noone outside US is using Drupal

Look you got a simple task.

You would want to add a simple ViewRow with custom content. It should be ultra easy like in other frameworks right? Wrong! Because you need existing entity. You cannot modify it because you need to display existin gentity also. You want to alter view to add non existing records (like empty records for each user) but you can not without using solution that take longer that writing custom controller by yourself.

Same goes for Drupal Commerce, perfect solution for problems that does not exists.

  • Reply

Anonymous (not verified)

3 years 10 months ago

if view has text field …

if view has text field "nothing"

<code>$row->_entity->set('nothing', "new value"); </code>

not found

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer