Add a command to automatically create a block

This commit is contained in:
Tom 2023-06-22 14:46:42 +02:00
parent 7b28fc5661
commit 290273e4ca
5 changed files with 161 additions and 0 deletions

View file

@ -0,0 +1,36 @@
package extras
import (
"context"
"html/template"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient/php"
_ "embed"
)
type Blocks struct {
ingredient.Base
Dependencies struct {
PHP *php.PHP
}
}
//go:embed blocks.php
var blocksPHP string
type Block struct {
Info string
Content template.HTML
Region string
BlockID string
}
// Create creates a new block with the given title and html content
func (blocks *Blocks) Create(ctx context.Context, server *phpx.Server, block Block) (err error) {
err = blocks.Dependencies.PHP.ExecScript(ctx, server, nil, blocksPHP, "create_basic_block", block.Info, block.Content, block.Region, block.BlockID)
return err
}

View file

@ -0,0 +1,36 @@
<?php
/**
* Creates a basic block and optionally places it into the right region
*/
function create_basic_block(string $info, string $html, string $region, string $block_id) {
// create a custom block
$block_content = \Drupal\block_content\Entity\BlockContent::create([
'info' => $info,
'type' => 'basic',
'body' => [
'value' => $html,
'format' => 'full_html',
],
]);
$block_content->save();
if ($region === "") {
return;
}
// get plugin and theme id
$plugin = 'block_content:' . $block_content->uuid();
$theme = \Drupal::theme()->getActiveTheme()->getName();
$block = \Drupal\block\Entity\Block::create([
'plugin' => $plugin,
'id' => $block_id,
'region' => $region,
'status' => TRUE,
'theme' => $theme,
'weight' => 0,
]);
$block->save();
}