(As discussed with Karen on IRC)
The following patch does two things :

* Refactor formatters API to be more tightly tied with themes and drupal_render, as initially suggested by dopry.
- formatters are basically theme functions (and thus overridable), operating on a #-array $element.
- field rendering on node_view relies completely on drupal_render cascading rendering
- field data sanitization (XSS prevention, etc) is moved out of formatters (we don't want to let that in the hands of theme overrides) and into a separate step, invoked before rendering (currently hook_field('sanitize'), but that might be revised)
- content_format() API function, used for views output and encouraged for custom themeing, keeps the same behavior

* Allow formatters to handle multiple values
- new 'multiple values' key in hook_field_formatter_info, is either CONTENT_HANDLE_MODULE or CONTENT_HANDLE_CORE (similar to what we now have for widgets)
- content_format() has to be fed with an "$items" array ($node->field_foo) when used with a 'multiple-values' formatter,
and with an $item array ($node->field_foo[n]) when used with a 'single' (regular) formatter

The patch is not quite ready yet, but I'm posting it here for early reviews.
It takes care of text and number fields for now, and leaves userref and noderef fields display broken for now.
It implements a dummy 'foo' formatter for text fields, illustrating what 'multiple values' formatters look like

It still has a few TODOS / open questions, notably :
- best way to implement the 'sanitize' pass : hook_field($op = 'sanitize'), or hook_field_sanitize (similar to hook_field_set_empty) - that's mainly a code-style point.
- the structure of the render array could possibly be unified a bit further between 'single' and 'multiple-values' formatters. I still need to check that.
- not sure of what to do with the $items array passed to template files when using a 'multiple-values' formatter :
we have several 'items', but only one output value. The current patch raises "undefined index $item['view']" warnings.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

Forgot to add : the patch does not take care of fieldgroups yet.
In order to test, please comment out the content of fieldgroup_nodeapi.

yched’s picture

FileSize
30.08 KB

Updated patch, should be complete AFAICT.

- all field modules are ported to the new formatters api
(BTW, the corresponding theme functions are pure logic and no markup, which hardly anyone would be willing to override - the 'formatters as theme' feature is precious for less basic contrib field types, though)

- the method used to hide empty fieldgroups didn't work with the new approach, so fieldgroups theming needed a refresh. While I was at it, I turned the 'simple fieldgroup' style into a template.

- I left the 'sanitize' step as a hook_field op. It makes 2-3 awkward lines in content_format, but I thought it was better than requiring yet another different hook from field modules.

- I left sample render #-arrays for 'single value formatter' and 'multiple values formatters' in code comments, to help visualizing the structures.
It should be noted that these #-arrays embed a full $node for each rendered field.
It does not have any impact on memory usage, because the $node we receive is a reference, and thus we don't create actual new copies, but it makes a reading through a dsm($node->content); much more painful.

- the patch still includes a dummy 'multiple values' formatter ('text_formatter_foo'), that will of course not be actually committed.

I'll wait a couple days for reviews or comments, but I think we're good to go (this was our last API-breaking task before an alpha release)

yched’s picture

FileSize
31.66 KB

Previous patch didn't include userreference.

eaton’s picture

Subscribing. A quick smoke-test indicates things are working nicely, but I'll be testing it more thoroughly over the weekend. Very, very cool.

yched’s picture

FileSize
31.66 KB

New patch fixes a typo in nodereference 'full node / teaser' formatters - reported by Moshe.

fwalch’s picture

FileSize
32.79 KB

Great work, yched! I was already searching for some functionality like this for my GHOP task to port cck_map to Drupal 6. I had a quick look at the code and noticed two things:

  • In content_handle(entity='formatter'), a variable $formatter is used, which doesn't exist. The attached patch fixes this and changes the $field parameter to $object.
  • in text.module, there's a formatter called 'foo' with the description 'Test multiple'. This is not changed by the patch.
yched’s picture

Yes Beeradb pinged me on IRC about your GHOP task. Placing points on a map is the typical use case for 'multiple values' formatter.

- You're quite right about content_handle params. I tested the 'multiple formatter' part in a previous iteration where I didn't use content_format yet, and then obviously stopped in the middle of the content_handle overhaul. Thanks for the patch.
- The 'foo' formatter is just there to illustrate how 'multiple' formatters work, it will be removed before we commit this :-)

yched’s picture

Status: Needs review » Fixed

The patch didn't apply anymore. We might find we can still further unify the structure of #-render-arrays for 'single' and 'multiple' formatters, but I committed the current state of the code so that we can move on.

deekayen’s picture

subscribe

yched’s picture

Hm. The code that I committed didn't contain the fixes by fwatch in #6. Fixed now.

yched’s picture

As suggested by Karen in our Chicago meetup, I also further unified the render #-arrays structures used for single and multiple formatters.

yched’s picture

Title: Formatters refactoring » Doc for Formatters refactoring
Priority: Normal » Critical
Status: Fixed » Active

Reopening as a reminder that I need to update the docs in sample modules and http://drupal.org/node/191796.
And this is critical...

KarenS’s picture

Status: Active » Fixed

Oops, missed this older issue. I opened a new one for formatter documentation and started working on it.

yched’s picture

Title: Doc for Formatters refactoring » Formatters refactoring

OK, resetting original title for posterity then

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.