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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maxheight’s picture

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

Dean Reilly’s picture

Version: 7.x-1.0-beta4 » 7.x-1.x-dev
Component: Miscellaneous » Code
Category: support » feature
FileSize
917 bytes

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.
Dean Reilly’s picture

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

Dean Reilly’s picture

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

chrisschaub’s picture

Testing, this works well. Vote for commit.

chrisschaub’s picture

Status: Needs review » Reviewed & tested by the community
tim.plunkett’s picture

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.

kyleoliveira’s picture

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.

purplezephyr’s picture

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

purplezephyr’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

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

purplezephyr’s picture

Status: Needs work » Needs review
FileSize
4.64 KB

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.

purplezephyr’s picture

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);
?>
purplezephyr’s picture

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.

purplezephyr’s picture

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).

lpalgarvio’s picture

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

KeyboardCowboy’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
1.83 KB

I took a different approach with this and added a hook to allow modules to customize the label of an item without requiring extra database fields. Apply this patch then you can simple implement this hook in a manner like:

/**
 * Alter the label for a field collection.
 *
 * @param FieldCollectionItemEntity $item
 *   The field collection item object.
 * @param $host
 *   The host entity of the field collection item.
 * @param $field
 *   The field information about the item.
 *
 * @return $label
 *   A string to represent the label for this item type.
 */
function MYMODULE_field_collection_item_label($item, $host, $field) {
  switch ($item->field_name) {
    case 'field_my_first_collection':
      $item_wrapper = entity_metadata_wrapper('field_collection_item', $item);

      $title  = $item_wrapper->field_title->value();
      $author = $item_wrapper->field_author->value();

      return "{$title} by {$author}";
  }
}

meb’s picture

#19 works for me. The approach works well for my needs. Thanks!

kristofferwiklund’s picture

Status: Needs review » Reviewed & tested by the community

Works great. Thanks

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

  • jmuzz committed 0ef51d5 on 7.x-1.x authored by KeyboardCowboy
    Issue #1617792 by KeyboardCowboy: Support for entityreference / setting...
jmuzz’s picture

Status: Reviewed & tested by the community » Needs work

Thanks @KeyboardCowboy, #19 committed.

It would still be nice to have an option to do it with the admin interface. The other patch might work if it is made to diff the files with themselves instead of with the *.orig versions which won't exist on the test server.