View source
<?php
namespace Drupal\Tests\field_collection\Functional;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\field_collection\Entity\FieldCollectionItem;
trait FieldCollectionTestTrait {
protected $field_collection_field_storage;
protected $field_collection_field;
protected $inner_field_name;
protected $inner_field_definition;
protected $field_collection_name;
protected $nodeStorage;
private function setUpFieldCollectionTest() {
$this->nodeStorage = \Drupal::entityTypeManager()
->getStorage('node');
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
$this->field_collection_name = 'field_test_collection';
$this->field_collection_field_storage = FieldStorageConfig::create([
'field_name' => $this->field_collection_name,
'entity_type' => 'node',
'type' => 'field_collection',
'cardinality' => 4,
]);
$this->field_collection_field_storage
->save();
$this->field_collection_field = $this
->addFieldCollectionFieldToContentType('article');
$this->inner_field_name = 'field_inner';
$inner_field_storage = FieldStorageConfig::create([
'field_name' => $this->inner_field_name,
'entity_type' => 'field_collection_item',
'type' => 'integer',
]);
$inner_field_storage
->save();
$this->inner_field_definition = [
'field_name' => $this->inner_field_name,
'entity_type' => 'field_collection_item',
'bundle' => $this->field_collection_name,
'field_storage' => $inner_field_storage,
'label' => $this
->randomMachineName() . '_label',
'description' => $this
->randomMachineName() . '_description',
'settings' => [],
];
$inner_field = FieldConfig::create($this->inner_field_definition);
$inner_field
->save();
entity_get_form_display('field_collection_item', $this->field_collection_name, 'default')
->setComponent($this->inner_field_name, [
'type' => 'number',
])
->save();
entity_get_display('field_collection_item', $this->field_collection_name, 'default')
->setComponent($this->inner_field_name, [
'type' => 'number_decimal',
])
->save();
}
protected function addFieldCollectionFieldToContentType($content_type) {
$field_collection_definition = [
'field_name' => $this->field_collection_name,
'entity_type' => 'node',
'bundle' => $content_type,
'field_storage' => $this->field_collection_field_storage,
'label' => $this
->randomMachineName() . '_label',
'description' => $this
->randomMachineName() . '_description',
'settings' => [],
];
$field_config = FieldConfig::create($field_collection_definition);
$field_config
->save();
\Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load("node.{$content_type}.default")
->setComponent($this->field_collection_name, [
'type' => 'field_collection_editable',
])
->save();
\Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load("node.{$content_type}.default")
->setComponent($this->field_collection_name, [
'type' => 'field_collection_embed',
])
->save();
return $field_config;
}
protected function createNodeWithFieldCollection($content_type) {
$node = $this
->drupalCreateNode([
'type' => $content_type,
]);
$entity = FieldCollectionItem::create([
'field_name' => $this->field_collection_name,
]);
$entity->{$this->inner_field_name}
->setValue(1);
$entity
->setHostEntity($node);
$entity
->save();
return [
$node,
$entity,
];
}
}