function BlockDependenciesTest::createBlock

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php \Drupal\Tests\views\Kernel\Plugin\BlockDependenciesTest::createBlock()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php \Drupal\Tests\views\Kernel\Plugin\BlockDependenciesTest::createBlock()
  3. 11.x core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php \Drupal\Tests\views\Kernel\Plugin\BlockDependenciesTest::createBlock()

Creates a block instance based on default settings.

Parameters

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->createBlock('system_powered_by_block', array(
    'label' => t('Hello, world!'),
));

The following defaults are provided:

  • label: Random string.
  • id: Random string.
  • region: 'sidebar_first'.
  • theme: The default theme.
  • visibility: Empty array.

Return value

\Drupal\block\Entity\Block The block entity.

2 calls to BlockDependenciesTest::createBlock()
BlockDependenciesTest::testExposedBlock in core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php
Tests that exposed filter blocks have the correct dependencies.
BlockDependenciesTest::testViewsBlock in core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php
Tests that exposed filter blocks have the correct dependencies.

File

core/modules/views/tests/src/Kernel/Plugin/BlockDependenciesTest.php, line 85

Class

BlockDependenciesTest
Tests views block config dependencies functionality.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

protected function createBlock($plugin_id, array $settings = []) {
    $settings += [
        'plugin' => $plugin_id,
        'region' => 'sidebar_first',
        'id' => strtolower($this->randomMachineName(8)),
        'theme' => $this->config('system.theme')
            ->get('default'),
        'label' => $this->randomMachineName(8),
        'visibility' => [],
        'weight' => 0,
    ];
    $values = [];
    foreach ([
        'region',
        'id',
        'theme',
        'plugin',
        'weight',
        'visibility',
    ] as $key) {
        $values[$key] = $settings[$key];
        // Remove extra values that do not belong in the settings array.
        unset($settings[$key]);
    }
    foreach ($values['visibility'] as $id => $visibility) {
        $values['visibility'][$id]['id'] = $id;
    }
    $values['settings'] = $settings;
    $block = Block::create($values);
    $block->save();
    return $block;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.