2025-04-14

1.hook_views_pre_render

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');
      $row->_entity->set('field_name', "new value");    
    }
  }
}

2. function hook_preprocess_views_view_field(&$variables)

function hook_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $field = $variables['field'];
  $row = $variables['row'];

  if ($view->storage->id() == 'view_name' && $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;
  }
}

 

 

 

标签: Drupal