Issue #2782309 by samuel.mortenson, martin107: Refactor File and Image related image field creation logic into a new trait

8.3.x
Alex Pott 2016-10-03 14:01:46 +01:00
parent dd6d586818
commit 66741e368b
2 changed files with 73 additions and 58 deletions

View File

@ -2,9 +2,8 @@
namespace Drupal\image\Tests;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
/**
* TODO: Test the following functions.
@ -24,6 +23,8 @@ use Drupal\field\Entity\FieldStorageConfig;
*/
abstract class ImageFieldTestBase extends WebTestBase {
use ImageFieldCreationTrait;
/**
* Modules to enable.
*
@ -51,62 +52,6 @@ abstract class ImageFieldTestBase extends WebTestBase {
$this->drupalLogin($this->adminUser);
}
/**
* Create a new image field.
*
* @param string $name
* The name of the new field (all lowercase), exclude the "field_" prefix.
* @param string $type_name
* The node type that this field will be added to.
* @param array $storage_settings
* A list of field storage settings that will be added to the defaults.
* @param array $field_settings
* A list of instance settings that will be added to the instance defaults.
* @param array $widget_settings
* Widget settings to be added to the widget defaults.
* @param array $formatter_settings
* Formatter settings to be added to the formatter defaults.
* @param string $description
* A description for the field.
*/
function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array(), $formatter_settings = array(), $description = '') {
FieldStorageConfig::create(array(
'field_name' => $name,
'entity_type' => 'node',
'type' => 'image',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
))->save();
$field_config = FieldConfig::create([
'field_name' => $name,
'label' => $name,
'entity_type' => 'node',
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
'description' => $description,
]);
$field_config->save();
entity_get_form_display('node', $type_name, 'default')
->setComponent($name, array(
'type' => 'image_image',
'settings' => $widget_settings,
))
->save();
entity_get_display('node', $type_name, 'default')
->setComponent($name, array(
'type' => 'image',
'settings' => $formatter_settings,
))
->save();
return $field_config;
}
/**
* Preview an image in a node.
*

View File

@ -0,0 +1,70 @@
<?php
namespace Drupal\Tests\image\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Provides a helper method for creating Image fields.
*/
trait ImageFieldCreationTrait {
/**
* Create a new image field.
*
* @param string $name
* The name of the new field (all lowercase), exclude the "field_" prefix.
* @param string $type_name
* The node type that this field will be added to.
* @param array $storage_settings
* (optional) A list of field storage settings that will be added to the
* defaults.
* @param array $field_settings
* (optional) A list of instance settings that will be added to the instance
* defaults.
* @param array $widget_settings
* (optional) Widget settings to be added to the widget defaults.
* @param array $formatter_settings
* (optional) Formatter settings to be added to the formatter defaults.
* @param string $description
* (optional) A description for the field. Defaults to ''.
*/
protected function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array(), $formatter_settings = array(), $description = '') {
FieldStorageConfig::create(array(
'field_name' => $name,
'entity_type' => 'node',
'type' => 'image',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
))->save();
$field_config = FieldConfig::create([
'field_name' => $name,
'label' => $name,
'entity_type' => 'node',
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
'description' => $description,
]);
$field_config->save();
entity_get_form_display('node', $type_name, 'default')
->setComponent($name, array(
'type' => 'image_image',
'settings' => $widget_settings,
))
->save();
entity_get_display('node', $type_name, 'default')
->setComponent($name, array(
'type' => 'image',
'settings' => $formatter_settings,
))
->save();
return $field_config;
}
}