diff -u field_collection.module.orig field_collection.module --- field_collection.module.orig 2013-04-12 04:09:52.000000000 -0400 +++ field_collection.module 2013-06-27 17:34:38.238515934 -0400 @@ -33,7 +33,6 @@ function field_collection_entity_info() { $return['field_collection_item'] = array( 'label' => t('Field collection item'), - 'label callback' => 'entity_class_label', 'uri callback' => 'entity_class_uri', 'entity class' => 'FieldCollectionItemEntity', 'controller class' => 'EntityAPIController', @@ -47,6 +46,7 @@ 'id' => 'item_id', 'revision' => 'revision_id', 'bundle' => 'field_name', + 'label' => 'label', ), 'module' => 'field_collection', 'view modes' => array( @@ -78,6 +78,27 @@ } /** + * Implements hook_field_extra_fields(). + */ +function field_collection_field_extra_fields() { + $extra = array(); + + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $extra['field_collection_item'][$field_name] = array( + 'form' => array( + 'label' => array( + 'label' => t('Label'), + 'description' => t('Field collection module element'), + 'weight' => -5, + ), + ), + ); + } + + return $extra; +} + +/** * Menu callback for loading the bundle names. */ function field_collection_field_name_load($arg) { @@ -252,28 +273,6 @@ } /** - * Specifies the default label, which is picked up by label() by default. - */ - public function defaultLabel() { - // @todo make configurable. - if ($this->fetchHostDetails()) { - $field = $this->fieldInfo(); - $label = $this->translatedInstanceLabel(); - - if ($field['cardinality'] == 1) { - return $label; - } - elseif ($this->item_id) { - return t('!instance_label @count', array('!instance_label' => $label, '@count' => $this->delta() + 1)); - } - else { - return t('New !instance_label', array('!instance_label' => $label)); - } - } - return t('Unconnected field collection item'); - } - - /** * Returns the path used to view the entity. */ public function path() { @@ -1397,6 +1396,34 @@ field_form_set_state($field_parents, $field_name, $language, $form_state, $field_state); field_attach_form('field_collection_item', $field_collection_item, $element, $form_state, $language); + $element['label'] = array( + '#type' => 'textfield', + '#title' => t('Label'), + '#required' => TRUE, + '#maxlength' => 255, + '#weight' => -5, + ); + + // there are cases where the label won't exist, which will cause + // an error unless we check first; + // see http://www.php.net/manual/en/function.property-exists.php#97538 + // about this code + if (isset($field_collection_item->label) || + property_exists($field_collection_item, 'label')) { + $element['label']['#default_value'] = $field_collection_item->label; + } + + if (module_exists('auto_entitylabel')) { + // auto_entitylabel_form_alter() has already been called, we need to + // force it to run again + // (I strongly suspect this means this code is Doing It Wrong, but + // at least this works) + $element['#auto_entitylabel_processed']=''; + + $element_state = array(); + auto_entitylabel_form_alter($element, $element_state, ''); + } + if (empty($element['#required'])) { $element['#after_build'][] = 'field_collection_field_widget_embed_delay_required_validation'; } @@ -1653,13 +1680,19 @@ $field_parents = $element['#field_parents']; $field_name = $element['#field_name']; $language = $element['#language']; + $delta = $element['#delta']; + $field_value_parents = array_merge($field_parents, array($field_name, $language, $delta)); + $field_value = drupal_array_get_nested_value($form_state['values'], $field_value_parents); $field_state = field_form_get_state($field_parents, $field_name, $language, $form_state); - $field_collection_item = $field_state['entity'][$element['#delta']]; + $field_collection_item = $field_state['entity'][$delta]; // Attach field API validation of the embedded form. field_attach_form_validate('field_collection_item', $field_collection_item, $element, $form_state); + // Set label. + $field_collection_item->label = $field_value['label']; + // Now validate required elements if the entity is not empty. if (!field_collection_item_is_empty($field_collection_item) && !empty($element['#field_collection_required_elements'])) { foreach ($element['#field_collection_required_elements'] as &$elements) {