function relation_generate_relations in Relation 8.2
Same name and namespace in other branches
- 8 relation.drush.inc \relation_generate_relations()
- 7 relation.drush.inc \relation_generate_relations()
Generates pseudorandom relations. Appropriate entities must already exist.
Parameters
$number_relations: Number of entities to generate of each entity_type.
$types: Array of relation_type to generate and delete relations. Set to NULL to generate and delete all.
$kill: Whether to delete all existing relations before creating new ones.
Return value
Array of relation IDs of the generated relations.
2 calls to relation_generate_relations()
- drush_relation_generate in ./
relation.drush.inc - Drush callback to generate relations.
- RelationGenerate::submitForm in relation_devel/
src/ Form/ RelationGenerate.php - Form submission handler.
File
- ./
relation.drush.inc, line 71 - Drush integration for the relation module.
Code
function relation_generate_relations($number_relations = 10, $relation_types = array(), $kill = FALSE) {
$relation_types = RelationType::loadMultiple($relation_types);
if ($kill) {
foreach ($relation_types as $relation_type) {
$relation_ids = Drupal::entityQuery('relation')
->condition('relation_type', $relation_type
->id())
->execute();
$storage_handler = \Drupal::entityTypeManager()
->getStorage('relation');
$relations = $storage_handler
->loadMultiple($relation_ids);
$storage_handler
->delete($relations);
relation_generate_message(t('Deleted all @type relations.', array(
'@type' => $relation_type
->label(),
)));
}
}
$new_relation_ids = array();
foreach ($relation_types as $relation_type) {
$available_types = array();
foreach ($relation_type->source_bundles as $bundle_key) {
list($entity_type, $bundle) = explode(':', $bundle_key, 2);
$available_types['source'][$entity_type][] = $bundle;
}
foreach ($relation_type->target_bundles as $bundle_key) {
list($entity_type, $bundle) = explode(':', $bundle_key, 2);
$available_types['target'][$entity_type][] = $bundle;
}
$arity = rand($relation_type->min_arity, $relation_type->min_arity);
for ($i = $number_relations; $i > 0; $i--) {
// Start new relation.
$endpoints = array();
for ($delta = 0; $delta < $arity; $delta++) {
$direction = $relation_type->directional && $delta > 0 ? 'target' : 'source';
$entity_type = array_rand($available_types[$direction]);
$entity_info = \Drupal::entityTypeManager()
->getDefinition($entity_type);
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type);
$query = Drupal::entityQuery($entity_type);
if (!in_array('*', $available_types[$direction][$entity_type])) {
// Entities with a single bundle don't support EFQ bundle condition.
if (count($bundles) > 1 && isset($entity_info['entity_keys']['bundle'])) {
$query
->condition($entity_info['entity_keys']['bundle'], $available_types[$direction][$entity_type], 'IN');
}
}
if ($results = $query
->execute()) {
$key = array_rand($results);
$endpoints[] = array(
'entity_type' => $entity_type,
'entity_id' => $results[$key],
'delta' => $delta,
);
}
}
$relation = Relation::create(array(
'relation_type' => $relation_type
->id(),
));
$relation->endpoints = $endpoints;
$relation
->save();
$new_relation_ids[] = $relation
->id();
relation_generate_message(\Drupal::translation()
->formatPlural($number_relations, 'Generated @count @relation_type relation.', 'Generated @count @relation_type relations.', array(
'@relation_type' => $relation_type
->id(),
)), 'ok');
}
}
return $new_relation_ids;
}