View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\Random;
use Drupal\devel_generate\DevelGenerateBase;
use Drupal\node\NodeInterface;
function devel_generate_menu_links_discovered_alter(&$links) {
$devel_generate_plugins = \Drupal::service('plugin.manager.develgenerate')
->getDefinitions();
foreach ($devel_generate_plugins as $id => $plugin) {
$label = $plugin['label'];
$links["devel_generate.{$id}"] = [
'title' => new TranslatableMarkup("Generate @label", [
'@label' => $label,
]),
'parent' => 'devel_generate.admin_config_generate',
'description' => $plugin['description'],
'route_name' => "devel_generate.{$id}",
'provider' => 'devel_generate',
];
}
$basics = [
'title' => new TranslatableMarkup('Generate'),
'description' => new TranslatableMarkup('Generate realistic items (content, users, menus, etc) to assist your site development and testing.'),
'route_name' => 'devel_generate.admin_config_generate',
'provider' => 'devel_generate',
];
$links['devel_generate.admin_config_generate'] = [
'parent' => 'system.admin_config',
'weight' => -9,
] + $basics;
$links['devel_generate.generate'] = [
'title' => new TranslatableMarkup('Devel generate'),
'parent' => 'system.admin_config_development',
] + $basics;
$links['devel_generate.generate2'] = [
'menu_name' => 'devel',
] + $basics;
}
function devel_generate_entity_insert(EntityInterface $entity) {
if ($entity
->getEntityTypeId() != 'node' || !isset($entity->devel_generate)) {
return;
}
$results = $entity->devel_generate;
if (!empty($results['max_comments'])) {
foreach ($entity
->getFieldDefinitions() as $field_name => $field_definition) {
if ($field_definition
->getType() == 'comment' && $entity
->get($field_name)->status == CommentItemInterface::OPEN) {
devel_generate_add_comments($entity, $field_definition, $results['users'], $results['max_comments'], $results['title_length']);
}
}
}
if (!empty($results['add_statistics']) && \Drupal::moduleHandler()
->moduleExists('statistics')) {
devel_generate_add_statistics($entity);
}
}
function devel_generate_add_comments(NodeInterface $node, FieldDefinitionInterface $field_definition, $users, $max_comments, $title_length = 8) {
$parents = [];
$field_name = $field_definition
->getName();
$num_comments = mt_rand(0, $max_comments);
for ($i = 1; $i <= $num_comments; $i++) {
switch ($i % 3) {
case 0:
case 1:
$parents = \Drupal::entityQuery('comment')
->condition('pid', 0)
->condition('entity_id', $node
->id())
->condition('entity_type', 'node')
->condition('field_name', $field_name)
->range(0, 1)
->execute();
break;
case 2:
$parents = \Drupal::entityQuery('comment')
->condition('pid', 0, '>')
->condition('entity_id', $node
->id())
->condition('entity_type', 'node')
->condition('field_name', $field_name)
->range(0, 1)
->execute();
break;
}
$random = new Random();
$stub = [
'entity_type' => $node
->getEntityTypeId(),
'entity_id' => $node
->id(),
'field_name' => $field_name,
'name' => 'devel generate',
'mail' => 'devel_generate@example.com',
'timestamp' => mt_rand($node
->getCreatedTime(), \Drupal::time()
->getRequestTime()),
'subject' => substr($random
->sentences(mt_rand(1, $title_length), TRUE), 0, 63),
'uid' => $users[array_rand($users)],
'langcode' => $node
->language()
->getId(),
];
if ($parents) {
$stub['pid'] = current($parents);
}
$comment = \Drupal::entityTypeManager()
->getStorage('comment')
->create($stub);
DevelGenerateBase::populateFields($comment);
$comment
->save();
}
}
function devel_generate_add_statistics(NodeInterface $node) {
$statistic = [
'nid' => $node
->id(),
'totalcount' => mt_rand(0, 500),
'timestamp' => \Drupal::time()
->getRequestTime() - mt_rand(0, $node
->getCreatedTime()),
];
$statistic['daycount'] = mt_rand(0, $statistic['totalcount']);
$database = \Drupal::database();
$database
->insert('node_counter')
->fields($statistic)
->execute();
}