Is there any way to have field collections use an included field as the entity label?

My ultimate goal is to be able to reference field collections on a different content type, then let the two things share some information between eachother (for example, letting a content type "project" read a quantity field on a "publications" type that contains field collections).

Right now field collections DO work with entity reference, but there's no label set, so entityreference gives an unusable list of the collection name followed by a number.

IE:
collection 1
collection 2
collection 3
collection 1
collection 2
collection 3
collection 4

I'm not familiar with the entity API enough to make this change myself. If there's no way to do this, if you can point me to the right section of code I'll try my darndest to submit a patch. This is a GREAT module, it just has some rough patches integrating with other things.

Thanks,
Patrick

Files: 
CommentFileSizeAuthor
#17 field_collection-add-label-field-1617792-17.patch5.94 KBpurplezephyr
#15 field_collection-add-label-field-1617792-15.patch5.94 KBpurplezephyr
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-15.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#12 field_collection-add-label-field-1617792-12.patch4.64 KBpurplezephyr
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-12.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#9 field_collection-add-label-field-1617792-5.patch4.58 KBpurplezephyr
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-5.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
#4 field_collection-add-label-field-1617792-4.patch5.64 KBDean Reilly
PASSED: [[SimpleTest]]: [MySQL] 78 pass(es).
[ View ]
#3 field_collection-add-label-field-1617792-3.patch5.61 KBDean Reilly
PASSED: [[SimpleTest]]: [MySQL] 78 pass(es).
[ View ]
#2 features_extra-remove-site-specific-variables-from-nodequeues-1666548-2.patch917 bytesDean Reilly
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch features_extra-remove-site-specific-variables-from-nodequeues-1666548-2_0.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Comments

I'm having the exact same issue.
Anyone have a solution?

Version:7.x-1.0-beta4» 7.x-1.x-dev
Component:Miscellaneous» Code
Category:support» feature
StatusFileSize
new917 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch features_extra-remove-site-specific-variables-from-nodequeues-1666548-2_0.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

There is a noted todo in the code to allow users to customise the entity label. I ran into it recently and started to do some work on it. I've removed the custom label callback that's currently being used and added in a label field to the entity (similar to the node module). This allows the user to set an entity label when filling out the form.

This may not seem very helpful, but once it's set up this way you can use the Automatic Entity Label module to generate the label based on field values or host entity field values.

There's still some work to do though:

  • We should probably be using a proper field for the label rather than just a table column or we're going to run into translation problems.
  • This should be optional and if a field collection bundle doesn't use the label field the default label logic which this patch removes should be used. Look into how the node module handles this.
  • I've not tried to use automatic entity label's token support yet so that might still need some work.
  • Automatic entity label's form alter fails to remove the label field because the field collection form is embedded in a larger form and thus the fields are not where they're expected to be. I manually call the alter function on just the entity variable in order to achieve the same thing but maybe we should be running all form_alter hooks over this?
  • Still needs a lot of testing.

StatusFileSize
new5.61 KB
PASSED: [[SimpleTest]]: [MySQL] 78 pass(es).
[ View ]

Oops, wrong patch in that last comment. Try this one.

StatusFileSize
new5.64 KB
PASSED: [[SimpleTest]]: [MySQL] 78 pass(es).
[ View ]

The upgrade will fail with that last patch as I made a mistake with one of the variables. This patch corrects the problem.

Testing, this works well. Vote for commit.

Status:Needs review» Reviewed & tested by the community

Status:Reviewed & tested by the community» Needs work

+++ b/field_collection.installundefined
@@ -47,6 +54,14 @@ function field_collection_field_schema($field) {
+ * Add label column to field_collection_item table.

This should have {} around the table name, and start with "Adds"

+++ b/field_collection.installundefined
@@ -47,6 +54,14 @@ function field_collection_field_schema($field) {
+function field_collection_update_7001() {
+  $schema = field_collection_schema();
+  db_add_field('field_collection_item', 'label', $schema['field_collection_item']['fields']['label']);
+}

This whole function should be in the file after 7000

+++ b/field_collection.moduleundefined
@@ -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()) {

It's not clear to me, but why is this removed? Is the new label required now?

+++ b/field_collection.moduleundefined
@@ -1064,6 +1063,20 @@ function field_collection_field_widget_form(&$form, &$form_state, $field, $insta
+      if (module_exists('auto_entitylabel')) {
+        $element_state = array();
+        auto_entitylabel_form_alter($element, $element_state, '');

What is this? Can we have a comment about it?

+++ b/field_collection.moduleundefined
@@ -1287,13 +1300,19 @@ function field_collection_field_widget_embed_validate($element, &$form_state, $c
+  // Set label

Missing a trailing full stop.

Has there been any movement on this? I would definitely like the ability to override the name of my Field Collection Items and this seems like an easy way to go about it.

StatusFileSize
new4.58 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-5.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Fixed several bugs in this patch, it should work now.

-didn't handle missing label correctly
-didn't work with auto_entitylabel because auto_entitylabel_form_alter() was being called before this patch called it; I used a brute-force solution, but it's probably not the right way to do it
-there is already a field_collection_update_7001(); moved to 7003 and the end of the file

Status:Needs work» Needs review

Status:Needs review» Needs work

The last submitted patch, field_collection-add-label-field-1617792-5.patch, failed testing.

Status:Needs work» Needs review
StatusFileSize
new4.64 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-12.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Not sure why that failed, maybe because I left out the diff command? Resubmitting with the command, against the dev branch...

Status:Needs review» Needs work

The last submitted patch, field_collection-add-label-field-1617792-12.patch, failed testing.

Status:Needs work» Needs review

Meanwhile, here is an example of how to use auto_entitylabel and field_collection together, including the title of the containing node in the label of the field collection. Hopefully it will help anyone in the same situation. It does take a bit of tweaking for field names, entity types, etc.

<?php
  $label
= $entity->field_nic_name[$language][0]["value"];
 
// we get called twice when a new field collection is added;
  // the first time, the entity properties are different, so skip it
 
if (!property_exists($entity, "is_new") || !$entity->is_new) {
   
$query = new EntityFieldQuery();
   
$query
       
->entityCondition("entity_type", "node")
        ->
entityCondition("bundle", "server")
        ->
fieldCondition($entity->field_name, "value", $entity->item_id)
        ->
propertyOrderBy("created", "ASC");
   
$result = $query->execute();
    if (empty(
$result)) {
     
watchdog("auto_entitylabel / field_collection", "No containing entity found for field collection item #" . $entity->item_id . ".", NULL, WATCHDOG_ERROR);
    }
    else {
     
$nids = array_keys($result["node"]);
      if (
count($nids) < 1) {
       
watchdog("auto_entitylabel / field_collection", "No containing entity found for field collection item #" . $entity->item_id . ".", NULL, WATCHDOG_ERROR);
      }
      elseif (
count($nids) > 1) {
       
watchdog("auto_entitylabel / field_collection", "More than one containing entity found for field collection item #" . $entity->item_id . ".", NULL, WATCHDOG_ERROR);
      }
      else {
       
$e = entity_load("node", array($nids[0]));
       
$label = $e[$nids[0]]->title . "-" . $label;
      }
    }
  }
  print(
$label);
?>

StatusFileSize
new5.94 KB
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch field_collection-add-label-field-1617792-15.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

I think it needed to be in git format, trying again.

Also, an improvement to the the first line of that code sample:

  $label = (isset($entity->field_nic_name[$language][0]["value"]) ? $entity->field_nic_name[$language][0]["value"] : "");

Status:Needs review» Needs work

The last submitted patch, field_collection-add-label-field-1617792-15.patch, failed testing.

Still don't know what the issue was with the patch, but here is a version that bumps the database update number (because the latest dev version has a 7003 already).

an idea: expose title/label field so that Title module can leverage it
https://drupal.org/project/title