According to this description of hook_preprocess_views_view_field(), $variables contains, among other things, $variables['field'].

However, if I do print_r($variables['field']) just to inspect the object it prints the entire object (to include my username and password!) and even causes the browser to crash.

I am trying to figure out how to check the field name but it is proving to be strangely non-straightforward.

How can I properly index $variables?

  • Did you try Devel module? It'll give you dpm(). Then do dpm($variables).
    – leymannx
    Oct 14 '15 at 17:48
  • dpm produces "Content Encoding Error" Oct 14 '15 at 17:54
  • Did you do dpm($variables) or dpm($variables['field'])?
    – leymannx
    Oct 14 '15 at 19:07
  • And you are calling MYTEMPLATE_preprocess_views_view_field($variables) in your template.php and are looking at the view at your frontend?
    – leymannx
    Oct 14 '15 at 19:12
  • Please excuse if these questions may sound stupid to you, I'm just trying to reproduce exactly.
    – leymannx
    Oct 14 '15 at 19:15

The browser is likely crashing because most of the views objects have circular references in them, and the output can be huge.

$variables['field'] is the field handler object for the field being rendered. I am 97% sure these are all derived from \Drupal\views\Plugin\views\field\FieldPluginBase() (3% uncertainty because it is the end of the day and I am really tired).

You best bet is to do a debug(get_class($variables['field'])) to get the fully qualified class name, and then typehint in your preprocess, eg:

function MYMODULE_preprocess_views_view_field(&$variables) {
  /** @var \Drupal\views\Plugin\views\field\Boolean $field */
  $field = $variables['field'];

and then use your IDE to navigate through what you need to do.

To see what is in $variables, just do a


and go through a similar exercise to get the types; it's just a boring associative array where the keys are all strings.

  • debug(get_class($variables['field'])) produces Drupal\views\Plugin\views\field\Field. debug(array_keys($variables)) produces Array ( [0] => view [1] => field [2] => row [3] => theme_hook_original [4] => attributes [5] => title_attributes [6] => content_attributes [7] => title_prefix [8] => title_suffix [9] => db_is_active [10] => is_admin [11] => logged_in [12] => user [13] => directory [14] => output ) Oct 15 '15 at 11:33
  • @BrianHuether So what are you trying to accomplish? The Field handler plugin allows Views to render entity data using the field formatters (ie, the Field API) rather than the equivalent Views plugin. drupal.org/node/2393339 may provide a little context around this.
    – mpdonadio
    Oct 15 '15 at 12:49
  • For now, I am just trying to figure out how to do a check on the field name so that I can then alter the markup for a given field. I am sure once Drupal 8 is officially released there will be a ton of tutorials on this stuff! Oct 15 '15 at 14:03

With this code you will get the field name:


or you could also want


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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