3

With the 'field collections' module at the form level, you can create a fieldset (or 'field group') that will accept multiple sets of values.

enter image description here

Is there a way to do this with the Form API directly (or with the help 'field collections' or 'field group' module) programmatically. I can make the form fieldset easy enough, but I'm not sure how to set multiple values for this set. In fact, I'm not really sure how you define multiple values for any field with the Form API.

UPDATE: Just to be clear I want to create the form, not the entity itself (at least not yet). I want to use the Form API to accomplish this to make my own $form – just the form.

0

Suppose 'field_page_collection1' is the field collection for the 'page' content type having two text fields 'field_page_collection1_text1', 'field_page_collection1_text2'.

To attach or create field-collection item for node having node id 1.

<?php
$node = node_load(1);
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_page_collection1')); // Create new field collection item.
$field_collection_item->setHostEntity('node', $node); // Attach it to the node.
$field_collection_item->field_page_collection1_text1[LANGUAGE_NONE][0]['value'] = 'some value for text field 1'; // Fill value for field_page_collection1_text1.
$field_collection_item->field_page_collection1_text2[LANGUAGE_NONE][0]['value'] = 'some value for text field 2'; // Fill value for field_page_collection1_text2.
$field_collection_item->save(); // Save field-collection item.
?>
| improve this answer | |
  • just to be clear I want to create the FORM, not the entity itself (at least not yet). I want to use the FORMapi to acomplish this to make my own $form---just the form. – Joe Mar 8 '13 at 19:32
0

Unfortunately, there's no straightforward approach in Drupal for doing this. You'll have to get a little creative with validation callbacks in the form API and using AJAX (optionally).

I've just created an example of how to do this on my GitHub (found here).

Drupal has an example already of how to alter forms by adding and removing fieldsets, but I thought it wasn't featured enough. I needed the ability to remove specific items, which wasn't included in the example.

Edit: Unfortunately, I didn't have the time to divulge into creating an example with drag/drop re-ordering, but there are a couple examples of how to do drag/drop tables in a couple of the Drupal core modules (Text Formats, for example). I imagine it can't be too difficult to get these two concepts working with each other.

| improve this answer | |
0

I would suggest you to use field API to create your field item with multiple fields. Then you can add this item to your module by using form API. To see example just check core field items from

\drupal\core\lib\Drupal\Core\Field\Plugin\Field

They are very similar to Form API.

You can also create field item from console:

drupal generate:plugin:field

| improve this answer | |

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.