use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
+use Drupal\Core\Entity\ContentEntityStorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
$values['target_id'] = array_rand($referenceable[$group]);
return $values;
}
+
+ // Attempt to create a sample entity, avoiding recursion.
+ $entity_storage = \Drupal::entityTypeManager()->getStorage($options['target_type']);
+ if ($options['target_type'] !== $field_definition->getTargetEntityTypeId() && $entity_storage instanceof ContentEntityStorageInterface) {
+ $bundle = static::getRandomBundle($entity_type, $options['handler_settings']);
+ $values['entity'] = $entity_storage->createWithSampleValues($bundle);
+ return $values;
+ }
+ }
+
+ /**
+ * Gets a bundle for a given entity type and selection options.
+ *
+ * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
+ * The entity type.
+ * @param array $selection_settings
+ * An array of selection settings.
+ *
+ * @return string|null
+ * Either the bundle string, or NULL if there is no bundle.
+ */
+ protected static function getRandomBundle(EntityTypeInterface $entity_type, array $selection_settings) {
+ if ($bundle_key = $entity_type->getKey('bundle')) {
+ if (!empty($selection_settings['target_bundles'])) {
+ $bundle_ids = $selection_settings['target_bundles'];
+ }
+ else {
+ $bundle_ids = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id());
+ }
+ return array_rand($bundle_ids);
+ }
}
/**