From f56a7eca9cf8d0ae9512e578520a25acadb1bed8 Mon Sep 17 00:00:00 2001 From: Dean Reilly Date: Thu, 5 Jul 2012 20:06:13 +0100 Subject: [PATCH] Issue #1617792: Added a label field to field collection entities. --- field_collection.install | 15 ++++++++++ field_collection.module | 67 +++++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/field_collection.install b/field_collection.install index 28c5bfb..44facc0 100644 --- a/field_collection.install +++ b/field_collection.install @@ -24,6 +24,13 @@ function field_collection_schema() { 'length' => 32, 'not null' => TRUE, ), + 'label' => array( + 'description' => 'The label of this field collection item, always treated as non-markup plain text.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ), ), 'primary key' => array('item_id'), ); @@ -47,6 +54,14 @@ function field_collection_field_schema($field) { } /** + * Add label column to field_collection_item table. + */ +function field_collection_update_7001() { + $schema = field_collection_schema(); + db_add_field('field_collection_item', 'label', $schema['label']); +} + +/** * Update the administer field collection permission machine name. */ function field_collection_update_7000() { diff --git a/field_collection.module b/field_collection.module index 80e4588..cb1a96b 100644 --- a/field_collection.module +++ b/field_collection.module @@ -24,7 +24,6 @@ function field_collection_help($path, $arg) { 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', @@ -33,6 +32,7 @@ function field_collection_entity_info() { 'entity keys' => array( 'id' => 'item_id', 'bundle' => 'field_name', + 'label' => 'label', ), 'module' => 'field_collection', 'view modes' => array( @@ -64,6 +64,27 @@ function field_collection_entity_info() { } /** + * 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) { @@ -152,28 +173,6 @@ class FieldCollectionItemEntity extends Entity { } /** - * 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() { @@ -1064,6 +1063,20 @@ function field_collection_field_widget_form(&$form, &$form_state, $field, $insta field_attach_form('field_collection_item', $field_collection_item, $element, $form_state, $language); + $element['label'] = array( + '#type' => 'textfield', + '#title' => t('Label'), + '#required' => TRUE, + '#default_value' => $field_collection_item->label, + '#maxlength' => 255, + '#weight' => -5, + ); + + if (module_exists('auto_entitylabel')) { + $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'; } @@ -1287,13 +1300,19 @@ function field_collection_field_widget_embed_validate($element, &$form_state, $c $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) { -- 1.7.5.4