annotate core/modules/field/src/Tests/EntityReference/EntityReferenceTestTrait.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field\Tests\EntityReference;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldConfig;
Chris@0 6 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Provides common functionality for the EntityReference test classes.
Chris@0 10 */
Chris@0 11 trait EntityReferenceTestTrait {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Creates a field of an entity reference field storage on the specified bundle.
Chris@0 15 *
Chris@0 16 * @param string $entity_type
Chris@0 17 * The type of entity the field will be attached to.
Chris@0 18 * @param string $bundle
Chris@0 19 * The bundle name of the entity the field will be attached to.
Chris@0 20 * @param string $field_name
Chris@0 21 * The name of the field; if it already exists, a new instance of the existing
Chris@0 22 * field will be created.
Chris@0 23 * @param string $field_label
Chris@0 24 * The label of the field.
Chris@0 25 * @param string $target_entity_type
Chris@0 26 * The type of the referenced entity.
Chris@0 27 * @param string $selection_handler
Chris@0 28 * The selection handler used by this field.
Chris@0 29 * @param array $selection_handler_settings
Chris@0 30 * An array of settings supported by the selection handler specified above.
Chris@0 31 * (e.g. 'target_bundles', 'sort', 'auto_create', etc).
Chris@0 32 * @param int $cardinality
Chris@0 33 * The cardinality of the field.
Chris@0 34 *
Chris@0 35 * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
Chris@0 36 */
Chris@0 37 protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1) {
Chris@0 38 // Look for or add the specified field to the requested entity bundle.
Chris@0 39 if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
Chris@0 40 FieldStorageConfig::create([
Chris@0 41 'field_name' => $field_name,
Chris@0 42 'type' => 'entity_reference',
Chris@0 43 'entity_type' => $entity_type,
Chris@0 44 'cardinality' => $cardinality,
Chris@0 45 'settings' => [
Chris@0 46 'target_type' => $target_entity_type,
Chris@0 47 ],
Chris@0 48 ])->save();
Chris@0 49 }
Chris@0 50 if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
Chris@0 51 FieldConfig::create([
Chris@0 52 'field_name' => $field_name,
Chris@0 53 'entity_type' => $entity_type,
Chris@0 54 'bundle' => $bundle,
Chris@0 55 'label' => $field_label,
Chris@0 56 'settings' => [
Chris@0 57 'handler' => $selection_handler,
Chris@0 58 'handler_settings' => $selection_handler_settings,
Chris@0 59 ],
Chris@0 60 ])->save();
Chris@0 61 }
Chris@0 62 }
Chris@0 63
Chris@0 64 }