Commit 22fc66d5 authored by alexpott's avatar alexpott

Issue #2277941 by yched: Allow injecting an arbitrary FieldConfig when...

Issue #2277941 by yched: Allow injecting an arbitrary FieldConfig when building a FieldInstanceConfig.
parent bfd7420c
......@@ -89,8 +89,7 @@ function custom_block_add_body_field($block_type_id, $label = 'Body') {
}
if (empty($instance)) {
$instance = entity_create('field_instance_config', array(
'field_name' => 'body',
'entity_type' => 'custom_block',
'field' => $field,
'bundle' => $block_type_id,
'label' => $label,
'settings' => array('display_summary' => FALSE),
......
......@@ -72,8 +72,7 @@ public function testBlockFields() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $this->field->getName(),
'entity_type' => 'custom_block',
'field' => $this->field,
'bundle' => 'link',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
......
......@@ -79,8 +79,7 @@ public function testExportImport() {
));
$this->field->save();
entity_create('field_instance_config', array(
'field_name' => $this->field->name,
'entity_type' => 'node',
'field' => $this->field,
'bundle' => $this->content_type->type,
))->save();
entity_get_form_display('node', $this->content_type->type, 'default')
......
......@@ -53,8 +53,7 @@ protected function setUp() {
))->save();
entity_create('field_instance_config', array(
'field_name' => $this->field->name,
'entity_type' => 'contact_message',
'field' => $this->field,
'bundle' => 'contact_message',
))->save();
......
......@@ -66,8 +66,7 @@ function setUp() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $this->field->name,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
'required' => TRUE,
));
......@@ -305,8 +304,7 @@ function testDefaultValue() {
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => $field->name,
'entity_type' => 'node',
'field' => $field,
'bundle' => 'date_content',
));
$instance->save();
......
......@@ -43,8 +43,7 @@ public function setUp() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $this->field->getName(),
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
'settings' => array(
'default_value' => 'blank',
......
......@@ -153,8 +153,7 @@ public function testFieldComponent() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'entity_test',
));
$instance->save();
......@@ -328,8 +327,7 @@ public function testDeleteFieldInstance() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'entity_test',
));
$instance->save();
......
......@@ -64,8 +64,7 @@ public function testFieldComponent() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'entity_test',
));
$instance->save();
......@@ -192,8 +191,7 @@ public function testDeleteFieldInstance() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'entity_test',
));
$instance->save();
......
......@@ -56,8 +56,7 @@ function testEntityReferenceDefaultValue() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $this->field->name,
'entity_type' => 'node',
'field' => $this->field,
'bundle' => 'reference_content',
'settings' => array(
'handler' => 'default',
......
......@@ -74,8 +74,7 @@ public function testNodeHandler() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'test_field',
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'test_bundle',
'settings' => array(
'handler' => 'default',
......@@ -217,8 +216,7 @@ public function testUserHandler() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'test_field',
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'test_bundle',
'settings' => array(
'handler' => 'default',
......@@ -363,8 +361,7 @@ public function testCommentHandler() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'test_field',
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'test_bundle',
'settings' => array(
'handler' => 'default',
......
......@@ -66,7 +66,7 @@ public function testSort() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'test_field',
'field' => $field,
'entity_type' => 'entity_test',
'bundle' => 'test_bundle',
'settings' => array(
......
......@@ -52,8 +52,7 @@ public function testSelectionHandler() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'test_field',
'entity_type' => 'entity_test',
'field' => $field,
'bundle' => 'test_bundle',
'settings' => array(
'handler' => 'views',
......
......@@ -211,36 +211,47 @@ class FieldInstanceConfig extends ConfigEntityBase implements FieldInstanceConfi
/**
* Constructs a FieldInstanceConfig object.
*
* @param array $values
* An array of field instance properties, keyed by property name. Most
* array elements will be used to set the corresponding properties on the
* class; see the class property documentation for details. Some array
* elements have special meanings and a few are required; these special
* elements are:
* - field_name: The name of the field this is an instance of. This only
* supports non-deleted fields.
* - field_uuid: (optional) The uuid of the field this is an instance of.
* If present and the instance is marked as 'deleted', this has priority
* over the 'field_name' value for retrieving the related field.
* - entity_type: required.
* - bundle: required.
*
* In most cases, Field instance entities are created via
* entity_create('field_instance_config', $values), where $values is the same
* parameter as in this constructor.
*
* @param array $values
* An array of field instance properties, keyed by property name. The field
* this is an instance of can be specified either with:
* - field: the FieldConfigInterface object,
* or by referring to an existing field in the current configuration with:
* - field_name: The field name.
* - entity_type: The entity type.
* Additionally, a 'bundle' property is required to indicate the entity
* bundle to which the instance is attached to. Other array elements will be
* used to set the corresponding properties on the class; see the class
* property documentation for details.
*
* @see entity_create()
*
* @ingroup field_crud
*/
public function __construct(array $values, $entity_type = 'field_instance_config') {
// Check required properties.
if (empty($values['field_name'])) {
throw new FieldException('Attempt to create an instance of a field without a field_name.');
// Allow either an injected FieldConfig object, or a field_name and
// entity_type.
if (isset($values['field'])) {
if (!$values['field'] instanceof FieldConfigInterface) {
throw new FieldException('Attempt to create a configurable instance of a non-configurable field.');
}
$field = $values['field'];
$values['field_name'] = $field->getName();
$values['entity_type'] = $field->getTargetEntityTypeId();
$this->field = $field;
}
if (empty($values['entity_type'])) {
throw new FieldException(String::format('Attempt to create an instance of field @field_name without an entity_type.', array('@field_name' => $values['field_name'])));
else {
if (empty($values['field_name'])) {
throw new FieldException('Attempt to create an instance of a field without a field_name.');
}
if (empty($values['entity_type'])) {
throw new FieldException(String::format('Attempt to create an instance of field @field_name without an entity_type.', array('@field_name' => $values['field_name'])));
}
}
// 'bundle' is required in either case.
if (empty($values['bundle'])) {
throw new FieldException(String::format('Attempt to create an instance of field @field_name without a bundle.', array('@field_name' => $values['field_name'])));
}
......@@ -471,31 +482,14 @@ public static function postDelete(EntityStorageInterface $storage, array $instan
*/
public function getField() {
if (!$this->field) {
// Load the corresponding field.
// - If the instance is deleted (case of field purge), load the field
// based on the UUID.
// - Otherwise (regular case), fetch the field from the EntityManager
// registry.
if (!empty($this->deleted)) {
if ($fields = entity_load_multiple_by_properties('field_config', array('uuid' => $this->field_uuid, 'include_deleted' => TRUE))) {
$field = current($fields);
}
else {
throw new FieldException(String::format('Attempt to create an instance of field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
}
$fields = \Drupal::entityManager()->getFieldStorageDefinitions($this->entity_type);
if (!isset($fields[$this->field_name])) {
throw new FieldException(String::format('Attempt to create an instance of field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
}
else {
$fields = \Drupal::entityManager()->getFieldStorageDefinitions($this->entity_type);
if (!isset($fields[$this->field_name])) {
throw new FieldException(String::format('Attempt to create an instance of field @field_name that does not exist on entity type @entity_type.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
}
if (!$fields[$this->field_name] instanceof FieldConfigInterface) {
throw new FieldException(String::format('Attempt to create a configurable instance of non-configurable field @field_name.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
}
$field = $fields[$this->field_name];
if (!$fields[$this->field_name] instanceof FieldConfigInterface) {
throw new FieldException(String::format('Attempt to create a configurable instance of non-configurable field @field_name.', array('@field_name' => $this->field_name, '@entity_type' => $this->entity_type)));
}
$this->field = $field;
$this->field = $fields[$this->field_name];
}
return $this->field;
......
......@@ -101,18 +101,18 @@ public function loadByProperties(array $conditions = array()) {
if (isset($conditions['entity_type']) && isset($conditions['field_name'])) {
// Optimize for the most frequent case where we do have a specific ID.
$id = $conditions['entity_type'] . $conditions['field_name'];
$fields = $this->entityManager->getStorage($this->entityTypeId)->loadMultiple(array($id));
$fields = $this->loadMultiple(array($id));
}
else {
// No specific ID, we need to examine all existing fields.
$fields = $this->entityManager->getStorage($this->entityTypeId)->loadMultiple();
$fields = $this->loadMultiple();
}
// Merge deleted fields (stored in state) if needed.
if ($include_deleted) {
$deleted_fields = $this->state->get('field.field.deleted') ?: array();
foreach ($deleted_fields as $id => $config) {
$fields[$id] = $this->entityManager->getStorage($this->entityTypeId)->create($config);
$fields[$id] = $this->create($config);
}
}
......
......@@ -108,18 +108,23 @@ public function loadByProperties(array $conditions = array()) {
if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
// Optimize for the most frequent case where we do have a specific ID.
$id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
$instances = $this->entityManager->getStorage($this->entityTypeId)->loadMultiple(array($id));
$instances = $this->loadMultiple(array($id));
}
else {
// No specific ID, we need to examine all existing instances.
$instances = $this->entityManager->getStorage($this->entityTypeId)->loadMultiple();
$instances = $this->loadMultiple();
}
// Merge deleted instances (stored in state) if needed.
if ($include_deleted) {
$deleted_instances = $this->state->get('field.instance.deleted') ?: array();
$deleted_fields = $this->state->get('field.field.deleted') ?: array();
foreach ($deleted_instances as $id => $config) {
$instances[$id] = $this->entityManager->getStorage($this->entityTypeId)->create($config);
// If the field itself is deleted, inject it directly in the instance.
if (isset($deleted_fields[$config['field_uuid']])) {
$config['field'] = $this->entityManager->getStorage('field_config')->create($deleted_fields[$config['field_uuid']]);
}
$instances[$id] = $this->create($config);
}
}
......
......@@ -136,8 +136,7 @@ function setUp() {
foreach ($this->bundles as $bundle) {
foreach ($this->fields as $field) {
entity_create('field_instance_config', array(
'field_name' => $field->name,
'entity_type' => $this->entity_type,
'field' => $field,
'bundle' => $bundle,
))->save();
}
......
......@@ -396,7 +396,7 @@ function testUpdateField() {
));
$field->save();
$instance = entity_create('field_instance_config', array(
'field_name' => 'field_update',
'field' => $field,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
));
......
......@@ -67,8 +67,7 @@ function testEmailField() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
));
$this->instance->save();
......
......@@ -40,8 +40,7 @@ public function testEntityCountAndHasData() {
));
$field->save();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_int',
'field' => $field,
'bundle' => 'entity_test',
))->save();
......
......@@ -54,8 +54,7 @@ public function testImportDeleteUninstall() {
$unrelated_field->save();
$unrelated_field_uuid = $unrelated_field->uuid();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_int',
'field' => $unrelated_field,
'bundle' => 'entity_test',
))->save();
......@@ -68,8 +67,7 @@ public function testImportDeleteUninstall() {
$field->save();
$field_uuid = $field->uuid();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'field' => $field,
'bundle' => 'entity_test',
))->save();
......@@ -131,8 +129,7 @@ public function testImportAlreadyDeletedUninstall() {
$field->save();
$field_uuid = $field->uuid();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'field' => $field,
'bundle' => 'entity_test',
))->save();
......
......@@ -51,8 +51,7 @@ public function testImportDeleteUninstall() {
$field->save();
$tel_field_uuid = $field->uuid();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_tel',
'field' => $field,
'bundle' => 'entity_test',
))->save();
......@@ -65,8 +64,7 @@ public function testImportDeleteUninstall() {
$text_field->save();
$text_field_uuid = $field->uuid();
entity_create('field_instance_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_text',
'field' => $text_field,
'bundle' => 'entity_test',
))->save();
......
......@@ -77,8 +77,7 @@ function createFieldWithInstance($suffix = '', $entity_type = 'entity_test', $bu
$this->$field->save();
$this->$field_id = $this->{$field}->uuid();
$this->$instance_definition = array(
'field_name' => $this->$field_name,
'entity_type' => $entity_type,
'field' => $this->$field,
'bundle' => $bundle,
'label' => $this->randomName() . '_label',
'description' => $this->randomName() . '_description',
......
......@@ -96,8 +96,7 @@ function setUp() {
$this->field->save();
$this->instance_definition = array(
'field_name' => $this->field_name,
'entity_type' => $this->entity_type,
'field' => $this->field,
'bundle' => 'entity_test',
);
$this->instance = entity_create('field_instance_config', $this->instance_definition);
......@@ -149,10 +148,11 @@ function testTranslatableFieldSaveLoad() {
$field_name_default = drupal_strtolower($this->randomName() . '_field_name');
$field_definition = $this->field_definition;
$field_definition['name'] = $field_name_default;
entity_create('field_config', $field_definition)->save();
$field = entity_create('field_config', $field_definition);
$field->save();
$instance_definition = $this->instance_definition;
$instance_definition['field_name'] = $field_name_default;
$instance_definition['field'] = $field;
$instance_definition['default_value'] = array(array('value' => rand(1, 127)));
$instance = entity_create('field_instance_config', $instance_definition);
$instance->save();
......
......@@ -73,8 +73,7 @@ function setUp() {
$this->field = entity_load('field_config', $this->entity_type . '.' . $this->field_name);
$instance = array(
'field_name' => $this->field_name,
'entity_type' => $this->entity_type,
'field' => $this->field,
'bundle' => $this->entity_type,
);
entity_create('field_instance_config', $instance)->save();
......
......@@ -81,8 +81,7 @@ function setUpFields($amount = 3) {
function setUpInstances($bundle = 'page') {
foreach ($this->fields as $key => $field) {
$instance = array(
'field_name' => $field->getName(),
'entity_type' => 'node',
'field' => $field,
'bundle' => 'page',
);
$this->instances[$key] = entity_create('field_instance_config', $instance);
......
......@@ -56,10 +56,9 @@ function testReEnabledField() {
));
$field->save();
entity_create('field_instance_config', array(
'field_name' => 'field_telephone',
'label' => 'Telephone Number',
'entity_type' => 'node',
'field' => $field,
'bundle' => 'article',
'label' => 'Telephone Number',
))->save();
entity_get_form_display('node', 'article', 'default')
......
......@@ -425,8 +425,7 @@ function testLockedField() {
));
$field->save();
entity_create('field_instance_config', array(
'field_name' => $field->name,
'entity_type' => 'node',
'field' => $field,
'bundle' => $this->type,
))->save();
entity_get_form_display('node', $this->type, 'default')
......
......@@ -75,8 +75,7 @@ function testURLValidation() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_DISABLED,
......@@ -200,8 +199,7 @@ function testLinkTitle() {
));
$this->field->save();
$this->instance = entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
'label' => 'Read more about this entity',
'settings' => array(
......@@ -320,8 +318,7 @@ function testLinkFormatter() {
));
$this->field->save();
entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $this->field,
'label' => 'Read more about this entity',
'bundle' => 'entity_test',
'settings' => array(
......@@ -463,8 +460,7 @@ function testLinkSeparateFormatter() {
));
$this->field->save();
entity_create('field_instance_config', array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
'settings' => array(
'title' => DRUPAL_OPTIONAL,
......
......@@ -391,8 +391,7 @@ function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
}
if (empty($instance)) {
$instance = entity_create('field_instance_config', array(
'field_name' => 'body',
'entity_type' => 'node',
'field' => $field,
'bundle' => $type->id(),
'label' => $label,
'settings' => array('display_summary' => TRUE),
......
......@@ -68,8 +68,7 @@ public function setUp() {
$field_private->save();
entity_create('field_instance_config', array(
'field_name' => $field_private->name,
'entity_type' => 'node',
'field' => $field_private,
'bundle' => 'page',
'widget' => array(
'type' => 'options_buttons',
......
......@@ -61,8 +61,7 @@ public function setUp() {
$field_private->save();
entity_create('field_instance_config', array(
'field_name' => $field_private->name,
'entity_type' => 'node',
'field' => $field_private,
'bundle' => 'page',
'widget' => array(
'type' => 'options_buttons',
......
......@@ -70,8 +70,7 @@ public function setUp() {
$this->field->save();
$instance = array(
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'field' => $this->field,
'bundle' => 'entity_test',
);
$this->instance = entity_create('field_instance_config', $instance);
......
......@@ -127,8 +127,7 @@ function setUp() {
function testRadioButtons() {
// Create an instance of the 'single value' field.
$instance = entity_create('field_instance_config', array(
'field_name' => $this->card_1->getName(),
'entity_type' => 'entity_test',
'field' => $this->card_1,
'bundle' => 'entity_test',
));
$instance->save();
......@@ -185,8 +184,7 @@ function testRadioButtons() {
function testCheckBoxes() {
// Create an instance of the 'multiple values' field.
$instance = entity_create('field_instance_config', array(
'field_name' => $this->card_2->getName(),