Creates a block instance based on default settings.
Note: Until this can be done programmatically, the active user account must have permission to administer blocks.
@todo Add support for creating custom block instances.
string $plugin_id: The plugin ID of the block type for this block instance.
array $settings: (optional) An associative array of settings for the block entity. Override the defaults by specifying the key and value in the array, for example:
$this
->drupalPlaceBlock('system_powered_by_block', array(
'label' => t('Hello, world!'),
));
The following defaults are provided:
\Drupal\block\Plugin\Core\Entity\Block The block entity.
protected function drupalPlaceBlock($plugin_id, array $settings = array()) {
$settings += array(
'plugin' => $plugin_id,
'region' => 'sidebar_first',
'machine_name' => strtolower($this
->randomName(8)),
'theme' => config('system.theme')
->get('default'),
'label' => $this
->randomName(8),
'visibility' => array(),
);
foreach (array(
'region',
'machine_name',
'theme',
'plugin',
'visibility',
) as $key) {
$values[$key] = $settings[$key];
unset($settings[$key]);
}
$values['settings'] = $settings;
// Build the ID out of the theme and machine_name.
$values['id'] = $values['theme'] . '.' . $values['machine_name'];
$block = entity_create('block', $values);
$block
->save();
return $block;
}