0

say my custom content type has 3 different fields: max, min, mean. What I want to achieve is group them into a new field say statistics so the new statistics field will have all the values in fields max, min, mean.

In terms of display, I now have several walk around solutions.

  1. in node--examplecontent.html.twig template:

    statistics:{{ node.field_max }}/{{ node.field_min }}/{{ node.field_min }}

    but the problems with this is that "statistics" is hard code so it can't benefit from the translation module if I want to build a multi-lang site.

  2. or I could define a dummy field "statistics", and hide it in the form display so user wont need to enter the value of "statistics" when create new content of the content type. then in node--examplecontent.html.twig template:

    {{ node.field_statistics.fieldDefinition.label }}:{{ node.field_max }}/{{ node.field_min }}/{{ node.field_min }}

    this will allow me to translate to label of field statistics to have it dispaly as a different language.

I wonder if there are some better ways to achieve it, thank you.

3

I think there are several ways to go about this, but with your current field setup and familiarity with template you can use the trans functionality in Twig (see https://www.drupal.org/node/2047135).

{% trans %}
  statistics: {{ node.field_max }}/{{ node.field_min }}/{{
node.field_min }}
{% endtrans %}

Other ways of doing things:

  • hook_entity_extra_field_info()
  • Display data via views (see nizz0k's answer)
  • Translate in PHP in the template preprocess function
  • Create a new field type that stores, min, max, and computes statistics via a Typed Data method.
  • Similarly to the above, add a typed data data type plugin to calculate statistics.
  • 1
    As I recall, trans doesn't get translations from Drupal, so you don't get from {% trans %} the same string a module gets. – kiamlaluno Feb 4 '16 at 16:56
  • 1
    No, I was wrong: drupal.stackexchange.com/questions/187057/…. – kiamlaluno Feb 4 '16 at 17:13
  • Yes, simply translating that is a perfectly valid solution. You could use Field Group to visually group those fields together in the UI. One of the few limitations if your fields would have multiple values. Then it would get confusing to edit it. A solution for that could be the use of modules like Paragraphs or Field collection. – Berdir Feb 5 '16 at 0:22
  • This seems to be what I need, but where I can do the translation after wrapping "statistics" with {% trans %}? For example I can translate the label of max at "Home-Administration-Structure-Content types-examplecontent-Manage fields". But I can't figure out where I can do the translation of elements in {% trans %}. – MuSaka Feb 6 '16 at 13:53
0

If you're doing this is views, you can rewrite the output of the fields into a single dummy field. Simply put the fields you want to combine above the dummy field and exclude them from the display, then use tokens to write your fields into your dummy field to display them all.

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.