X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fblock%2Ftests%2Fsrc%2FTraits%2FBlockCreationTrait.php;fp=web%2Fcore%2Fmodules%2Fblock%2Ftests%2Fsrc%2FTraits%2FBlockCreationTrait.php;h=fe0a379aff4ceac54a7b477338d1e5114bf3abe4;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/block/tests/src/Traits/BlockCreationTrait.php b/web/core/modules/block/tests/src/Traits/BlockCreationTrait.php new file mode 100644 index 000000000..fe0a379af --- /dev/null +++ b/web/core/modules/block/tests/src/Traits/BlockCreationTrait.php @@ -0,0 +1,67 @@ +drupalPlaceBlock('system_powered_by_block', array( + * 'label' => t('Hello, world!'), + * )); + * @endcode + * The following defaults are provided: + * - label: Random string. + * - ID: Random string. + * - region: 'sidebar_first'. + * - theme: The default theme. + * - visibility: Empty array. + * + * @return \Drupal\block\Entity\Block + * The block entity. + * + * @todo + * Add support for creating custom block instances. + */ + protected function placeBlock($plugin_id, array $settings = []) { + $config = \Drupal::configFactory(); + $settings += [ + 'plugin' => $plugin_id, + 'region' => 'sidebar_first', + 'id' => strtolower($this->randomMachineName(8)), + 'theme' => $config->get('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; + } + +}