summaryrefslogtreecommitdiff
path: root/modules/field
diff options
context:
space:
mode:
Diffstat (limited to 'modules/field')
-rw-r--r--modules/field/field.crud.inc11
-rw-r--r--modules/field/tests/field.test34
2 files changed, 44 insertions, 1 deletions
diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc
index 7b502943f..e8a48008b 100644
--- a/modules/field/field.crud.inc
+++ b/modules/field/field.crud.inc
@@ -45,6 +45,9 @@
* - type (string)
* The type of the field, such as 'text' or 'image'. Field types
* are defined by modules that implement hook_field_info().
+ * - object_types (array)
+ * The array of entity types that can hold instances of this field. If
+ * empty or not specified, the field can have instances in any entity type.
* - cardinality (integer)
* The number of values the field can hold. Legal values are any
* positive integer or FIELD_CARDINALITY_UNLIMITED.
@@ -265,6 +268,7 @@ function field_create_field($field) {
}
$field += array(
+ 'object_types' => array(),
'cardinality' => 1,
'translatable' => FALSE,
'locked' => FALSE,
@@ -396,6 +400,9 @@ function field_update_field($field) {
if ($field['type'] != $prior_field['type']) {
throw new FieldException("Cannot change an existing field's type.");
}
+ if ($field['object_types'] != $prior_field['object_types']) {
+ throw new FieldException("Cannot change an existing field's object_types property.");
+ }
if ($field['storage']['type'] != $prior_field['storage']['type']) {
throw new FieldException("Cannot change an existing field's storage type.");
}
@@ -611,6 +618,10 @@ function field_create_instance($instance) {
if (empty($instance['bundle'])) {
throw new FieldException(t('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $instance['field_name'])));
}
+ // Check that the field can be attached to this object type.
+ if (!empty($field['object_types']) && !in_array($instance['object_type'], $field['object_types'])) {
+ throw new FieldException(t('Attempt to create an instance of field @field_name on forbidden object type @obj_type.', array('@field_name' => $instance['field_name'], '@obj_type' => $instance['object_type'])));
+ }
// Set the field id.
$instance['field_id'] = $field['id'];
diff --git a/modules/field/tests/field.test b/modules/field/tests/field.test
index 3052af9ad..94c94afd7 100644
--- a/modules/field/tests/field.test
+++ b/modules/field/tests/field.test
@@ -2297,7 +2297,6 @@ class FieldInstanceCrudTestCase extends FieldTestCase {
$this->instance_definition = array(
'field_name' => $this->field['field_name'],
'object_type' => 'test_entity',
- 'object_type' => 'test_entity',
'bundle' => 'test_bundle',
);
}
@@ -2355,6 +2354,39 @@ class FieldInstanceCrudTestCase extends FieldTestCase {
$this->pass(t('Cannot create an instance of a non-existing field.'));
}
+ // Create a field restricted to a specific entity type.
+ $field_restricted = array(
+ 'field_name' => drupal_strtolower($this->randomName()),
+ 'type' => 'test_field',
+ 'object_types' => array('test_cacheable_entity'),
+ );
+ field_create_field($field_restricted);
+
+ // Check that an instance can be added to an object type allowed
+ // by the field.
+ try {
+ $instance = $this->instance_definition;
+ $instance['field_name'] = $field_restricted['field_name'];
+ $instance['object_type'] = 'test_cacheable_entity';
+ field_create_instance($instance);
+ $this->pass(t('Can create an instance on a object type allowed by the field.'));
+ }
+ catch (FieldException $e) {
+ $this->fail(t('Can create an instance on a object type allowed by the field.'));
+ }
+
+ // Check that an instance cannot be added to an object type
+ // forbidden by the field.
+ try {
+ $instance = $this->instance_definition;
+ $instance['field_name'] = $field_restricted['field_name'];
+ field_create_instance($instance);
+ $this->fail(t('Cannot create an instance on a object type forbidden by the field.'));
+ }
+ catch (FieldException $e) {
+ $this->pass(t('Cannot create an instance on a object type forbidden by the field.'));
+ }
+
// TODO: test other failures.
}