devel_generate.module
Same filename in other branches
Devel sub-module to for generating content, menus, taxonomy terms etc.
See src/Plugin for specific details of each type that can be generated.
File
-
devel_generate/
devel_generate.module
View source
<?php
/**
* @file
* Devel sub-module to for generating content, menus, taxonomy terms etc.
*
* See src/Plugin for specific details of each type that can be generated.
*/
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\devel_generate\DevelGenerateBase;
use Drupal\node\NodeInterface;
/**
* Implements hook_menu_links_discovered_alter().
*/
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',
];
}
// Store the basic link info for repeated use. Each of the three actual links
// require subtle variations on this.
$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',
];
// Define a separate group on admin/config page, so that 'Generate' has its
// own block with all the generate links.
$links['devel_generate.admin_config_generate'] = [
'parent' => 'system.admin_config',
// The main development group has weight -10 in system.links.menu.yml so use
// -9 here as this block should be near but just after it on the page.
'weight' => -9,
] + $basics;
// Add a link in the main development group, to allow direct access to the
// the Generate page and to make the back breadcrumb more useful.
$links['devel_generate.generate'] = [
'title' => new TranslatableMarkup('Devel generate'),
'parent' => 'system.admin_config_development',
] + $basics;
// Define a top-level link (with no parent) in the 'devel' menu. This also
// means that it will be available in the devel admin toolbar.
$links['devel_generate.generate2'] = [
'menu_name' => 'devel',
] + $basics;
}
/**
* Implements hook_entity_insert().
*/
function devel_generate_entity_insert(EntityInterface $entity) {
// Adding url alias is now moved into develGenerateContentAddNode().
// @todo Can more of this processing moved there too?
if ($entity->getEntityTypeId() != 'node' || !isset($entity->devel_generate)) {
return;
}
/** @var \Drupal\node\NodeInterface $entity */
$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) {
// Add comments for each comment field on entity.
devel_generate_add_comments($entity, $field_definition, $results['users'], $results['max_comments'], $results['title_length']);
}
}
}
// Add node statistics.
if (!empty($results['add_statistics']) && \Drupal::moduleHandler()->moduleExists('statistics')) {
devel_generate_add_statistics($entity);
}
}
/**
* Create comments and add them to a node.
*
* @param \Drupal\node\NodeInterface $node
* Node to add comments to.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field storage definition.
* @param array $users
* Array of users to assign comment authors.
* @param int $max_comments
* Max number of comments to generate per node.
* @param int $title_length
* Max length of the title of the comments.
*/
function devel_generate_add_comments(NodeInterface $node, FieldDefinitionInterface $field_definition, array $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:
// No parent.
case 1:
// Top level parent.
$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:
// Non top level parent.
$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);
// Populate all core fields on behalf of field.module.
DevelGenerateBase::populateFields($comment);
$comment->save();
}
}
/**
* Generate statistics information for a node.
*
* @param \Drupal\node\NodeInterface $node
* A node object.
*/
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();
}
Functions
Title | Deprecated | Summary |
---|---|---|
devel_generate_add_comments | Create comments and add them to a node. | |
devel_generate_add_statistics | Generate statistics information for a node. | |
devel_generate_entity_insert | Implements hook_entity_insert(). | |
devel_generate_menu_links_discovered_alter | Implements hook_menu_links_discovered_alter(). |