function BlockContentDevelGenerate::develGenerateContentAddBlock
Create one block. Used by both batch and non-batch code branches.
Parameters
array $results: Results information.
2 calls to BlockContentDevelGenerate::develGenerateContentAddBlock()
- BlockContentDevelGenerate::batchContentAddBlock in devel_generate/
src/ Plugin/ DevelGenerate/ BlockContentDevelGenerate.php - Batch wrapper for calling ContentAddBlock.
- BlockContentDevelGenerate::generateContent in devel_generate/
src/ Plugin/ DevelGenerate/ BlockContentDevelGenerate.php - Generate content when not in batch mode.
File
-
devel_generate/
src/ Plugin/ DevelGenerate/ BlockContentDevelGenerate.php, line 360
Class
- BlockContentDevelGenerate
- Provides a BlockContentDevelGenerate plugin.
Namespace
Drupal\devel_generate\Plugin\DevelGenerateCode
protected function develGenerateContentAddBlock(array &$results) : void {
if (!isset($results['time_range'])) {
$results['time_range'] = 0;
}
$block_type = array_rand($results['block_types']);
// Add the block type label if required.
$title_prefix = $results['add_type_label'] ? $this->blockContentTypeStorage
->load($block_type)
->label() . ' - ' : '';
$values = [
'info' => $title_prefix . $this->getRandom()
->sentences(mt_rand(1, $results['title_length']), TRUE),
'type' => $block_type,
// A flag to let hook_block_content_insert() implementations know that this is a generated block.
'devel_generate' => $results,
];
if (isset($results['add_language'])) {
$values['langcode'] = $this->getLangcode($results['add_language']);
}
if (isset($results['reusable'])) {
$values['reusable'] = (int) $results['reusable'];
}
/** @var \Drupal\block_content\BlockContentInterface $block */
$block = $this->blockContentStorage
->create($values);
// Populate non-skipped fields with sample values.
$this->populateFields($block, $results['skip_fields'], $results['base_fields']);
// Remove the fields which are intended to have no value.
foreach ($results['skip_fields'] as $field) {
unset($block->{$field});
}
$block->save();
// Add translations.
$this->develGenerateContentAddBlockTranslation($results, $block);
}