Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\Tests\field\Traits;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\field\Entity\FieldConfig;
|
Chris@17
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@17
|
7
|
Chris@17
|
8 /**
|
Chris@17
|
9 * Provides common functionality for the EntityReference test classes.
|
Chris@17
|
10 */
|
Chris@17
|
11 trait EntityReferenceTestTrait {
|
Chris@17
|
12
|
Chris@17
|
13 /**
|
Chris@17
|
14 * Creates a field of an entity reference field storage on the specified bundle.
|
Chris@17
|
15 *
|
Chris@17
|
16 * @param string $entity_type
|
Chris@17
|
17 * The type of entity the field will be attached to.
|
Chris@17
|
18 * @param string $bundle
|
Chris@17
|
19 * The bundle name of the entity the field will be attached to.
|
Chris@17
|
20 * @param string $field_name
|
Chris@17
|
21 * The name of the field; if it already exists, a new instance of the existing
|
Chris@17
|
22 * field will be created.
|
Chris@17
|
23 * @param string $field_label
|
Chris@17
|
24 * The label of the field.
|
Chris@17
|
25 * @param string $target_entity_type
|
Chris@17
|
26 * The type of the referenced entity.
|
Chris@17
|
27 * @param string $selection_handler
|
Chris@17
|
28 * The selection handler used by this field.
|
Chris@17
|
29 * @param array $selection_handler_settings
|
Chris@17
|
30 * An array of settings supported by the selection handler specified above.
|
Chris@17
|
31 * (e.g. 'target_bundles', 'sort', 'auto_create', etc).
|
Chris@17
|
32 * @param int $cardinality
|
Chris@17
|
33 * The cardinality of the field.
|
Chris@17
|
34 *
|
Chris@17
|
35 * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
|
Chris@17
|
36 */
|
Chris@17
|
37 protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1) {
|
Chris@17
|
38 // Look for or add the specified field to the requested entity bundle.
|
Chris@17
|
39 if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
|
Chris@17
|
40 FieldStorageConfig::create([
|
Chris@17
|
41 'field_name' => $field_name,
|
Chris@17
|
42 'type' => 'entity_reference',
|
Chris@17
|
43 'entity_type' => $entity_type,
|
Chris@17
|
44 'cardinality' => $cardinality,
|
Chris@17
|
45 'settings' => [
|
Chris@17
|
46 'target_type' => $target_entity_type,
|
Chris@17
|
47 ],
|
Chris@17
|
48 ])->save();
|
Chris@17
|
49 }
|
Chris@17
|
50 if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
|
Chris@17
|
51 FieldConfig::create([
|
Chris@17
|
52 'field_name' => $field_name,
|
Chris@17
|
53 'entity_type' => $entity_type,
|
Chris@17
|
54 'bundle' => $bundle,
|
Chris@17
|
55 'label' => $field_label,
|
Chris@17
|
56 'settings' => [
|
Chris@17
|
57 'handler' => $selection_handler,
|
Chris@17
|
58 'handler_settings' => $selection_handler_settings,
|
Chris@17
|
59 ],
|
Chris@17
|
60 ])->save();
|
Chris@17
|
61 }
|
Chris@17
|
62 }
|
Chris@17
|
63
|
Chris@17
|
64 }
|