11

I have a view with field Global: Custom Text, which is modified as a link. I want to change replacement tokens for generating this link, dependent on a value of other field.

Tried to modify this in hook_views_pre_render but I can't find a way to do this.

In hook_views_post_render there are two arrays:

$view->style_plugin->render_tokens
$view->style_plugin->row_tokens

which contains generated links for every row, but they are not present in pre_render, and changing them in post_render doesn't work.

I've also tried to do that in template_preprocess_views_view_fields but for that specific view this function does not firing (for other views it does), and I don't know why.

2 Answers 2

15
function hook_views_pre_render(&$view) {

  switch ($view->name) {
    case 'YOUR_VIEW_NAME':
      //  override the global custom text field value
      $view->field['nothing']->options['alter']['text'] = 'My custom text';
    break;
  }
}

Check the link for more details How to insert values into Global custom text field of views programmatically?

add nid as field in view and use that field for condition you can add condition as follows: if ($nid == '124') { $view->field['nothing']->options['alter']['text'] = $nid}

Simple way is to create a new field field_waga which will have field_waga_1 as new name if you check in the view and then you can change the label and override that field with your custom value

if($view->result[0]->field_waga[0]['raw']['value'] == '10') {
//here you can create the field to act as custom text field and enter the custom //value
$view->result[0]->field_waga_1[0]['#markup'] = 'alter';
}
8
  • This solution changes every field to 'My custom text', I cannot see an option to alter only chosen fields, and to alter a link (if field is modified to display as a link) this way. Unfortunately I can't use a preprocess_views_view_fields() function, which is mentioned in link you provided, because for unknown (for me ;) ) reason this function is not fired for this specific view.
    – dmk.i-t
    Commented Dec 4, 2013 at 11:12
  • you can add condition as follows: if ($nid == '124') { $view->field['nothing']->options['alter']['text'] = $nid}
    – harshal
    Commented Dec 4, 2013 at 11:20
  • check updated answer
    – harshal
    Commented Dec 4, 2013 at 11:22
  • Added condition in this way: if($view->result[0]->field_waga[0]['raw']['value'] == '10') { $view->field['nothing']->options['alter']['text'] = 'ALTER'; } If a have a value '10' in first row than all fields 'nothing' has value 'ALTER'. Not only first, but all rows, unfortunately. I think it is a global alter and it cannot be used to chosen rows.
    – dmk.i-t
    Commented Dec 4, 2013 at 11:30
  • 1
    No just add a field_waga which you already have again using the add field and then use that field as custom field to input your custom values
    – harshal
    Commented Dec 4, 2013 at 12:16
0

As an update to the above answer, I had to set 'alter_text' to TRUE in order for this to work, e.g.:

function hook_views_pre_render(&$view) {

  switch ($view->name) {
    case 'YOUR_VIEW_NAME':
    //  override the global custom text field value
    $view->field['nothing']->options['alter']['text'] = 'My custom text';
    $view->field['nothing']->options['alter']['alter_text'] = TRUE;

    break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.