2

My install is using Inline Entity Forms and I would like it so that when a user visits the create node page, the inline entity form starts already visible but with all fields empty and ready to be created (no need to click "Add Node").

I would like to implement either a module or tpl file in my theme to achieve this. Please can you advise on how to go about it.

I've used this patch https://www.drupal.org/node/2225675 but it only works when editing existing content.

0

2 Answers 2

2

Once again thanks for your help on this user1455180. I managed to get it working by adding

$field = $context['field'];

to the beginning of the function you gave me and inserting the name of the field acting as the entity reference in the place of "FIELD_NAME".

function YOURMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
$field = $context['field'];
  if (isset($field['field_name']) && ('FIELD_NAME' == $field['field_name'])) {
    if (empty($element['form']) && empty($element['entities'][0])) {
      $element['actions'] = array();
      $field = $context['field'];
      $instance = $context['instance'];
      $settings = inline_entity_form_settings($field, $instance);
      $controller = inline_entity_form_get_controller($instance);

      $parents = array_merge($element['#field_parents'], array($element['#field_name'], $element['#language']));
      $parent_langcode = entity_language($element['#entity_type'], $element['#entity']);

      $ief_id = $element['#ief_id'];

      $element['form'] = array (
        '#type' => 'container',
        '#op' => 'add',
        '#parents' => array_merge($parents, array ('form')),
        '#entity_type' => $settings['entity_type'],
        '#parent_language' => $parent_langcode,
        '#ief_id' => $ief_id,
      );
      $bundle = reset($settings['bundles']);

      $form_state['inline_entity_form'][$ief_id]['form settings'] = array (
        'bundle' => $bundle,
      );

      $element['form'] = inline_entity_form_entity_form($controller, $element['form'], $form_state);
    }
  }
}
2

I read the codes in the inline_entity_form.module, and found the inline_entity_form_single is always displaying a form, so i copied its code to my module and it works:

function YOURMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  $field = $context['field'];
  if (isset($field['field_name']) && ('FIELD_NAME' == $field['field_name'])) {
    if (empty($element['form']) && empty($element['entities'][0])) {
      $element['actions'] = array();
      $instance = $context['instance'];
      $settings = inline_entity_form_settings($field, $instance);
      $controller = inline_entity_form_get_controller($instance);

      $parents = array_merge($element['#field_parents'], array($element['#field_name'], $element['#language']));
      $parent_langcode = entity_language($element['#entity_type'], $element['#entity']);

      $ief_id = $element['#ief_id'];

      $element['form'] = array (
        '#type' => 'container',
        '#op' => 'add',
        '#parents' => array_merge($parents, array ('form')),
        '#entity_type' => $settings['entity_type'],
        '#parent_language' => $parent_langcode,
        '#ief_id' => $ief_id,
      );
      $bundle = reset($settings['bundles']);

      $form_state['inline_entity_form'][$ief_id]['form settings'] = array (
        'bundle' => $bundle,
      );

      $element['form'] = inline_entity_form_entity_form($controller, $element['form'], $form_state);
    }
  }
}
4
  • Hi and thank you for the reply. I've used this code in an existing module I have and am not seeing any change (I made sure to change the function name for my module). I also tried changing FIELD_NAME to my fields name after it didn't work. However, no luck.. I perhaps should have mentioned that I am using the "Multiple Values" widget. Was there anything else you did to get this to work?
    – Nick
    May 29, 2015 at 20:17
  • Hi, i was using multiple values too. can you remove "&& empty($element['entities'][0])", clear the caches and try again? The codes I pasted above was just to insert a form for the current element if there is no form at all and no entities. Jun 1, 2015 at 6:24
  • I'm sure i must be doing something wrong as still no luck. I've even made a new install with only the required modules. I don't seem to be getting past this line : if (isset($field['field_name']) && ('FIELD_NAME' == $field['field_name'])) {
    – Nick
    Jun 1, 2015 at 14:10
  • I saw your latest comment, good to see it working! Thank you for correcting me. yes, $field = $context['field']; should be put in the beginning.. I mistakenly copied it inside "if" statement from my codes because i did a lot of other things previous from this part. Jun 2, 2015 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.