1

I want to create multiple nodes from one page where I input one or two values into each entity, where each of the entities has a reference.

Example: If I had three kids, I would measure their height each week, entity types "kid" and "height". Height references to kid and instead of click add "height" three times and select which kid then input height, I would like to have a page with the names (or ID) of the kids, then one input field for each, so that when I save, three entities of height are created with the values specified.

2
  • 1
    Hello and welcome. Please remember that simply providing specification is not enough, unless what you want seems to be something widely and universally useful. In specific cases like this, please do your search and research (see Help Center, Asking section for details) and ask when you will hit specific problem with implementation.
    – Mołot
    Dec 6 '14 at 2:00
  • Thanks Molot. I have done my search and research as best as I can, but I am still unable to find the solutions. I would assume there was a modue for this particular prblem, and I'm quite certain I stumbled upon a way to solve it long tme ago.
    – HansP
    Dec 6 '14 at 15:07
0

There are a couple of modules to create entities (nodes etc) inline i.e in the parent form, when it's being created itself.

When you save the form, it creates the entity AND the entities that are referencing it, but it's up to you to create the entity structure and references to achieve what you want.

I can vouch for References Dialog

However, I think you are making it too complex and you only really want one node type (kids) with an unlimited field (height), if that is your real scenario.

For example you easily create a custom form with three (or loop for x) fields and when it saves it adds the result to existing nodes, eg :

// Get and render the form somewhere.
$form = drupal_get_form('kids_height_form');
print render($form);

/**
 * Form constructor.
 */
function kids_height_form() {
  $kids = array(2210, 2043, 2038);
  $entities = entity_load('node', $kids);
  // dpm($entities);
  foreach ($entities as $kid) {
    $id = $kid->nid;
    $form["height_$id"] = array(
      '#title' => 'Height this week for $id',
      '#type'  => 'textfield',
    );
  }
  $form['submit'] = array(
    '#type'  => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

/**
 * Form submit handler for kids_height_form().
 */
function kids_height_form_submit($form, &$form_state) {
  $kids = array(1, 2, 3);
  $entities = entity_load('node', $kids);
  foreach ($entities as $kid) {
    $id     = $kid->entity_id;
    $height = $form_state['values']["height_$id"];
    $count  = count($kid->field_height[LANGUAGE_NONE]);
    $kid->field_height[LANGUAGE_NONE][$count + 1] = array('value' => $height);
    entity_save('node', $kid);
  }
}

The top bit prints the form, the form constructor simply create as many text fields as there are node ids in the array and the submit handler does the same loop except also adds the result to a new delta for the height field and saves the entity.

You can also achieve this with EntityMetaWrappers and may be a bit neater but the process is very similar. (this is untested by the way and will need work to suit your needs);

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.