function ContentDevelGenerate::settingsForm
Same name in other branches
- 4.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::settingsForm()
Overrides DevelGenerateBase::settingsForm
File
-
devel_generate/
src/ Plugin/ DevelGenerate/ ContentDevelGenerate.php, line 149
Class
- ContentDevelGenerate
- Provides a ContentDevelGenerate plugin.
Namespace
Drupal\devel_generate\Plugin\DevelGenerateCode
public function settingsForm(array $form, FormStateInterface $form_state) : array {
$types = $this->nodeTypeStorage
->loadMultiple();
if (empty($types)) {
$create_url = $this->urlGenerator
->generateFromRoute('node.type_add');
$this->setMessage($this->t('You do not have any content types that can be generated. <a href=":create-type">Go create a new content type</a>', [
':create-type' => $create_url,
]), 'error');
return [];
}
$options = [];
foreach ($types as $type) {
$options[$type->id()] = [
'type' => [
'#markup' => $type->label(),
],
];
if ($this->commentManager instanceof CommentManagerInterface) {
$comment_fields = $this->commentManager
->getFields('node');
$map = [
$this->t('Hidden'),
$this->t('Closed'),
$this->t('Open'),
];
$fields = [];
foreach ($comment_fields as $field_name => $info) {
// Find all comment fields for the bundle.
if (in_array($type->id(), $info['bundles'])) {
$instance = FieldConfig::loadByName('node', $type->id(), $field_name);
$default_value = $instance->getDefaultValueLiteral();
$default_mode = reset($default_value);
$fields[] = new FormattableMarkup('@field: @state', [
'@field' => $instance->label(),
'@state' => $map[$default_mode['status']],
]);
}
}
// @todo Refactor display of comment fields.
if ($fields !== []) {
$options[$type->id()]['comments'] = [
'data' => [
'#theme' => 'item_list',
'#items' => $fields,
],
];
}
else {
$options[$type->id()]['comments'] = $this->t('No comment fields');
}
}
}
$header = [
'type' => $this->t('Content type'),
];
if ($this->commentManager instanceof CommentManagerInterface) {
$header['comments'] = [
'data' => $this->t('Comments'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
];
}
$form['node_types'] = [
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
];
$form['kill'] = [
'#type' => 'checkbox',
'#title' => $this->t('<strong>Delete all content</strong> in these content types before generating new content.'),
'#default_value' => $this->getSetting('kill'),
];
$form['num'] = [
'#type' => 'number',
'#title' => $this->t('How many nodes would you like to generate?'),
'#default_value' => $this->getSetting('num'),
'#required' => TRUE,
'#min' => 0,
];
$options = [
1 => $this->t('Now'),
];
foreach ([
3600,
86400,
604800,
2592000,
31536000,
] as $interval) {
$options[$interval] = $this->dateFormatter
->formatInterval($interval, 1) . ' ' . $this->t('ago');
}
$form['time_range'] = [
'#type' => 'select',
'#title' => $this->t('How far back in time should the nodes be dated?'),
'#description' => $this->t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
'#options' => $options,
'#default_value' => 604800,
];
$form['max_comments'] = [
'#type' => $this->moduleHandler
->moduleExists('comment') ? 'number' : 'value',
'#title' => $this->t('Maximum number of comments per node.'),
'#description' => $this->t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'),
'#default_value' => $this->getSetting('max_comments'),
'#min' => 0,
'#access' => $this->moduleHandler
->moduleExists('comment'),
];
$form['title_length'] = [
'#type' => 'number',
'#title' => $this->t('Maximum number of words in titles'),
'#default_value' => $this->getSetting('title_length'),
'#required' => TRUE,
'#min' => 1,
'#max' => 255,
];
$form['skip_fields'] = [
'#type' => 'textfield',
'#title' => $this->t('Fields to leave empty'),
'#description' => $this->t('Enter the field names as a comma-separated list. These will be skipped and have a default value in the generated content.'),
'#default_value' => NULL,
];
$form['base_fields'] = [
'#type' => 'textfield',
'#title' => $this->t('Base fields to populate'),
'#description' => $this->t('Enter the field names as a comma-separated list. These will be populated.'),
'#default_value' => NULL,
];
$form['add_type_label'] = [
'#type' => 'checkbox',
'#title' => $this->t('Prefix the title with the content type label.'),
'#description' => $this->t('This will not count against the maximum number of title words specified above.'),
'#default_value' => $this->getSetting('add_type_label'),
];
$form['add_alias'] = [
'#type' => 'checkbox',
'#disabled' => !$this->moduleHandler
->moduleExists('path'),
'#description' => $this->t('Requires path.module'),
'#title' => $this->t('Add an url alias for each node.'),
'#default_value' => FALSE,
];
$form['add_statistics'] = [
'#type' => 'checkbox',
'#title' => $this->t('Add statistics for each node (node_counter table).'),
'#default_value' => TRUE,
'#access' => $this->moduleHandler
->moduleExists('statistics'),
];
// Add the language and translation options.
$form += $this->getLanguageForm('nodes');
// Add the user selection checkboxes.
$author_header = [
'id' => $this->t('User ID'),
'user' => $this->t('Name'),
'role' => $this->t('Role(s)'),
];
$num_users = $this->database
->select('users')
->countQuery()
->execute()
->fetchField();
$author_form_limit = 50;
$query = $this->database
->select('users', 'u')
->fields('u', [
'uid',
])
->range(0, $author_form_limit)
->orderBy('uid');
$uids = $query->execute()
->fetchCol();
$author_rows = [];
foreach ($uids as $uid) {
/** @var \Drupal\user\UserInterface $user */
$user = $this->userStorage
->load($uid);
$author_rows[$user->id()] = [
'id' => [
'#markup' => $user->id(),
],
'user' => [
'#markup' => $user->getAccountName(),
],
'role' => [
'#markup' => implode(", ", $user->getRoles()),
],
];
}
$form['authors-wrap'] = [
'#type' => 'details',
'#title' => $this->t('Users'),
'#open' => FALSE,
'#description' => $this->t('Select users for randomly assigning as authors of the generated content.') . ($num_users > $author_form_limit ? ' ' . $this->t('The site has @num_users users, only the first @$author_form_limit are shown and selectable here.', [
'@num_users' => $num_users,
'@$author_form_limit' => $author_form_limit,
]) : ''),
];
$form['authors-wrap']['authors'] = [
'#type' => 'tableselect',
'#header' => $author_header,
'#options' => $author_rows,
];
$role_rows = [];
$roles = array_map(static fn($role): string => $role->label(), $this->roleStorage
->loadMultiple());
foreach ($roles as $role_id => $role_name) {
$role_rows[$role_id] = [
'id' => [
'#markup' => $role_id,
],
'role' => [
'#markup' => $role_name,
],
];
}
$form['authors-wrap']['roles'] = [
'#type' => 'tableselect',
'#header' => [
'id' => $this->t('Role ID'),
'role' => $this->t('Role Description'),
],
'#options' => $role_rows,
'#prefix' => $this->t('Specify the roles that randomly selected authors must have.'),
'#suffix' => $this->t('You can select users and roles. Authors will be randomly selected that match at least one of the criteria. Leave <em>both</em> selections unchecked to use a random selection of @$author_form_limit users, including Anonymous.', [
'@$author_form_limit' => $author_form_limit,
]),
];
$form['#redirect'] = FALSE;
return $form;
}