4

Is there a way to have a CCK Field have two values for one input?

For example, a field called 'Sodium' and two values, '190mg' and '8%'- first being the amount, second being % of daily value.

If not, whats the best way to achieve this?

Edit:

The best solution I am looking for would be sets of Fieldgroups, and each row having 3 inputs. Label | value (required) | value (optional) - so, Sodium | 190mg | 8%

  • your solution appears great to me. Simplest way to achieve what you want. – Nikhil Mar 14 '11 at 17:21
6

Take a look at the CCK multi group options, which are only a part of CCK 3.0+. This functionality allows you to combine multiple base fields into one field grouping. See http://drupal.org/node/119102 for the long history on how this came about.

I actually can't find good documentation on using it, as CCK 3.0 is still only in Alpha, so unfortunately you'll just have to play around with it some.

The other option is to write your own CCK field type, in which there is no issue creating multiple inputs for one field. The upshot is that you can definitely achieve this using your own custom field, the downsides are you are writing custom code (which is nice to avoid) and it's not as flexible as the multigroup options built into CCK 3.0

2

So you want to combine two cck fields in one, in a way?

You could use two seperate fields 'sodium mg' & 'sodium %' - and add a computed field, so you can combine the values of those two fields into one field, 'sodium values' for example.

Some more info about the Computed Field module to make such computed field possible (from its project page):

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

1

I might be missing something but the easiest thing would be to enter the whole string "Sodium | 190mg | 8%" into the text field and then parse out the values at the theme level to do whatever you need to do - putting it in table, making a graph, etc. Underneath the text field put an example, problem solved. No alpha required.

0

The module that allows you to achieve this in Drupal 6 is called Multigroup, and it is part of CCK 3.x, which has an alpha release. This module allows you to add multiple fields to a fieldset, and then presents the fieldset in the familiar 'add another' manner that CCK uses for multi-value fields.

  • Can a site using CCK 2 start using CCK 3 without a lot of issues? – Kevin Mar 14 '11 at 13:56
0

You can add all the data you want to a single CCK field, but you need a CCK type that supports this.

With CCK you can specify, how the data store should be (field_settings), how the form for inputting should be (widget) and how the field should be renderen (formatters).

You're not quite clear on how you want things, but the easiest thing to do, would be to create a have a single input/storage, and use a custom formatter to display the single value as two, based on some regex or similar.

You could also make your widget, that would create a custom form for the field and how the data should be stored. If multiple values is setup for the field, you could use delta to separate the values. This requires more in depth knowledge of the inner workings of CCK, but would give you better data control.

The documentation on doing all of this is very limited, but looking at other modules will give you an idea on how to do it.

  • I'm not sure what you are saying here. – Kevin Mar 14 '11 at 13:42
  • That would depend on the site. The CCK maintainers have stated there will be an upgrade path, but I've never tested moving from 2 to 3. I would recommend extensive testing with 3 to make sure everything is working as expected. – jhedstrom Mar 14 '11 at 14:21
  • Yeah. Its a legacy site with a -lot- of modules. – Kevin Mar 14 '11 at 14:25

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.