User login

The saga of changing a value of a field via a preprocess function, played out on IRC

(Courtesy the Completing a Site (the Other 90%) chapter of Definitive Guide to Drupal 7.)

The short

You can change the value of a field in a preprocess function here:

<?php
$vars['items'][0]['#markup']
?>

Nowhere else!

The long, unnecessary story of figuring this out

Functions investigated:

09:27 mlncn: I'm implementing hook_preprocess_field() and it ran at least a couple times for the field i cared about, and now dpm() produces nothing for that field-- but output for other fields
09:27 mlncn: Has anyone ever heard of such a thing?
09:28 mlncn: the hook is implemented in a module
09:30 agentrickard: dpm() inside preprocess gets killed and never prints, AFAIK
09:30 agentrickard: try debug()
09:33 mlncn: agentrickard: thanks-- progress, it's getting into my if statement and giving nesting level too deep error :-)
09:33 mlncn: trying again with , TRUE
09:34 mlncn: agentrickard: displays nothing. But now i know at least the code is being run
09:35 mlncn: ... and that it will be utter hell to debug without a debugger. Drag.
09:37 catch: mlncn: I nicked your query, ended up needing something more aggressive and made a sandbox project for it: http://drupal.org/sandbox/catch/1077232
09:37 mlncn: catch: awesome :-)
09:39 mlncn: so i have evidence that hook_field_preprocess() is running for a lowly text field, because Drupal crashes out if i put a var_export() in there (debug without the TRUE)
09:40 mlncn: but other than that i cannot get it to do anything -- print debug('hello'), change the field's value, nothing
09:42 mlncn: ok, print_r() works (no set message)
09:42 mlncn: what have we wrought?
09:48 mlncn: hook_preprocess_field() i mean
09:58 mlncn: OK, so the code is running, but i cannot change the values. Am i unclear on the concept?
09:58 mlncn: http://drupalbin.com/17844
09:58 mlncn: the print_r statements print the new values, but the node prints the original value unchanged
10:00 mlncn: the field is an ordinary textfield
10:02 agentrickard reads
10:02 mlncn: i've confirmed it's not the theme overriding. or at least not the custom theme. Same issue in Garland, but i'll try Stark
10:04 mlncn: yep. Same in Stark. The code is running, but it's not doing anything.
10:04 mlncn looks really closely at his ampersand, to see if it is an imposter
10:05 timplunkett: mlncn: maybe dgd7glue_process_field ?
10:05 timplunkett isnt sure about preprocess vs process yet
10:06 mlncn: i should just change #markup i guess
10:08 mlncn: now that i note it is there. though asking it to run the new value through the text_formatter from the preprocess seems like the way we intended things to work?
10:09 mlncn: or hmm-- is there something i'm supposed to set to flag it to render again?
10:12 mlncn: Markup has no effect either. Will try timplunkett's process suggestion, because preprocess seems to do nothing whatsoever
10:13 timplunkett: mlncn: when things that worked in D6 preprocess don't work in D7, i always try process just in case
10:17 mlncn: Same in preprocess. function runs, nothing happens.
10:18 mlncn: Let me guess-- theme_field() is somehow not using the preprocess and process (even though they run!?), and a field.tpl.php needs to exist for any of this to work?
10:24 timplunkett: mlncn: uhhhh yeah, i guess so. http://api.drupal.org/api/drupal/developer--theme.php/function/theme_field/7
10:24 timplunkett: "// This function is never used; see the corresponding template file instead."
10:25 mlncn: so Drupal core without a field.tpl.php defined is wasting its own (and everyone's) time running preprocess and process hooks it will never use
10:25 mlncn: i'd say that performance plus DX makes this a critical bug, but maybe i'm a little biased because i really need my morning back
10:36 mlncn: ok. The functions are used, because i can change the classes. Still can't change any values though
10:43 mlncn: got it
10:47 mlncn: http://drupalbin.com/17845
10:47 mlncn: Only $vars['items'][0]['#markup'] has any effect
11:09 catch: mlncn: preprocess should run for theme functions, I think.
11:09 mlncn: catch: it does. But nothing done to values or even the markup in the '#elements' array has any effect
11:10 mlncn: catch: there's a $vars['items'][0]['#markup'] which is where you can change things
11:10 catch: mlncn: this issue and others http://drupal.org/node/572618
11:10 Druplicon: http://drupal.org/node/572618 => All theme functions should take a single argument to make preprocess sane and meaningful => Drupal core, theme system, critical, closed (fixed), 90 comments, 19 IRC mentions
11:10 catch: mlncn: and this is different to if there's a template?
11:11 mlncn: catch: no, i don't think so. i was just speculating before i finally found the one place the value can be changed where it has an effect
11:13 mlncn: catch: it's easy once you know... and now the readers of a certain book that was just delayed another three hours in coming out will know :-P
11:14 mlncn: guess i'll leave a comment somewhere on api.drupal.org but the fact that there isn't a hook_preprocess_field() defined there makes it hard to know where

Searched words: 
cannot change field value from a preprocess function drupal drupal hook_preprocess_field not running for a field

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.