Add another set of field collection to the $form in mymodule_form_alter

523 Views Asked by At

I am working on custom quiz module, where we have Multiple choice questions. We have created separate field collections for Question and it's options. Author can add unlimited number of question and it's answers.

While creating a new multiple choice quiz, Drupal renders a question with one option, but by default we want to render 2 options for one question. How should I add one set of field collection to another filed collection in form alter or is there any field collection configuration?

My scenario:

  1. Question (textarea)
    1. Option (textarea)

We want it to be:

  1. Question (textarea)
    1. Option (textarea)
    2. Option (textarea)

I have used below code to add field_collection to the form but while submitting the form the values are not submitted. In the node page I can't see the values.

module_load_include('inc', 'field_collection', 'field_collection.pages');
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_mc_options'));
field_attach_form('field_collection_item', $field_collection_item, $form['field_mc_questions']['und'][0]['field_mc_options'], $form_state);
1

There are 1 best solutions below

1
On

Did you hve a look at the Built-in Multiple Choice Question Module ? It Inherits from the QuizQuestion-Class which has a getNodeForm()-Method. You can override it when you Subclass QuizQuestion...