1

When viewing a node, how can I display all values of a List (text) instead of just the selected ones.

So, instead of displaying:

B (good)

I want to display all values, with some markup telling selected and unselected apart:

[_] A (superior)
[_] A-
[_] B+
[X] B (good)
[_] B-
[_] C+
[_] C (average)
[_] C-
[_] D+
[_] D (poor)    
[_] F (failure)
[_] I (incomplete)
[_] PR (course in progress)
[_] W (withdrew from course)
[_] FF (failing in a pass/fail course)
[_] PP (passing in a pass/fail course)
[_] N/C (not for credit)   

Now, I have actually solved this already for myself and for some others (who had desperately been searching for this functionality) in my sandbox module https://drupal.org/sandbox/kari.kaariainen/1349570, but I just wanted to ask if anyone else has a) needed this, b) solved it and how?

Edit: my solution is to rebuild the $items at hook_field_formatter_prepare_view so that it doesn't have just the selected items, but all items, including data about whether an item has been selected or not. $items will then be handled by hook_field_formatter_view actually outputting items with added markup and translated.

It's kind of frustrating to bring back data at prepare_view, so I'm wondering if data for all items could be retrieved from database initially.

Another thing is i18n support, which I'm now copying from i18n module. Code for each field type (there will be more than just text lists) will need to be duplicated and modified separately. I'm wondering if field translation could be left for i18n and not duplicating code.

  • You may want to bring a bit of your code here for people to get quick access to your solution. Also, I found this which is probably what you did? drupal.org/node/1722166 – NickO Mar 24 '14 at 11:59
  • Thanks, I added description of my solution to the question. Your link was about listing fields, my question is about listing values. – Kari Kääriäinen Mar 25 '14 at 17:18
  • Looking for a solution for Drupal 8 – unor Apr 29 '18 at 23:52
2

Even if no one here says they have needed to do that you can pretty much bet others out there have, so it's good that you have your code on drupal.org where other can use it if they need it.

I had a quick look over your code and your solution is the way I would do it: create a new field formatter for text list fields using hook_field_formatter_info() and hook_field_formatter_view().

A couple of things I notice though are:

  1. The name of the module is very long. If you can make it shorter while still being somewhat descriptive I think that would be good (also better for you as the coder).
  2. The markup you are outputting is not themeable, so if other people start to use the module and want to override the markup in any way it is harder for them. See image_field_formatter_view() as an example of creating a render array with a specific theme function. Also see hook_theme() for defining your own theme functions.
  3. Text that gets displayed to the user that is hard coded into the module should be run through the t() function so that others can write translations for your module. See the drupal 6 version of that t() api docs page for better and more thorough information. t() is not currently used is for $form['instructions'].
  4. This one is a bit more of a personal choice but I would probably make the markup options a lot less than 500 characters. I would also quite possibly not allow HTML markup and just allow them to enter characters that would then display in a span. If the user wants something fancy they can override the theme function (as per point 2). Also, the instructions encourage users to use inline styles, which are not recommended. Users should use the classes you provide to add any css in their theme's css files.
  • Thanks a lot! I tried to come up with a shorter name but couldn't. I might put up an issue about it in the sandbox to get suggestions. This started out as a formatter for text lists only, but now I'm extending it to term references, and possibly other field types. That may affect the name choice. Markup can be freely defined in settings form. I'll think about it, what to have there, and what to make themable. Thanks. – Kari Kääriäinen Mar 25 '14 at 11:56
  • Do you know if it's possible to change a sandbox module name without starting a new one? – Kari Kääriäinen Mar 28 '14 at 6:13
  • I don't see why not. You can't change the repo name but that is just an id number. You can change the name of the project when editing the project page. Then just change the names of your files. – rooby Mar 28 '14 at 7:42

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.