From 19dd83859a98f6a040279ea5b65f03dd6db04f63 Mon Sep 17 00:00:00 2001 From: James Silver Date: Mon, 18 Mar 2013 23:46:22 +0000 Subject: [PATCH] Issue #1946474 by jamsilver: Fixed Broken 'Add' links may get rendered for brand-new entities. --- field_collection.module | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-) diff --git a/field_collection.module b/field_collection.module index bf6a005..16edcc3 100644 --- a/field_collection.module +++ b/field_collection.module @@ -1255,6 +1255,18 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i function field_collection_field_formatter_links(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) { $settings = $display['settings']; + $element += array('#prefix' => '', '#suffix' => ''); + + // In specific configurations this formatter may be rendered on an entity add + // form, before it has been saved. As such it is as of yet impossible for us + // to create field collection items. + $info = entity_get_info($entity_type); + $is_new = !isset($entity->{$info['entity keys']['id']}); + if ($is_new) { + $element['#suffix'] .= '' . t('This content must be saved before field values may be entered here.') . ''; + return; + } + if ($settings['add'] && ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || count($items) < $field['cardinality'])) { // Check whether the current is allowed to create a new item. $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name'])); @@ -1280,7 +1292,6 @@ function field_collection_field_formatter_links(&$element, $entity_type, $entity $element[$index]['#attributes']['class'][] = 'field-collection-view-final'; } - $element += array('#prefix' => '', '#suffix' => ''); $element['#prefix'] .= '
'; $element['#suffix'] .= '
'; -- 1.7.8.1