### Example Settings Array in PHP
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
Provides an example of a settings array in PHP, defining various input types like checkbox, text, select, radios, and multi-checkboxes. This showcases the structure for defining block settings.
```php
return [
[
'name' => 'demo-checkbox',
'label' => 'Demo Checkbox',
'value' => $field->input('demo-checkbox', 'checkbox'),
],
[
'name' => 'demo-text',
'label' => 'Demo Text Field',
'value' => $field->input('demo-text', 'text'),
],
[
'name' => 'demo-select',
'label' => 'Demo Select Field',
'value' => $field->input('demo-select', 'select', [
'foo' => 'foo value',
'bar' => 'bar value',
]),
],
[
'name' => 'demo-radios',
'label' => 'Demo Radios Field',
'value' => $field->input(
'demo-radios',
'radios', [
'foo' => 'foo value',
'*bar' => 'bar value',
]
),
],
[
'name' => 'demo-checkboxes',
'label' => 'Demo Checkboxes Field',
'value' => $field->input(
'demo-checkboxes',
'checkboxes', [
'foo' => 'foo value',
'*bar' => 'bar value',
]
),
],
];
```
--------------------------------
### Configure RockDevTools for LESS/CSS Assets
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/setup.md
This PHP code snippet illustrates how to configure RockDevTools in the `_init.php` file to process LESS files and save them as CSS. It specifically shows how to add RockPageBuilder's LESS files to the compilation process, ensuring that custom block styles are loaded correctly in the frontend.
```php
// /site/templates/_init.php
if ($config->rockdevtools) {
$devtools = rockdevtools();
// parse all less files to css
$devtools->assets()
->less()
->add('/site/templates/uikit/src/less/uikit.theme.less')
->add('/site/templates/src/*.less')
->add('/site/templates/sections/*.less')
// add this line
->add('/site/templates/RockPageBuilder/*.less')
// save the css files
// this is the file that you need to include in your main markup!
->save('/site/templates/bundle/styles.css');
// optionally add a minify step
// see RockDevTools docs
}
```
--------------------------------
### Define Global Block Settings (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This example demonstrates how to set default settings for all blocks in a project using `site/ready.php`. It allows for global settings like a muted background and conditional settings based on block templates.
```php
// in site/ready.php
/** @var RockPageBuilder $rpb */
$rpb = $this->wire->modules->get('RockPageBuilder');
$rpb->defaultSettings(
function (BlockSettingsArray $settings, RockFieldsField $field, Block $block) {
$settings->add([
'name' => 'bgmuted',
'label' => 'Add muted background',
'value' => $field->input('bgmuted', 'checkbox'),
]);
if($block->template == 'fooblock') {
$settings->add(...);
}
}
);
```
--------------------------------
### RockPageBuilder Block Markup and Frontend Editing (JavaScript/PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/quickstart.md
This code snippet demonstrates how to structure a RockPageBuilder block, including integrating the 'alfred' function for frontend editing. It shows how to access block properties like 'title' and make them editable directly on the frontend. This example is provided in both JavaScript and PHP formats, with and without comments.
```javascript
// in every block you have access to the $block variable
// it's a $page with superpowers 😉 more on that later...
>
// show the title field of the block as h1 headline
// note that we are using title(), not title! this is
// all it takes to make the field frontend editable 💪
= $block->title() ?>
```
```php
```
--------------------------------
### Loading Blocks with Namespaces (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Provides an example of how to load RockPageBuilder blocks from a specified directory and assign them a custom namespace to prevent naming conflicts. This is a streamlined way to manage block definitions.
```php
$mx->loadBlocks("fieldname", $path, "FooNamespace");
```
--------------------------------
### Implementing Block Migrations (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Provides examples of migration methods for blocks that run before and after YAML settings are applied. `migrateBeforeYaml` runs prior to YAML settings being applied, meaning YAML settings will override values set here. `migrateAfterYaml` runs after YAML settings are applied, allowing values set here to override YAML settings.
```php
public function migrateBeforeYaml() {
// this code will run BEFORE yaml settings have been applied
// that means settings from the yaml file will overwrite settings from here
}
```
```php
public function migrateAfterYaml() {
// this code will run AFTER yaml settings have been applied
// that means settings from here will overwrite settings from the yaml file
}
```
--------------------------------
### Include RockPageBuilder Assets in Frontend (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/setup.md
This snippet shows the equivalent of including RockPageBuilder and RockFrontend assets using standard PHP syntax, suitable for non-Latte template files. These assets are essential for the frontend editing experience and the visual interface of RockPageBuilder.
```php
assets() ?>
assets() ?>
```
--------------------------------
### Accessing RockPageBuilder Module in ProcessWire
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Demonstrates how to get an instance of the RockPageBuilder module within a ProcessWire site using either the global `$modules` object or a helper function. This is the first step to interacting with the module's API.
```php
get('RockPageBuilder');
// Or use the helper function
$rockpagebuilder = rockpagebuilder();
?>
```
--------------------------------
### Render Block with Latte Template
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Example of rendering a block using the Latte templating engine. It conditionally displays an image with floating capabilities or a lightbox-enabled section based on settings.
```html
{if $settings->float}
{else}
{/if}
```
--------------------------------
### Defining Block Labels (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Shows how to define custom labels for blocks using PHP. The first example returns the 'body' field as the label. The second example demonstrates how to return custom HTML, including the 'title' field, as the block's label.
```php
public function getLabel() {
return $this->body;
}
```
```php
public function getLabel() {
return $this->html("MyBlock: {$this->title}");
}
```
--------------------------------
### Include RockPageBuilder Assets in Frontend (Latte)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/setup.md
This snippet demonstrates how to include the necessary JavaScript and CSS assets for RockPageBuilder and RockFrontend in a Latte template file. These assets are crucial for enabling frontend editing features and the RockPageBuilder GUI. They are typically loaded in the `
` section of your main markup file.
```latte
{* NOTE: styles.min.css & scripts.min.js are generated in _init.php via RockDevTools *}
{rockfrontend()->styleTag('/site/templates/dst/styles.min.css')|noescape}
{rockfrontend()->scriptTag('/site/templates/dst/scripts.min.js', 'defer')|noescape}
{rockfrontend()->assets()|noescape}
{rockpagebuilder()->assets()|noescape}
```
--------------------------------
### Access Block Settings in Frontend Views (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This code provides examples of how to access block settings within a block's view file. It shows how to check a setting's value and retrieve a single setting with a default value.
```php
// in your block's view file you can access settings via the $settings variable:
if($settins->mySetting == 'foo') echo 'Foo setting is set!';
// get a single setting and set a default value
$mySetting = $block->settings('mySetting', 'default value');
// same as above but different syntax
$mySetting = $settings->mySetting ?: 'default value';
```
--------------------------------
### Set Block Thumbnail using Font Awesome Icon
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/blocks/readme.md
This example demonstrates how to use a Font Awesome icon as a block's thumbnail by setting the 'icon' property in the info() method. Any valid Font Awesome 4 icon name can be used.
```php
public function info() {
return [
...
'icon' => 'check',
];
}
```
--------------------------------
### Configure Content-Only Field
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
PHP example using RockMigrations to configure a field for a content-only block. It sets 'rpb-nolabel' to true to hide the field label and reduce padding, simplifying the UI for blocks with a single primary field.
```php
$rm->migrate([
'fields' => [
self::field_body => [
'type' => 'textarea',
'rpb-nolabel' => true,
...
```
--------------------------------
### Initializing RockPageBuilder Blocks (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Shows how to initialize RockPageBuilder and add demo blocks to your ProcessWire site. This code snippet should be placed in your site's init.php file or within your module's init() method.
```php
// site/init.php (or in module init())
if($modules->isInstalled('RockPageBuilder')) {
$mx = $modules->get('RockPageBuilder');
$mx->addBlocks($config->paths->siteModules."RockPageBuilder/demo/");
}
```
--------------------------------
### Old Method: Define Block Settings Input (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This demonstrates an older method for defining block settings using `settingsInput` and `settingsSleep`. It shows how to create a table of input fields for block configurations.
```php
public function settingsInput(RockFieldsField $field) {
return $field->table([
'Headline' => $field->input("headline"),
'Whatever' => $field->input("whatever"),
]);
}
public function settingsSleep(RockFieldsField $field) {
return [
$field->getInputArray("headline"),
$field->getInputArray("whatever"),
];
}
```
--------------------------------
### Enable and Render Overlay Feature in PHP
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Shows how to enable the overlay feature by setting a configuration variable and then how to render an overlay image based on the current page template using RockPageBuilder's overlay method.
```php
$config->overlays = true;
```
```php
echo $rockpagebuilder->overlay($page->template);
```
--------------------------------
### Add Existing Block to Field
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
PHP example demonstrating how to add an existing block (identified by its page ID) to a RockPageBuilder field. The changes are then saved to the page.
```php
$page->getUnformatted('your_rpb_field')
->add(1035) // add block id 1035 to this field
->save();
```
--------------------------------
### Reset and Add Blocks to Field
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
PHP example illustrating how to reset a RockPageBuilder field before adding new blocks. This ensures that only the newly added blocks are present in the field after the operation.
```php
$page->rmtest->reset()->create(...)->create(...)->save();
```
--------------------------------
### Old Method: Define Block Settings Input with Options (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This snippet shows how to define block settings using `settingsInput` and return an array to specify options like label, icon, and collapsed state for the settings field.
```php
public function settingsInput(RockFieldsField $field) {
return [
'label' => 'My Settings field',
'icon' => 'check',
'value' => $field->table([
'Headline' => $field->input("headline"),
'Whatever' => $field->input("whatever"),
]),
'collapsed' => Inputfield::collapsedNo,
];
}
```
--------------------------------
### Configuring Block Information (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Illustrates how to define metadata for a RockPageBuilder block using the `info()` method. This includes settings like icon, title, color, sort order, and conditional display logic based on the parent page.
```php
public function info() {
return [
'icon' => 'bullhorn',
'title' => 'Jobs',
'color' => 'PaleGreen',
'sort' => 900,
'show' => function($page) {
// show the button to add this block only on page having title 'Jobs'
return $page->title == 'Jobs';
},
];
}
```
--------------------------------
### Get RockPageBuilder Blocks FieldData
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/api/readme.md
Retrieves all blocks associated with a RockPageBuilder field from a ProcessWire page. The result is a FieldData object, which is a WireArray, allowing for further manipulation and filtering of blocks.
```php
$blocks = $page->rockpagebuilder_blocks;
```
--------------------------------
### Creating and Modifying Fields and Templates with RockMigrations (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/fields/readme.md
Demonstrates how to use RockMigrations to create new fields and templates, or update existing ones. The `migrate()` method accepts an array defining fields (with their types and labels) and templates (specifying their fields). This process is idempotent, ensuring consistent results across multiple executions.
```php
public function migrate()
{
$rm = $this->rockmigrations();
$rm->migrate([
'fields' => [
'foo' => [
'type' => 'text',
'label' => 'Foo Field',
],
'bar' => [...],
],
'templates' => [
'demo' => [
'fields' => [
'title',
'foo',
'bar',
],
],
],
]);
}
```
--------------------------------
### Enabling YAML Recorder for Blocks (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Illustrates how to configure a block to save its field and template settings as a YAML configuration file. This is achieved by returning an array with the 'yaml' key set to true within the block's info() method.
```php
class ExampleBlock extends Block
{
public function info()
{
return [
'title' => 'IconColumns',
'yaml' => true,
];
}
}
```
--------------------------------
### Load and Register Blocks (PHP)
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Configures available blocks for specific fields using `loadBlocks` or hooks. It demonstrates loading blocks from directories, specifying namespaces, and using hooks to conditionally allow blocks based on fields or page templates.
```php
wire->modules->get('RockPageBuilder');
// Load blocks from a specific directory for a field
$rpb->loadBlocks(
'my_blocks_field', // Field name
$this->wire->config->paths->templates . 'blocks/', // Path to blocks
'MyProject\Blocks' // Namespace
);
// Load blocks from multiple directories
$rpb->loadBlocksArray('content_blocks', [
'/site/templates/RockPageBuilder/content/' => 'RockPageBuilderBlock',
'/site/modules/MyModule/blocks/' => 'MyModule\Blocks',
]);
// Use hook to define allowed blocks per field/page
$this->wire->addHookAfter('RockPageBuilder::getAllowedBlocks', function($event) {
$field = $event->arguments(0);
$page = $event->arguments(1);
if ($field->name === 'sidebar_blocks') {
// Only allow specific blocks for sidebar
$event->return->add([
'RockPageBuilderBlock\Text',
'RockPageBuilderBlock\CallToAction',
]);
}
// Conditionally allow blocks based on page template
if ($page->template->name === 'product') {
$event->return->add('RockPageBuilderBlock\ProductGallery');
}
});
?>
```
--------------------------------
### Adding Default Settings Globally in PHP
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
Shows how to define global default settings for all blocks using the `defaultSettings` method in PHP. This promotes code reuse by avoiding repetitive setting definitions.
```php
// in site/ready.php
use RockPageBuilder\Block;
use RockPageBuilder\BlockSettingsArray;
/** @var RockPageBuilder $rpb */
$rpb = $this->wire->modules->get('RockPageBuilder');
$rpb->defaultSettings(
function (BlockSettingsArray $settings, RockFieldsField $field, Block $block) {
$settings->add([
'name' => 'bgmuted',
'label' => 'Add muted background',
'value' => $field->input('bgmuted', 'checkbox'),
]);
if($block->template == 'your-block-type-template') {
$settings->add(...);
}
}
);
```
--------------------------------
### Set Block Field Defaults (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/blocks/readme.md
Provides default values for block fields. The 'defaults' key in the info() method allows pre-filling fields when a new block is created. This example sets a default title for a 'Gallery' block.
```php
class Gallery extends Block
{
public function info()
{
return [
'title' => 'Gallery',
'defaults' => [
'title' => 'Some great images',
],
];
}
}
```
--------------------------------
### Conditional Settings with showIf in PHP
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
Demonstrates how to use the `showIf` attribute in PHP to conditionally display settings based on other field values. This allows for dynamic user interfaces.
```php
public function settingsTable(\ProcessWire\RockFieldsField $field)
{
return $this->getDefaultSettings($field)
->add([
'name' => 'showImages',
'label' => 'Show Items with Images',
'value' => $field->input('showImages', 'checkbox'),
])->add([
'name' => 'noBackground',
'label' => 'Do not add blurred background behind images',
'value' => $field->input('noBackground', 'checkbox'),
'showIf' => 'showImages=1',
]);
}
```
--------------------------------
### Set Custom Parent for Blocks
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
PHP code for defining a custom parent for RockPageBuilder blocks within a block's class. This example sets the parent to the result of the page's getBlockPage() method, meaning new blocks will be added as children of that page.
```php
public function getParent($field, $page)
{
// use the page where the block is created as parent
// that means new blocks will be added as child of the page
return $page->getBlockPage();
}
```
--------------------------------
### YAML Migrations for Blocks (PHP)
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Enables automatic YAML recording of field and template settings for blocks, facilitating deployment and version control. It shows how to enable YAML generation and use migration hooks (`migrateBeforeYaml`, `migrateAfterYaml`) for custom logic before and after YAML settings are applied.
```php
'My Block',
'yaml' => true, // Enable YAML migration file generation
];
}
// This creates MyBlock.yaml alongside MyBlock.php when fields are saved via GUI
// The YAML file contains all field and template configurations
// You can also use migration hooks for custom logic:
public function migrateBeforeYaml()
{
// Runs BEFORE yaml settings are applied
// Settings from yaml will overwrite these
$this->rm()->createField(self::field_custom, 'text');
}
public function migrateAfterYaml()
{
// Runs AFTER yaml settings are applied
// These settings will overwrite yaml
$this->rm()->setFieldData(self::field_custom, [
'notes' => 'Custom note added after YAML',
]);
}
// Main migration method (alias for migrateAfterYaml)
public function migrate()
{
$rm = $this->rockmigrations();
$rm->migrate([
'fields' => [...],
'templates' => [...],
]);
}
?>
```
--------------------------------
### Create Block View File with RockMigrations
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
A PHP snippet showing how to use RockMigrations to automatically create a view file for a block. This is typically called within the block's migrate() method.
```php
// in the block's migrate() method:
$rm->createViewFile($this);
```
--------------------------------
### Custom Latte View Stub for RockPageBuilder
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/stubs/readme.md
This Latte view stub provides a template for the HTML structure of a new block in RockPageBuilder. It includes example markup with common classes like `tm-block`, UIkit classes, and dynamic attributes. This allows for consistent styling and structure across all generated blocks, incorporating project-specific elements like `$site->bgClass`.
```latte
{* site/templates/RockPageBuilder/stubs/.Block.latte *}
```
--------------------------------
### Enable and Render Design Overlays in PHP
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
This snippet shows how to enable the design overlay feature in RockPageBuilder and how to render overlay images in templates or block views. This feature helps developers compare their implementation against design mockups during development. Supported image formats include PNG, JPG, JPEG, and SVG.
```php
overlays = true;
// Render overlay in your template
// This looks for /site/templates/overlays/home.png for the home template
echo $rockpagebuilder->overlay($page->template);
// Or in your block view file
echo $block->overlay();
// Place design images in /site/templates/overlays/
// Supported formats: png, jpg, jpeg, svg
// Files are displayed as semi-transparent overlays with opacity slider
```
--------------------------------
### Configure Block Settings Wrapper Options (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This code shows how to control the display options for the block settings wrapper field within a block's `info()` method. Options include disabling the settings field entirely or configuring its label, icon, and collapsed state.
```php
public function info() {
return [
...
'settings' => false, // no settings field for this block
'settings' => [
'label' => 'Settings for this block',
'icon' => 'check',
'collapsed' => Inputfield::collapsedNo,
],
];
}
```
--------------------------------
### Define Block Info (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/blocks/readme.md
Defines the title, description, and icon for a block within its info() method. This information is used in the UI when adding new blocks. The 'show' key can be used to conditionally display the block based on page properties or user roles.
```php
public function info()
{
return [
'title' => 'Spacings',
'description' => 'Please see docs about Block Spacings!',
'icon' => 'cube',
...
];
}
public function info()
{
return [
'title' => 'Blog-Headline',
'description' => 'This headline is only available on blogitem pages!',
'show' => function($page) {
return $page->template == 'blogitem' ? true : false;
},
];
}
public function info()
{
return [
'title' => 'Superuser-Block',
'show' => function($page) {
return $this->wire->user->isSuperuser();
},
];
}
```
--------------------------------
### Creating a Custom Block with RockPageBuilder (PHP)
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Shows how to define a custom content block by extending the `RockPageBuilder\Block` class. It includes defining block metadata, custom fields using RockMigrations, and setting up template associations. This is the core logic for a new block type.
```php
'My Custom Block',
'icon' => 'cube',
'color' => 'blue', // Border color in admin
'description' => 'A sample block with content and image',
'hideTitle' => false, // Set true to hide title field
'spaceV' => '50px, 100px', // Vertical spacing (min, max)
];
}
// Define fields via RockMigrations (recommended approach)
public function migrate()
{
$rm = $this->rockmigrations();
$rm->migrate([
'fields' => [
self::field_content => [
'type' => 'textarea',
'inputfieldClass' => 'InputfieldTinyMCE',
'contentType' => FieldtypeTextarea::contentTypeHTML,
'label' => 'Content',
'rows' => 5,
],
self::field_image => [
'type' => 'image',
'maxFiles' => 1,
'label' => 'Featured Image',
],
],
'templates' => [
$this->getTplName() => [
'fields' => [
RockPageBuilder::field_eyebrow,
'title',
self::field_content,
self::field_image,
],
],
],
]);
}
}
```
--------------------------------
### Define Default Block Settings in PHP
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
This snippet demonstrates how to define global default settings for all blocks in RockPageBuilder using a PHP callback. It shows how to add options like background color and spacing, and conditionally add settings based on block type. These defaults can be extended or overridden by individual blocks.
```php
wire->modules->get('RockPageBuilder');
// Define default settings for all blocks
$rpb->defaultSettings(function(
BlockSettingsArray $settings,
RockFieldsField $field,
Block $block
) {
// Add background color option to all blocks
$settings->add([
'name' => 'bgmuted',
'label' => 'Muted Background',
'value' => $field->input('bgmuted', 'checkbox'),
]);
// Add spacing option to all blocks
$settings->add([
'name' => 'spacing',
'label' => 'Vertical Spacing',
'value' => $field->input('spacing', 'select', [
'*normal' => 'Normal',
'compact' => 'Compact',
'spacious' => 'Spacious',
]),
]);
// Conditional settings based on block type
if ($block->template == 'rockpagebuilderblock-hero') {
$settings->add([
'name' => 'fullscreen',
'label' => 'Fullscreen Hero',
'value' => $field->input('fullscreen', 'checkbox'),
]);
}
});
// In your block, remove or modify default settings:
public function settingsTable(RockFieldsField $field)
{
$settings = $this->getDefaultSettings($field);
// Remove a global setting for this block
$settings->remove('name=bgmuted');
// Add block-specific settings
$settings->prepend([
'name' => 'priority',
'label' => 'Priority Setting',
'value' => $field->input('priority', 'text'),
]);
return $settings;
}
```
--------------------------------
### Render Widget from Template File
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
PHP code demonstrating how to render a RockPageBuilder widget directly from a template file. It shows two methods: rendering by widget name ('Team') or by block ID.
```php
// either via widget name:
echo $rockpagebuilder->widgets('Team')->render();
// or you can also render any block like this
// where 123 is the block id
echo $pages->get(123)->renderBlock();
```
--------------------------------
### Combine Global and Custom Settings (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
This PHP snippet illustrates how to combine global settings with custom settings within a block. It uses `getGlobalSettings` and then `add`, `prepend`, `insertAfter`, or `insertBefore` to manage custom fields.
```php
public function settingsTable(\ProcessWire\RockFieldsField $field)
{
$settings = $this->getGlobalSettings($field)
->use([
'style',
'demo-checkbox',
]);
// add: add a new settings field at the end
$settings->add([
'name' => 'custom-checkbox',
'label' => 'Custom checkbox',
'value' => $field->input('custom-checkbox', 'checkbox'),
]);
// prepend: add checkbox at first position
$settings->prepend([
'name' => 'prepended-custom-checkbox',
'label' => 'Prepended custom checkbox',
'value' => $field->input('prepended-custom-checkbox', 'checkbox'),
]);
// insertAfter/insertBefore: add checkbox after/before other setting
$newItem = $settings->getItem([
'name' => 'inserted-custom-checkbox',
'label' => 'Inserted custom checkbox',
'value' => $field->input('inserted-custom-checkbox', 'checkbox'),
]);
$settings->insertAfter($newItem, $settings->get('style'));
return $settings;
}
```
--------------------------------
### PHP Frontend Rendering of RockPageBuilder Blocks
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Demonstrates how to render RockPageBuilder field content in ProcessWire template files. It shows including block styles, rendering the default blocks field, and including frontend editing assets.
```php
= $page->title ?>
= $rockpagebuilder->styles() ?>
= $rockpagebuilder->render() ?>
= $rockpagebuilder->render(true) ?>
= $rockpagebuilder->render(false) ?>
= $rockpagebuilder->assets() ?>
```
--------------------------------
### Use Method Syntax for Verbose Field Names in PHP
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/fields-access/readme.md
Illustrates how to use the RockPageBuilder's helper method syntax to simplify access to fields with long or prefixed names. This method, originating from RockMigrations' MagicPages, shortens verbose field names into callable methods.
```php
echo $block->pdf();
```
```php
$block->pdf(); // frontend editable field
$block->pdf(1); // using ->getFormatted()
$block->pdf(2); // using ->getUnformatted()
```
```php
// field foo_bar_baz
$block->baz()
// field foo_something
$block->something()
```
--------------------------------
### Use Global Settings in a Block (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
This PHP snippet shows how a block can utilize globally defined settings. The `getGlobalSettings` method fetches the settings, and the `use` method specifies which settings to include and their rendering order.
```php
public function settingsTable(\ProcessWire\RockFieldsField $field)
{
return $this->getGlobalSettings($field)
->use([
'style',
'demo-checkbox',
]);
}
```
--------------------------------
### Complex showIf Conditions in JavaScript
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
Illustrates how to construct complex conditional logic for the `showIf` attribute using logical operators like AND (`&&`) and OR (`||`), and grouping with parentheses. This enables advanced visibility rules.
```javascript
'showIf' => 'advancedOptions=1 && (layout=grid || layout=list)'
```
```javascript
'showIf' => 'whatever=1 || text="foo bar"'
```
--------------------------------
### Configure Vertical Spacing and Background Classes
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Manage automatic vertical spacing and background color handling between blocks by configuring properties in your block class's info() method. You can set single values, responsive pairs, or separate top/bottom spacing. Additionally, dynamic `spaceID` and `bgID` can be defined for grouping and styling purposes, and helper methods generate CSS classes and styles for templates.
```php
'My Block',
// Single spacing value
'spaceV' => '50px',
// Responsive spacing (mobile, desktop)
'spaceV' => '30px, 80px',
// Or define top/bottom separately
'spaceT' => '20px, 40px', // Top spacing
'spaceB' => '40px, 80px', // Bottom spacing
// Space ID for grouping (same ID = adjacent blocks share margins)
'spaceID' => 'white',
];
}
// Dynamic spaceID based on block settings
public function spaceID(): string
{
return $this->settings('background', 'white');
}
// Dynamic background ID for section grouping
public function bgID(): string
{
return $this->settings('bgColor') ?: 'default';
}
// In your Latte template - apply spacing styles
// Add custom styles along with spacing
styles('border: 1px solid red;')|noescape}>
// Get CSS classes for background sections
```
--------------------------------
### Minimum Viable RockPageBuilder Block (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Presents the most basic structure for a RockPageBuilder block. It requires extending the `RockPageBuilderBlock` class and can be placed in a monitored folder or explicitly added using `$mx->addBlocks()`.
```php
[
'label' => 'Settings for this block',
'icon' => 'check',
'collapsed' => Inputfield::collapsedYes,
],
];
}
```
--------------------------------
### Accessing Translations in Templates (HTML/Latte)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Demonstrates how to display translated strings within a template file using the $block->x() helper function. This function retrieves the translated string defined in the block's PHP file based on the provided key.
```html
{$block->x('my_foo_string')}
{$block->x('my_bar_string')}
```
--------------------------------
### Custom Block Classes in RockPageBuilder (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Demonstrates how to define custom block classes in RockPageBuilder, extending the base Block class and implementing custom methods. This approach contrasts with the hook-based customization in RepeaterMatrix.
```php
class BlockFoo extends Block {
public function foo() {
return 'foo';
}
}
class BlockBar extends Block {
public function bar() {
return 'bar';
}
}
```
--------------------------------
### Manage Widgets (Global Reusable Blocks)
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Create and manage widgets, which are global reusable blocks whose changes are reflected across all pages they are used on. Widgets are stored on the home page in the 'rockpagebuilder_widgets' field. You can render widgets by block type name or ID, retrieve all widgets, or convert existing blocks into widgets. Widget blocks can also hook into RockPageBuilder to specify which pages display them.
```php
widgets('ContactInfo')->render();
// Render widget by ID
echo $pages->get(123)->renderBlock();
// Get all widgets
$allWidgets = $rockpagebuilder->widgets();
// Get specific widget
$teamWidget = $rockpagebuilder->widget('Team');
if ($teamWidget->id) {
echo $teamWidget->renderBlock();
}
// Convert existing block to widget programmatically
$block->toWidget();
// In widget block: notify RockPageBuilder which pages display this widget
// Add to your Widget block's init() method:
public function init()
{
$this->addHookAfter("RockPageBuilder::getWidgetPages", function($event) {
$pages = $event->return;
// Add all job pages that display this widget
$pages->add($this->wire->pages->find("template=job"));
$event->return = $pages;
});
}
```
--------------------------------
### Custom Block View Template (Latte) for RockPageBuilder
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Provides a Latte template for a custom RockPageBuilder block. It demonstrates how to access block data, settings, and fields like title, content, and image, and how to apply classes and styles. This defines the frontend presentation of the block.
```latte
{* site/templates/RockPageBuilder/blocks/MyBlock/MyBlock.latte *}
{var $style = $block->settings('style', 'default')}
{var $img = $block->image()}
styles()|noescape}
{alfred($block)}
>
{* Eyebrow headline *}
{$block->eyebrow()}
{* Main headline - editable on frontend *}
{$block->title()}
{* Content field - editable on frontend *}
{$block->content()}
{* Image with responsive sizing *}
```
--------------------------------
### Block Labels and Custom Getters (PHP)
Source: https://context7.com/baumrock/rockpagebuilder/llms.txt
Defines custom labels and computed properties for blocks in the admin interface. It shows how to create simple text labels, labels based on field values, and custom getters for dynamic properties like image URLs and cache-busting timestamps.
```php
title ?: 'Untitled Block';
// Or return field value
return $this->body;
}
// Label with custom HTML formatting
public function getLabel()
{
return $this->html("{$this->className}: {$this->title}");
}
// Custom computed properties
public function getImageUrl(): string
{
$img = $this->getFormatted(self::field_image);
if (!$img) return '';
return $img->maxSize(1280, 720)->webp->url . $this->m();
}
// Get cache-busting modified timestamp
public function m($len = 4): string
{
// Returns ?m=1234 based on block's modified timestamp
return "?m=" . substr($this->modified, -$len);
}
?>
```
--------------------------------
### Conditional Output with Block Helper Methods (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Shows how to use RockPageBuilder's built-in helper methods like `isEven()` to conditionally render HTML content. This allows for easy styling of alternating blocks or specific elements within the output.
```php
if($block->isEven()) echo "= $block->title ?>
";
else echo "= $block->title ?>
";
```
--------------------------------
### RepeaterMatrix Hook-based Customization (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
Illustrates how to customize RepeaterMatrix blocks using method hooks. This method involves checking the page type within the hook to apply specific logic, which is presented as a less convenient alternative to RockPageBuilder's class-based approach.
```php
$wire->addHookMethod('RepeaterBlockPage::foo', function($event) {
$page = $event->object;
if($page->type !== 'rpbTypeFoo') return;
$event->return = 'foo';
});
$wire->addHookMethod('RepeaterBlockPage::bar', function($event) {
$page = $event->object;
if($page->type !== 'rpbTypeBar') return;
$event->return = 'bar';
});
```
--------------------------------
### Define Block Settings via API (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
This snippet shows how to add custom settings to a block by implementing the `settingsTable` method. It utilizes RockFields to define input fields for block-specific configurations like padding.
```php
public function settingsTable(RockFieldsField $field) {
$settings = $this->getDefaultSettings($field);
$settings->add([
'name' => 'blockpadding',
'label' => 'Block-Padding',
'value' => $field->input('blockpadding', 'radios', [
'*s' => 'small padding',
'm' => 'medium padding',
'l' => 'large padding',
]),
]);
return $settings;
}
```
--------------------------------
### Define Global Settings (PHP)
Source: https://github.com/baumrock/rockpagebuilder/blob/main/docs/settings/readme.md
This snippet demonstrates how to define global settings for RockPageBuilder blocks. It creates a PHP file that returns an array of field configurations, including a checkbox and a select dropdown.
```php
'demo-checkbox',
'label' => 'Demo Checkbox',
'value' => $field->input('demo-checkbox', 'checkbox'),
],
[
'name' => 'style',
'label' => 'Style Variation',
'value' => $field->input('style', 'select', [
'*one' => 'Style One (image in the background)',
'two' => 'Style Two (image on the right)',
'three' => 'Style Three (centered)',
]),
]
];
```
--------------------------------
### Manually Include Template File
Source: https://github.com/baumrock/rockpagebuilder/blob/main/README.md
If a block's view is not automatically rendered, this PHP line can be included in the template file to manually render the block's content. It assumes '_main.php' contains the necessary rendering logic.
```php
include '_main.php';
```