Commit a6a4adc9 authored by alexpott's avatar alexpott

Issue #1981316 by aspilicious, andypost, pcambra, swentel: Drop procedural...

Issue #1981316 by aspilicious, andypost, pcambra, swentel: Drop procedural usage of fields in [f-l] modules.
parent db0e9117
...@@ -67,14 +67,15 @@ function getLastFileId() { ...@@ -67,14 +67,15 @@ function getLastFileId() {
* A list of widget settings that will be added to the widget defaults. * A list of widget settings that will be added to the widget defaults.
*/ */
function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) { function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
$field = array( $field_definition = array(
'field_name' => $name, 'field_name' => $name,
'type' => 'file', 'type' => 'file',
'settings' => array(), 'settings' => array(),
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1, 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
); );
$field['settings'] = array_merge($field['settings'], $field_settings); $field_definition['settings'] = array_merge($field_definition['settings'], $field_settings);
$field = field_create_field($field); $field = entity_create('field_entity', $field_definition);
$field->save();
$this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings); $this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
return $field; return $field;
...@@ -106,7 +107,7 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra ...@@ -106,7 +107,7 @@ function attachFileField($name, $entity_type, $bundle, $instance_settings = arra
'settings' => array(), 'settings' => array(),
); );
$instance['settings'] = array_merge($instance['settings'], $instance_settings); $instance['settings'] = array_merge($instance['settings'], $instance_settings);
field_create_instance($instance); entity_create('field_instance', $instance)->save();
entity_get_form_display($entity_type, $bundle, 'default') entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($name, array( ->setComponent($name, array(
...@@ -123,7 +124,7 @@ function updateFileField($name, $type_name, $instance_settings = array(), $widge ...@@ -123,7 +124,7 @@ function updateFileField($name, $type_name, $instance_settings = array(), $widge
$instance = field_info_instance('node', $name, $type_name); $instance = field_info_instance('node', $name, $type_name);
$instance['settings'] = array_merge($instance['settings'], $instance_settings); $instance['settings'] = array_merge($instance['settings'], $instance_settings);
field_update_instance($instance); $instance->save();
entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default') entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
->setComponent($instance['field_name'], array( ->setComponent($instance['field_name'], array(
...@@ -238,4 +239,5 @@ function assertFileIsPermanent(FileInterface $file, $message = NULL) { ...@@ -238,4 +239,5 @@ function assertFileIsPermanent(FileInterface $file, $message = NULL) {
$message = isset($message) ? $message : t('File %file is permanent.', array('%file' => $file->getFileUri())); $message = isset($message) ? $message : t('File %file is permanent.', array('%file' => $file->getFileUri()));
$this->assertTrue($file->isPermanent(), $message); $this->assertTrue($file->isPermanent(), $message);
} }
} }
...@@ -52,7 +52,7 @@ function testRequired() { ...@@ -52,7 +52,7 @@ function testRequired() {
$this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.')); $this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.'));
// Try again with a multiple value field. // Try again with a multiple value field.
field_delete_field($field_name); $field->delete();
$this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1')); $this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));
// Try to post a new node without uploading a file in the multivalue field. // Try to post a new node without uploading a file in the multivalue field.
...@@ -153,4 +153,5 @@ function testFileExtension() { ...@@ -153,4 +153,5 @@ function testFileExtension() {
$this->assertFileExists($node_file, t('File exists after uploading a file with extension checking.')); $this->assertFileExists($node_file, t('File exists after uploading a file with extension checking.'));
$this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.')); $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.'));
} }
} }
...@@ -44,18 +44,16 @@ public function setUp() { ...@@ -44,18 +44,16 @@ public function setUp() {
$this->installSchema('file', 'file_managed'); $this->installSchema('file', 'file_managed');
$this->installSchema('file', 'file_usage'); $this->installSchema('file', 'file_usage');
$field = array( entity_create('field_entity', array(
'field_name' => 'file_test', 'field_name' => 'file_test',
'type' => 'file', 'type' => 'file',
'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
); ))->save();
field_create_field($field); entity_create('field_instance', array(
$instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'file_test', 'field_name' => 'file_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($instance);
file_put_contents('public://example.txt', $this->randomName()); file_put_contents('public://example.txt', $this->randomName());
$this->file = entity_create('file', array( $this->file = entity_create('file', array(
'uri' => 'public://example.txt', 'uri' => 'public://example.txt',
......
...@@ -53,7 +53,7 @@ function forum_enable() { ...@@ -53,7 +53,7 @@ function forum_enable() {
// is being enabled at the same time as taxonomy after both modules have been // is being enabled at the same time as taxonomy after both modules have been
// enabled, the field might exist but still be marked inactive. // enabled, the field might exist but still be marked inactive.
if (!field_read_field('taxonomy_forums', array('include_inactive' => TRUE))) { if (!field_read_field('taxonomy_forums', array('include_inactive' => TRUE))) {
$field = array( entity_create('field_entity', array(
'field_name' => 'taxonomy_forums', 'field_name' => 'taxonomy_forums',
'type' => 'taxonomy_term_reference', 'type' => 'taxonomy_term_reference',
'settings' => array( 'settings' => array(
...@@ -64,8 +64,7 @@ function forum_enable() { ...@@ -64,8 +64,7 @@ function forum_enable() {
), ),
), ),
), ),
); ))->save();
field_create_field($field);
// Create a default forum so forum posts can be created. // Create a default forum so forum posts can be created.
$term = entity_create('taxonomy_term', array( $term = entity_create('taxonomy_term', array(
...@@ -78,14 +77,13 @@ function forum_enable() { ...@@ -78,14 +77,13 @@ function forum_enable() {
$term->save(); $term->save();
// Create the instance on the bundle. // Create the instance on the bundle.
$instance = array( entity_create('field_instance', array(
'field_name' => 'taxonomy_forums', 'field_name' => 'taxonomy_forums',
'entity_type' => 'node', 'entity_type' => 'node',
'label' => $vocabulary->name, 'label' => $vocabulary->name,
'bundle' => 'forum', 'bundle' => 'forum',
'required' => TRUE, 'required' => TRUE,
); ))->save();
field_create_instance($instance);
// Assign form display settings for the 'default' form mode. // Assign form display settings for the 'default' form mode.
entity_get_form_display('node', 'forum', 'default') entity_get_form_display('node', 'forum', 'default')
...@@ -124,7 +122,7 @@ function forum_uninstall() { ...@@ -124,7 +122,7 @@ function forum_uninstall() {
variable_del('node_options_forum'); variable_del('node_options_forum');
field_delete_field('taxonomy_forums'); field_info_field('taxonomy_forums')->delete();
// Purge field data now to allow taxonomy module to be uninstalled // Purge field data now to allow taxonomy module to be uninstalled
// if this is the only field remaining. // if this is the only field remaining.
field_purge_batch(10); field_purge_batch(10);
......
...@@ -78,49 +78,43 @@ function setUp() { ...@@ -78,49 +78,43 @@ function setUp() {
language_save($german); language_save($german);
// Create the test text field. // Create the test text field.
$field = array( entity_create('field_entity', array(
'field_name' => 'field_test_text', 'field_name' => 'field_test_text',
'type' => 'text', 'type' => 'text',
'translatable' => FALSE, 'translatable' => FALSE,
); ))->save();
field_create_field($field); entity_create('field_instance', array(
$instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'field_test_text', 'field_name' => 'field_test_text',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($instance);
// Create the test translatable field. // Create the test translatable field.
$field = array( entity_create('field_entity', array(
'field_name' => 'field_test_translatable_text', 'field_name' => 'field_test_translatable_text',
'type' => 'text', 'type' => 'text',
'translatable' => TRUE, 'translatable' => TRUE,
); ))->save();
field_create_field($field); entity_create('field_instance', array(
$instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'field_test_translatable_text', 'field_name' => 'field_test_translatable_text',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($instance);
// Create the test entity reference field. // Create the test entity reference field.
$field = array( entity_create('field_entity', array(
'translatable' => TRUE, 'translatable' => TRUE,
'settings' => array( 'settings' => array(
'target_type' => 'entity_test', 'target_type' => 'entity_test',
), ),
'field_name' => 'field_test_entity_reference', 'field_name' => 'field_test_entity_reference',
'type' => 'entity_reference', 'type' => 'entity_reference',
); ))->save();
field_create_field($field); entity_create('field_instance', array(
$instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'field_test_entity_reference', 'field_name' => 'field_test_entity_reference',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($instance);
// Set up the mock serializer. // Set up the mock serializer.
$normalizers = array( $normalizers = array(
......
...@@ -51,30 +51,28 @@ function testDefaultImages() { ...@@ -51,30 +51,28 @@ function testDefaultImages() {
$widget_settings = array( $widget_settings = array(
'preview_image_style' => 'medium', 'preview_image_style' => 'medium',
); );
$this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings); $instance = $this->createImageField($field_name, 'article', $field_settings, $instance_settings, $widget_settings);
$field = field_info_field($field_name); $field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, 'article');
// Add another instance with another default image to the page content type. // Add another instance with another default image to the page content type.
$instance2 = array( $instance2 = entity_create('field_instance', array(
'field_name' => $field['field_name'], 'field_name' => $field->id(),
'entity_type' => 'node', 'entity_type' => 'node',
'bundle' => 'page', 'bundle' => 'page',
'label' => $instance['label'], 'label' => $instance->label(),
'required' => $instance['required'], 'required' => $instance->required,
'settings' => array( 'settings' => array(
'default_image' => $default_images['instance2']->id(), 'default_image' => $default_images['instance2']->id(),
), ),
); ));
field_create_instance($instance2); $instance2->save();
$instance2 = field_info_instance('node', $field_name, 'page');
$widget_settings = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($field['field_name']); $widget_settings = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($field['field_name']);
entity_get_form_display('node', 'page', 'default') entity_get_form_display('node', 'page', 'default')
->setComponent($field['field_name'], $widget_settings) ->setComponent($field->id(), $widget_settings)
->save(); ->save();
entity_get_display('node', 'page', 'default') entity_get_display('node', 'page', 'default')
->setComponent($field['field_name']) ->setComponent($field->id())
->save(); ->save();
// Confirm the defaults are present on the article field settings form. // Confirm the defaults are present on the article field settings form.
...@@ -145,7 +143,7 @@ function testDefaultImages() { ...@@ -145,7 +143,7 @@ function testDefaultImages() {
// Upload a new default for the field. // Upload a new default for the field.
$field['settings']['default_image'] = array($default_images['field_new']->id()); $field['settings']['default_image'] = array($default_images['field_new']->id());
field_update_field($field); $field->save();
// Confirm that the new default is used on the article field settings form. // Confirm that the new default is used on the article field settings form.
$this->drupalGet("admin/structure/types/manage/article/fields/$instance->id/field"); $this->drupalGet("admin/structure/types/manage/article/fields/$instance->id/field");
...@@ -180,7 +178,7 @@ function testDefaultImages() { ...@@ -180,7 +178,7 @@ function testDefaultImages() {
// Upload a new default for the article's field instance. // Upload a new default for the article's field instance.
$instance['settings']['default_image'] = $default_images['instance_new']->id(); $instance['settings']['default_image'] = $default_images['instance_new']->id();
field_update_instance($instance); $instance->save();
// Confirm the new field instance default is used on the article field // Confirm the new field instance default is used on the article field
// admin form. // admin form.
...@@ -219,7 +217,7 @@ function testDefaultImages() { ...@@ -219,7 +217,7 @@ function testDefaultImages() {
// Remove the instance default from articles. // Remove the instance default from articles.
$instance['settings']['default_image'] = 0; $instance['settings']['default_image'] = 0;
field_update_instance($instance); $instance->save();
// Confirm the article field instance default has been removed. // Confirm the article field instance default has been removed.
$this->drupalGet("admin/structure/types/manage/article/fields/$instance->id"); $this->drupalGet("admin/structure/types/manage/article/fields/$instance->id");
...@@ -251,4 +249,5 @@ function testDefaultImages() { ...@@ -251,4 +249,5 @@ function testDefaultImages() {
) )
); );
} }
} }
...@@ -76,7 +76,7 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc ...@@ -76,7 +76,7 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1, 'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
); );
$field['settings'] = array_merge($field['settings'], $field_settings); $field['settings'] = array_merge($field['settings'], $field_settings);
field_create_field($field); entity_create('field_entity', $field)->save();
$instance = array( $instance = array(
'field_name' => $field['field_name'], 'field_name' => $field['field_name'],
...@@ -88,7 +88,8 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc ...@@ -88,7 +88,8 @@ function createImageField($name, $type_name, $field_settings = array(), $instanc
'settings' => array(), 'settings' => array(),
); );
$instance['settings'] = array_merge($instance['settings'], $instance_settings); $instance['settings'] = array_merge($instance['settings'], $instance_settings);
$field_instance = field_create_instance($instance); $field_instance = entity_create('field_instance', $instance);
$field_instance->save();
entity_get_form_display('node', $type_name, 'default') entity_get_form_display('node', $type_name, 'default')
->setComponent($field['field_name'], array( ->setComponent($field['field_name'], array(
...@@ -127,4 +128,5 @@ function uploadNodeImage($image, $field_name, $type) { ...@@ -127,4 +128,5 @@ function uploadNodeImage($image, $field_name, $type) {
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches); preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
return isset($matches[1]) ? $matches[1] : FALSE; return isset($matches[1]) ? $matches[1] : FALSE;
} }
} }
...@@ -43,18 +43,16 @@ public function setUp() { ...@@ -43,18 +43,16 @@ public function setUp() {
$this->installSchema('file', array('file_managed', 'file_usage')); $this->installSchema('file', array('file_managed', 'file_usage'));
$field = array( entity_create('field_entity', array(
'field_name' => 'image_test', 'field_name' => 'image_test',
'type' => 'image', 'type' => 'image',
'cardinality' => FIELD_CARDINALITY_UNLIMITED, 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
); ))->save();
field_create_field($field); entity_create('field_instance', array(
$instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'image_test', 'field_name' => 'image_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($instance);
file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg'); file_unmanaged_copy(DRUPAL_ROOT . '/core/misc/druplicon.png', 'public://example.jpg');
$this->image = entity_create('file', array( $this->image = entity_create('file', array(
'uri' => 'public://example.jpg', 'uri' => 'public://example.jpg',
......
...@@ -35,17 +35,15 @@ public function setUp() { ...@@ -35,17 +35,15 @@ public function setUp() {
parent::setUp(); parent::setUp();
// Create an link field and instance for validation. // Create an link field and instance for validation.
$this->field = array( entity_create('field_entity', array(
'field_name' => 'field_test', 'field_name' => 'field_test',
'type' => 'link', 'type' => 'link',
); ))->save();
field_create_field($this->field); entity_create('field_instance', array(
$this->instance = array(
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'field_test', 'field_name' => 'field_test',
'bundle' => 'entity_test', 'bundle' => 'entity_test',
); ))->save();
field_create_instance($this->instance);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment