Commit 66741e36 authored by alexpott's avatar alexpott

Issue #2782309 by samuel.mortenson, martin107: Refactor File and Image related...

Issue #2782309 by samuel.mortenson, martin107: Refactor File and Image related image field creation logic into a new trait
parent dd6d5868
......@@ -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 @@
*/
abstract class ImageFieldTestBase extends WebTestBase {
use ImageFieldCreationTrait;
/**
* Modules to enable.
*
......@@ -51,62 +52,6 @@ protected function setUp() {
$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.
*
......
<?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;
}
}
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