When a new node is being created we need a way to get the host entity.

I've created the following patch to address this.

CommentFileSizeAuthor
#1 field_collection_patch-2211667-1.patch621 bytesgmercer
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gmercer’s picture

Status: Active » Needs review
FileSize
621 bytes
jmuzz’s picture

Can you explain why this is necessary? It's already putting the field collection item itself in the host's field when the host is new. The code related to that is enough to get them saved together.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Directly below that change is this line:

field_attach_form('field_collection_item', $field_collection_item, $element, $form_state, $language);

That runs a whole host of code, not all of which is passed the $element (where the entity is stored).

For example, passing the field collection item to an 'allowed_values_function' without this patch will not allow the function to introspect the field collection's host entity.

Consider the following code:


/**
 * Returns the list of profile visibility options base on content type.
 * For publication we add the 'Featured' option
 */
function foo_get_allowed_profile_visibility($field, $instance, $entity_type, $entity, &$cacheable) {
  // This needs to be checked for every field.
  $cacheable = FALSE;

  // If this is a field collection item, use the bundle of the host entity.
  if ($entity instanceof FieldCollectionItemEntity) {
    $bundle = $entity->hostEntityBundle();
  }
  // Otherwise, extract it from the entity itself.
  elseif ($entity) {
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  }
  else {
    $bundle = '';
  }

  if ($bundle != 'publication') {
    return array(
      0 => 'Hidden',
      1 => 'Visible',
    );
  }
  else {
    return array(
      0 => 'Hidden',
      1 => 'Visible',
      2 => 'Featured',
    );
  }
}

  • Commit 10957df on 7.x-1.x authored by gmercer, committed by jmuzz:
    Issue #2211667 by gmercer: Attach host entity to new field collection...
jmuzz’s picture

Status: Reviewed & tested by the community » Fixed

Makes sense, thanks gmercer for the patch and tim.plunkett for laying that out for me.

I verified the patch is needed for the call to hostEntityBundle() to succeed in an allowed_values_function and committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.