diff --git a/modules/entity_form/entity_browser_entity_form.module b/modules/entity_form/entity_browser_entity_form.module index 4fe1dec..7e13d95 100644 --- a/modules/entity_form/entity_browser_entity_form.module +++ b/modules/entity_form/entity_browser_entity_form.module @@ -26,11 +26,29 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re 'instance', ]); + $entity_form_display_storage = \Drupal::entityTypeManager()->getStorage('entity_form_display'); + $target_entity_type_id = $instance->getTargetEntityTypeId(); + $target_bundle = $instance->getTargetBundle() ?: $target_entity_type_id; + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ - $entity_form_id = $instance->getTargetEntityTypeId() . '.' . $instance->getTargetBundle() . '.default'; + $entity_form_id = $target_entity_type_id . '.' . $target_bundle . '.default'; // TODO - 'default' might become configurable or something else in the future. // See https://www.drupal.org/node/2510274 - $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load($entity_form_id); + $form_display = $entity_form_display_storage->load($entity_form_id); + + // If not found, create a fresh entity object. We do not preemptively create + // new entity form display configuration entries for each existing entity type + // and bundle whenever a new form mode becomes available. Instead, + // configuration entries are only created when an entity form display is + // explicitly configured and saved. + if (!$form_display) { + $form_display = $entity_form_display_storage->create(array( + 'targetEntityType' => $target_entity_type_id, + 'bundle' => $target_bundle, + 'mode' => 'default', + 'status' => TRUE, + )); + } $widget = $form_display->getRenderer($instance->getName()); @@ -47,8 +65,8 @@ function entity_browser_entity_form_inline_entity_form_reference_form_alter(&$re ])); $cardinality = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED; - if($instance->getFieldStorageDefinition()->get('cardinality') !== $cardinality) { - $cardinality = $instance->getFieldStorageDefinition()->get('cardinality') - $count_existing_selection; + if ($instance->getFieldStorageDefinition()->getCardinality() !== $cardinality) { + $cardinality = $instance->getFieldStorageDefinition()->getCardinality() - $count_existing_selection; } unset($reference_form['entity_id']);