A couple recent threads raised the following points about build modes:

1) they should be renamed 'view_mode' to be consistent with the direction in #658364: Does build/view/formatter terminology make sense?.

2) hook_field_build_modes() should:
a) be an _info() hook (and have an associated _alter() hook)
b) return an array in the form

'mode_name' => array(
  'label' => '...', 
  'description' => '...',
),
<code>
instead of the current <code>'mode_name' => 'Label'

(no room for help text or additional meta data) - see #553298-21: Redesign the 'Manage Display' screen.
c) be part of the entity API level, not Field API. hook_node_view() receives the $build_mode, meaning non field-related additions can take build modes into account to adjust their behavior.

3) currently a 'full' build mode is assumed on every entity type, and the display settings for this build mode are used when displaying an object in a build mode that the user has not configured yet (e.g: user has enabled search module but didn't configure 'Manage Display' for the 'Seach index' build mode). As pointed in #612894-18: field_format() is just a pile of code that doesn't work, this raises the question of a 'default' build mode.

This last point is not fully mapped out yet, might be best in a separate thread / patch.
Feedback welcome on the other points.

Comments

te-brian’s picture

+1 to 2) at least. I think some help text for each build mode would greatly assist intermediate developers with understanding how to take advantage of them.

I can't really comment on 1) or 3). build -> view does seem to make sense, in that we are not altering the structure or data of the entity so much as we deciding on how they "display".

sun’s picture

Title: build mode API cleanups » Clean-up entity build/view modes

Agreed on all points. Also 3) renaming "full" to "default" -- I was a bit confused when I saw that default argument handling for the first time and asked myself, why we hard-code one of the build modes. If "full" was named "default", then I wouldn't have asked myself this question.

I wonder whether we can even shorten $view_mode to just $mode...? Or perhaps even find a better name... I mean, we are all totally familiar with $build_mode, because we all know that it stemmed from the old $page and $teaser arguments... but honestly, if I'd look at Drupal for the first time, then I'd probably have to consult the handbooks or API documentation to understand and figure out what a "build_mode", "view_mode", or "mode" is..........

yay! The idea: That other patch renamed the argument to $display. And I think, this term is not only exactly what the argument is about ("display as ..."), but also nicely maps to the actual configuration page in the Field UI ("Manage display").

yched’s picture

'full' / 'default':
CCK D6 required a formatter named 'default' for each field type. Was cumbersome, because "other" formatters could use descriptive machine names, but the basic formatter was stuck with the non-descriptive name 'default', which made no sense of its own.
In D7 we changed that: in hook_field_info(), each field type exposes what's its 'default_formatter'.
We could go with something similar here and require entity types to expose what is their 'default_build_mode' in hook_entity_info(). Consistency ++ ?

'build_mode':
'mode or 'view_mode' work for me but I'm fully open on looking for better metaphors.
Possible tracks: flavour (obvious no, but that's the idea), context, style (consistent with image styles), display...
Also: FYI here's the 'plan' I had in mind to avoid 'display config hell': #367498-2: Introduce 'display' as a way to group and reuse instance and formatter settings..
In short: introduce 'display styles' as an indirection between build_modes hardcoded in module code and actual user-defined display settings, letting users say something like 'Full and RSS use Style 1, all other modes (including the ones that will be added by module foo I'll install 6 month from now) use Style 0'. Then, as you add more fields, you only need to maintain 2 sets of display settings, as opposed to one per build mode currently.

We will definitely not be there in core D7, but we might want to adopt names that preserve a way towards this.
Hence I favor 'mode' or 'context' as a replacement for our current 'build_mode', rather than 'display' or 'style' that I was kind of saving for these animals :-p.

effulgentsia’s picture

subscribe

yched’s picture

Status: Active » Needs review
StatusFileSize
new67.34 KB

Attached patch does 1 (goes with 'view_mode' for now) and 2 (no 'description' yet).
the '$build_mode / build_mode / build mode / Build mode' rename accounts for 80% of the patch size...

TODO: 3 - 'full' / 'default' view mode. Pending feedback on my suggestion in #3.

yched’s picture

StatusFileSize
new73.13 KB

With updated docs for hook_entity_view_mode_info(). Currently 'build modes' are actually not documented anywhere in core.
Also, turns out that, of all the alternate proposals (mode, display, style...) 'view_mode' is the one that allows the clearest sentences IMO.

te-brian’s picture

A quick superficial review of #6. I am liking how the info hook looks. One general question would be on the choice of "label". In FAPI its 'title' and 'description', in Menu its 'title' and 'description'. In this case its is just going to be displayed as a label in most interfaces, but is there an argument against title?

Overall I like the view_mode to build_mode change. At least in terms of the comments and the code it seems to work. I especially noticed that the word "display" is deeply linked with this concept and "view" seems more synonymous with "display" than "build" does. There probably is a point to be made that not all "build modes" have to do with visual output, but I think this is a good compromise.

+++ includes/common.inc	21 Dec 2009 20:50:46 -0000
@@ -6388,6 +6389,29 @@ function entity_create_stub_entity($enti
 /**
+ * Returns informations about view modes for an entity type.
+ *
+ * @see hook_entity_view_mode_info()
+ * @see hook_entity_view_mode_info_alter()
+ *

"Returns informations" => "Returns information"

+++ modules/comment/comment.module	21 Dec 2009 20:13:21 -0000
@@ -832,16 +832,16 @@ function comment_view($comment, $node, $
  * Builds a structured array representing the comment's content.
  *
  * The content built for the comment (field values, comments, file attachments or
- * other comment components) will vary depending on the $build_mode parameter.
+ * other comment components) will vary depending on the $view_mode parameter.
  *

The word "built" might cause confusion for people already familiar with the old "build modes".

+++ modules/field/field.attach.inc	21 Dec 2009 20:14:00 -0000
@@ -143,7 +143,7 @@ define('FIELD_STORAGE_INSERT', 'insert')
  *   - The $form in the 'form' operation.
- *   - The value of $build_mode in the 'view' operation.
+ *   - The value of $view_mode in the 'view' operation.
  *   - Otherwise NULL.

Sentences like this seem to make more sense now :)

+++ modules/node/node.module	21 Dec 2009 20:18:13 -0000
@@ -222,27 +222,34 @@ function node_entity_info() {
-function node_field_build_modes($obj_type) {
-  $modes = array();
-  if ($obj_type == 'node') {
-    $modes = array(
-      'teaser' => t('Teaser'),
-      'full' => t('Full node'),
-      'rss' => t('RSS'),
+function node_entity_view_mode_info() {
+  $modes['node'] = array(
+    'full' => array(
+      'label' => t('Full node'),
+    ),
+    'teaser' => array(
+      'label' => t('Teaser'),
+    ),
+    'rss' => array(
+      'label' => t('RSS'),
+    ),

I like how this is looking. Seems more... Drupalish.

+++ modules/system/system.api.php	21 Dec 2009 22:53:31 -0000
@@ -149,6 +149,74 @@ function hook_entity_info_alter(&$entity
+ * View modes let entities be displayed differently depending on the display
+ * context. For instance, a node can be displayed differently on its own page

Double use of "display" seems less than ideal. Maybe "let entities be displayed differently depending on the output context"? I dunno, that's not great either.

+++ modules/system/system.api.php	21 Dec 2009 22:53:31 -0000
@@ -149,6 +149,74 @@ function hook_entity_info_alter(&$entity
+ * Modules defining entity types should use this hook to define the set of
+ * build modes that makes sense for their own display contexts. Third party
+ * modules can also use this hook to define additional build modes for existing
+ * entity types.

"build modes" => "view modes" :)

This review is powered by Dreditor.

sun’s picture

+1! Let me quickly state how cool it is to have someone else look at these kind of patches. And how AWESOME it is that yched rolls them! :) Seems like we're almost done here!

dries’s picture

Overall I like the view_mode to build_mode change. At least in terms of the comments and the code it seems to work. I especially noticed that the word "display" is deeply linked with this concept and "view" seems more synonymous with "display" than "build" does. There probably is a point to be made that not all "build modes" have to do with visual output, but I think this is a good compromise.

Does this suggest that 'display mode' is the better name?

te-brian’s picture

@Dries
After looking at where these "build_mode" or "view_modes" live in the wild, I think it would hurt code readability to call it "display_mode", because the word display is used in many functions as a variable. I do think "display_mode" matches up nicely with the "manage display" tab, but there is no reason that tab could not be "Manage View Modes" or something like it. I think to make "display_mode" readable, some other changes to the code would have to be made.

dries’s picture

OK, let's stick with 'view_mode' for now.

yched’s picture

StatusFileSize
new70.98 KB

Rerolled + updated for the remarks in #7: I'm on the road, just a lame Eclipse setup, so no -uP patch today :-(.

- title / label: entity API and Field API are both consistent on 'label' for human readable names of their 'things' (entity types, field types, formatters, widgets...). FAPI #title is something different in a different land, not really relevant here IMO. 'Node' is not the 'title' of the node entity type.

-

+++ modules/comment/comment.module 21 Dec 2009 20:13:21 -0000
@@ -832,16 +832,16 @@ function comment_view($comment, $node, $
  * Builds a structured array representing the comment's content.
  *
  * The content built for the comment (field values, comments, file attachments or
- * other comment components) will vary depending on the $build_mode parameter.
+ * other comment components) will vary depending on the $view_mode parameter.
  *

This is the PHPdoc for comment_build_content(), which #658364: Does build/view/formatter terminology make sense? left with 'build', and whose role, according to the func description, in still to 'build' a renderable array. Might be argued I guess, but let's leave it this way here.

- LOL at 'build mode' occurrences left in the newly added help text. I did the 'build mode' => 'view mode' replacements, then wrote out those couple lines, but 'build mode' does have a nice music that sticks in your brain and fingers.

Other points are fixed.

Also, I leave out the 'full' / 'default' mode question for a followup, the patch is big enough like this, and will get stale pretty easily.

te-brian’s picture

Patch applied. No obvious problems. I guess we are waiting on bot.

sun’s picture

+++ modules/node/node.module	22 Dec 2009 21:31:42 -0000
@@ -222,27 +222,34 @@
- * Implements hook_field_build_modes().
+ * Implements hook_entity_view_mode_info().
...
-function node_field_build_modes($obj_type) {
...
+function node_entity_view_mode_info() {

Hm. That's a pretty long info hook name... Of course, it makes sense in the context of this patch...

But when taking a (large) step back, then I seriously have to wonder why this info is not simply contained in hook_entity_info() instead? Don't those view modes 100% depend on the entity, and, doesn't (almost) every entity require at least one view mode? (unless it defaults to a default/full view mode like all of the current core entities seem to do)

So, when taking this step back, I really have to wonder why this is a separate hook at all.

This may sound like scope-creep for this issue/patch, but then again, we seem to touch every single line that deals with build/view modes in core in here...

I know that we already have a lot of properties in hook_entity_info() and that adding view modes would increase that list - but from a technical standpoint, I think it really belongs together, and would also clarify the task of a larger API revamp in D8, since we'd know most/all all of the entity properties due to the possible registry information.

The hook currently seems to be in place so that we can support registering view modes for entities that are not registered by the same module (e.g. node/search). However, for such purposes, we already have hook_entity_info_alter().

Thoughts?

I'm on crack. Are you, too?

te-brian’s picture

@sun

But when taking a (large) step back, then I seriously have to wonder why this info is not simply contained in hook_entity_info() instead?

Makes sense, but this is probably a little deeper than I'm willing to commit an opinion :)

yched’s picture

Merging into hook_entity_info(): that's another one I've been going back and forth since we introduced build modes. I'm a little worried of cycle logic - where you'd need the entity_info() in order to build the list of view modes.
For instance: 'add a view mode to all entities having this property' - could probably be done in hook_entity_info_alter(), though.

Pondering a little more...

Status: Needs review » Needs work

The last submitted patch, , failed testing.

Status: Needs work » Needs review

Re-test of from comment #2400422 was requested by @user.

Status: Needs review » Needs work

The last submitted patch, , failed testing.

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new69.85 KB

Attached patch merges 'view modes' info into hook_entity_info() as suggested in #14.
As previously, no -uP patch on my Xmas holiday config :-p.

I cannot think of any hard drawbacks, only slightly annoying side effects:
- it makes adding new view modes to existing entity types is a little 'hidden' (hook_entity_info_alter() instead of hook_entity_view_mode_info())
- there is no obvious place to explain what 'view modes' are. Patch #12 had this in the PHPdoc for hook_entity_view_mode_info():

+ * Exposes information about view modes for entity types.
+ *
+ * View modes let entities be displayed differently depending on the context.
+ * For instance, a node can be displayed differently on its own page ('full'
+ * mode), on the home page or taxonomy listings ('teaser' mode), or in an RSS
+ * feed ('rss' mode).
+ *
+ * Modules taking part in the display of the entity (notably the Field API) can
+ * adjust their behavior depending on the requested view mode.
+ *
+ * Modules defining entity types should use this hook to define the set of
+ * view modes that makes sense for their own display contexts. Third party
+ * modules can also use this hook to define additional view modes for existing
+ * entity types.

This is now crammed into the @return info for the hook_entity_info().

Status: Needs review » Needs work

The last submitted patch, , failed testing.

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new68.18 KB

Invalid patch format ? Hm, does the bot refuse Eclipse non -uP patches now ?

Retrying, with a double check on win line endings...

effulgentsia’s picture

1) I think this patch is an awesome improvement to D7!

2) Apologies for not chiming in earlier, but having read the above issue comments and patch #22, I'm wondering why not name it $display_context. If there's agreement that this is a good name, I'll be happy to roll the patch for it, using #22 as a starting point. I think this name is both clear and $display could still be retained throughout the code to refer to what it already refers to: the settings to use for a particular display context (or the name of the context itself, in a situation where the settings could be retrieved given that name). I also think this would flow nicely with yched's proposal in #3, where "display style" or $display_style could be used to refer to a name for which display settings can be assigned, and then there could be a mapping to say which $display_style to use for a $display_context.

sun’s picture

Thanks, yched! This makes much more sense now!

For me, this patch looks RTBC. I'm not sold on the idea that "display_context" would be a better name... "A display_context of teaser" sounds a bit weird to me. While a display context of "print" would perhaps make slightly more sense, I think that "view mode" really maps best to the functionality's purpose - at least for D7.

So, again, for me this looks RTBC, but I'll leave that to someone else.

yched’s picture

- I vote for 'view' over 'display', because 'view' is already known in core - and used much more consistently now. 'display' doesn't exist, and I see no need to introduce a new synonym.
- i vote for 'mode' over 'context', because 'context' looks like an accurate, specific word referring to a 'technical concept' - which is not what current HEAD's 'build modes' are. They're just flags for a 'flavour', a 'variant'... a 'mode'.
- In addition to the word-by-word comparison, 'view modes' makes better sentences than 'display context' ;-).

effulgentsia’s picture

Status: Needs review » Reviewed & tested by the community

Ok, I'm sold on 'view mode' being a better name than 'display context'. I still like 'display context' more if we were only thinking from a field perspective, where we already do use the word 'display', and where the view mode very much is a context by any definition of the word (a node 'teaser' is a display context for the field). But, thinking about #24 and #25, I agree that we can't just take the field's perspective, especially since view modes are defined for the entity, and from the entity's perspective, 'display' is a new term that conveys no new information than the already accepted term, 'view', and 'mode' is more accurate than 'context'.

I read through the patch pretty thoroughly, and it looks great. Bot likes it (though perhaps, given the breadth of the patch, should be re-run prior to commit). So: RTBC.

dries’s picture

Umph, this patch needs a quick reroll because of the other field API clean-up patch that just went in. Sorry about that!

Status: Reviewed & tested by the community » Needs review

Re-test of from comment #2405772 was requested by @user.

yched’s picture

StatusFileSize
new68.53 KB

Yup, rerolled. S**t happens :-p.
Agreed that a bot re-run would be nice, but there are currently no active client.

yched’s picture

StatusFileSize
new69.04 KB

- added missing 'view modes' info in comment_entity_info() (comment module was missing an implementation of hook_field_build_modes() so far)
- initialize 'view modes' as an empty array in entity_get_info()

dries’s picture

Status: Needs review » Fixed

Looks great now. Committed to CVS HEAD. Still running the tests locally though. Thanks!

yched’s picture

Status: Fixed » Reviewed & tested by the community
StatusFileSize
new516 bytes

Quick followup.
That drupal_static_reset() was needed in earlier iterations when entity_view_mode_info() was its own function and hook, but I forgot to remove it when moving that information into the main hook_entity_info() in patch #20.

Trivial, setting straight to RTBC.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks yched.

kika’s picture

What about update guide docs?

cburschka’s picture

Status: Fixed » Needs work
Issue tags: +Needs documentation

Yes, the hook changes should still be documented in the update guide.

yched’s picture

Status: Needs work » Fixed
Issue tags: -Needs documentation

I tried to update http://drupal.org/update/modules/6/7 with the changes in this patch and in #658364: Does build/view/formatter terminology make sense? as well. Added a comment over there.

te-brian’s picture

I opened #671268: Add descriptions for view modes. to see about getting some description copy going.

Status: Fixed » Closed (fixed)

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

fgm’s picture