Skip to content

Instantly share code, notes, and snippets.

@ValeriiVasyliev
Created August 19, 2017 08:41
Show Gist options
  • Save ValeriiVasyliev/310f437f522e6132c397c5458a7b10ac to your computer and use it in GitHub Desktop.
Save ValeriiVasyliev/310f437f522e6132c397c5458a7b10ac to your computer and use it in GitHub Desktop.
Drupal 8. Change View Field Value

Method 1

/**
 * Implements hook_views_pre_render().
 */
function hook_views_pre_render(&$view) {
  if ($view->name == 'myview') {
    foreach ($view->result as &$row) {
      $row->field_myfield[0]['rendered']['#markup'] = $row->field_myfield[0]['rendered']['#markup'] ? t('Yes') : t('No');
    }
  }
}

Method 2

/**
 * Preprocess vars for views-view-field.tpl.php.
 */
function hook_preprocess_views_view_field(&$vars) {
  if ($vars['view']->name == 'myview' && $vars['field']->options['id'] == 'field_myfield') {
    $vars['output'] = $vars['output'] ? t('Yes') : t('No');
    $vars['field']->last_render = $vars['output'];
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment