Certain fields cannot be attached to certain entities.

For instance "comment_body" field can only be attached to comments.

If you try to clone "comment body" to another entity you will get the fatal error something like

FieldException: Attempt to create an instance of field comment_body on forbidden entity type entityform. in field_create_instance()

I will provide a patch to fix this

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tedbow’s picture

Status: Active » Needs review
FileSize
1.54 KB

Attaching patch

Creates helper function "_field_tools_entity_can_attach_field"

This function was also added here #1604704: Clone fields from various bundles into 1 bundle

joachim’s picture

Status: Needs review » Needs work

Good point; I never thought of that!

Patch looks good, though could you run it through Coder module please to fix docblock formatting, constants, and whitespace please?

Also:

+++ b/field_tools.module
@@ -95,3 +95,16 @@ function field_tools_menu() {
+ * @param string|array $field_info

Let's just say this always has to be a $field_info. It's cleaner to not have mixed purpose params, and it's no great burden on a caller to have to load the field info first; callers will usually have it.

tedbow’s picture

Status: Needs work » Needs review
FileSize
2.76 KB

Ok, ran through and fixed whitespace errors

joachim’s picture

Status: Needs review » Fixed

Your docblock still wasn't laid out quite right; I've committed it with a bit of tidying up.

For reference, this is how docblocks should be laid out (at least last time I checked coding standards... which do change!)

/**
 * Helper function to determine whether a field can be attached an entity type.
 *
 * Certain fields like comment_body are restricted to certain entity types.
 *
 * @param string $entity_type
 *  The entity type to check.
 * @param array $field_info
 *  The field info to check.
 *
 * @return boolean
 *  Whether the field can be attached to the entity type.
 */

- #1604768 by tedbow: Fixed lack of check that fields aren't restricted to certain entity types.

Thanks again for the patch!

Status: Fixed » Closed (fixed)

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