Is it currently possible to change the text that displays on a Flag link field in Views? I can't seem to figure it out. If I use "rewrite the output of this field" and set it to a token, I don't have any way to reference the link URL.

Comments

barraponto’s picture

Category: support » feature

I'm going through the same problem, I'll see if I can get anything done.

barraponto’s picture

So, I managed to theme it from the field_handler. However I was using the AJAX link, and once it is clicked on, the result will be the default flag text. There's absolutely nothing views can do to change that text, at least with the current implementation of the AJAX response. Here is the code I used:

function MYTHEME_preprocess_views_view_field(&$vars) {
  $view = $vars['view'];
  $field = $vars['field'];
  // you might also need to check view display and real_field
  if ($view->name == 'MYNAME' && $field->field == 'MYFIELD') {
    $content_id = $row->{$field->aliases['content_id']};
    $is_flagged = $row->{$field->aliases['is_flagged']};
    // this is the secret, the field handler implements a get_flag() method
    $flag = $field->get_flag();
    $flag->flag_short = t('A NEW TEXT FOR THE LINK');
    $flag->unflag_short = t('You probably need this as well');
    // finally, render the output
    $vars['output'] = $field->theme($is_flagged ? 'unflag' : 'flag', $content_id);
  }
}

I don't know what can be done about the ajax response, maybe just send a views param, and let the theme function use some specificity like theme('flag' . '__' . $viewname); , which would help the themer a lot. It could be used in the handler render() method as well, to save themers a lot of time.

joachim’s picture

Version: 7.x-2.0-beta6 » 7.x-3.x-dev

The Views handler just calls $flag->theme(), so this issue is dependent on that taking more parameters. Which IIRC there is an issue for.

rodyk’s picture

For me the right code is :

<?php
function MYTHEME_preprocess_views_view_field(&$vars) {
  $view = $vars['view'];
  $field = $vars['field'];
  $row = $vars['row'];
  // you might also need to check view display and real_field
  if ($view->name == 'MYNAME' && $field->field == 'MYFIELD') {
    $entity_id = $row->{$field->aliases['entity_id']};
    $is_flagged = $row->{$field->aliases['is_flagged']};
    // this is the secret, the field handler implements a get_flag() method
    $flag = $field->get_flag();
    $flag->flag_short = t('A NEW TEXT FOR THE LINK');
    $flag->unflag_short = t('You probably need this as well');
    // finally, render the output
    $vars['output'] = $flag->theme($is_flagged ? 'unflag' : 'flag', $entity_id);
  }
}
?>

It works like this ;)