Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Fieldtypes;

use Statamic\Fieldtypes\Select;
use Statamic\Facades\GlobalSet;
use Illuminate\Support\Str;

class DefaultBlockSelect extends Select
{
protected $component = 'select';
protected $categories = ['special'];

protected function configFieldItems(): array
{
return [
'placeholder' => [
'display' => 'Placeholder',
'instructions' => 'The placeholder text when nothing is selected.',
'type' => 'text',
],
];
}

public function preload(): array
{
$options = [];

// Get the Default blocks global data
$globalSet = GlobalSet::findByHandle('default_blocks');

if ($globalSet && $globalSet->inCurrentSite()) {
$data = $globalSet->inCurrentSite()->data();
$defaultBlocks = $data->get('default_blocks', []);

foreach ($defaultBlocks as $block) {
// Only include groups that are available as blocks
if ($block['available_as_block'] ?? false) {
$label = $block['label'] ?? 'Unnamed Block';
$slug = Str::slug($label);
$options[] = [
'value' => $slug,
'label' => $label
];
}
}
}

return array_merge(parent::preload(), [
'options' => $options
]);
}

public function preProcess($data)
{
return $data;
}

public function process($data)
{
return $data;
}
}
57 changes: 57 additions & 0 deletions resources/presets/default_blocks/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

return [
'handle' => 'default_blocks',
'name' => 'Default blocks',
'description' => 'Reusable page builder blocks that can be placed globally.',
'operations' => [
[
'type' => 'copy',
'input' => 'app/FieldTypes/DefaultBlockSelect.php',
'output' => 'app/FieldTypes/DefaultBlockSelect.php',
],
[
'type' => 'copy',
'input' => 'content/globals/default_blocks.yaml',
'output' => 'content/globals/default_blocks.yaml',
],
[
'type' => 'copy',
'input' => 'resources/blueprints/globals/default_blocks.yaml',
'output' => 'resources/blueprints/globals/default_blocks.yaml',
],
[
'type' => 'copy',
'input' => 'resources/fieldsets/default_blocks.yaml',
'output' => 'resources/fieldsets/default_blocks.yaml',
],
[
'type' => 'copy',
'input' => 'resources/views/layout/_default_blocks.antlers.html',
'output' => 'resources/views/layout/_default_blocks.antlers.html',
],
[
'type' => 'copy',
'input' => 'resources/views/page_builder/_default_blocks.antlers.html',
'output' => 'resources/views/page_builder/_default_blocks.antlers.html',
],
[
'type' => 'update_page_builder',
'block' => [
'name' => 'Default blocks',
'instructions' => 'Add a reusable block from the Default blocks global.',
'icon' => 'programming-module-box-cube',
'handle' => 'default_blocks',
],
],
[
'type' => 'update_role',
'role' => 'editor',
'permissions' => ['view default_blocks globals', 'edit default_blocks globals'],
],
[
'type' => 'notify',
'content' => "Add the following partials to your `resources/views/default.antlers.html` template:\n\n{{# Default blocks (before content, inside main) #}}\n{{ partial:snippets/default_blocks position=\"before_content\" }}\n\n{{# Default blocks (after content, inside main) #}}\n{{ partial:layout/default_blocks position=\"after_content\" }}\n\n{{# Default blocks (before footer, outside main) #}}\n{{ partial:layout/default_blocks position=\"before_footer\" }}",
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Default blocks
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
tabs:
main:
display: 'Default blocks'
sections:
-
display: 'Default blocks'
instructions: 'Configure blocks that can appear automatically on pages or be manually placed via the page builder.'
fields:
-
handle: default_blocks
field:
type: replicator
display: 'Blocks'
instructions: 'Create reusable blocks with automatic positioning or manual page builder placement.'
button_label: 'Add Default block'
collapse: accordion
sets:
main:
display: Main
sets:
default_block:
display: 'Default block'
instructions: 'Configure a reusable block with its content and display rules.'
fields:
-
handle: label
field:
type: text
display: 'Group Label'
instructions: 'Internal label for this default block.'
validate:
- required
width: 50
-
handle: available_as_block
field:
type: toggle
display: 'Available as Block'
instructions: 'Make this group available as a reusable page builder block.'
default: false
width: 50
-
handle: position
field:
type: select
display: 'Auto Position'
instructions: 'Where should these blocks automatically appear?'
options:
none: 'None (Manual only)'
before_content: 'Before main content (inside main)'
after_content: 'After main content (inside main)'
before_footer: 'Before footer (outside main)'
default: none
width: 50
-
handle: visibility_mode
field:
type: button_group
display: 'Visibility Mode'
instructions: 'Control which pages show this default block.'
options:
all: 'All Pages'
whitelist: 'Only Selected'
blacklist: 'All Except Selected'
default: all
width: 50
if:
position: 'not none'
-
handle: whitelist_entries
field:
type: entries
display: 'Show on These Pages'
instructions: 'Select pages where this default block should appear.'
collections:
- pages
- offerings
create: false
if:
visibility_mode: 'equals whitelist'
enabled: 'equals true'
-
handle: blacklist_entries
field:
type: entries
display: 'Hide on These Pages'
instructions: 'Select pages where this default block should NOT appear.'
collections:
- pages
- offerings
create: false
if:
visibility_mode: 'equals blacklist'
enabled: 'equals true'
-
import: page_builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title: 'Default blocks'
fields:
-
handle: default_block_info
field:
type: html
display: 'Select block'
html: '<div class="text-xs text-gray-700">Select a reusable block from the Default blocks global. Only blocks with "Available as Block" enabled will appear here.</div>'
-
handle: default_block
field:
type: default_block_select
display: 'Default block'
placeholder: 'Choose a default block'
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{{#
@name Default blocks
@desc Renders Default blocks based on visibility settings
@param position The position to render blocks for (after_content, before_content or before_footer)
#}}

{{ _current_entry_id = id }}
{{ _requested_position = position ?? 'after_content' }}
{{ _slug = label | slugify}}

{{# Loop through all default blocks #}}
{{ default_blocks:default_blocks }}

{{# Only process groups with auto-positioning in the requested position #}}
{{ if position != 'none' && position == _requested_position }}

{{ _should_show = false }}

{{# Check visibility mode for this group #}}
{{ if visibility_mode == 'all' }}
{{ _should_show = true }}
{{ elseif visibility_mode == 'whitelist' }}
{{# Check if current page is in whitelist #}}
{{ whitelist_entries }}
{{ if id == _current_entry_id }}
{{ _should_show = true }}
{{ /if }}
{{ /whitelist_entries }}
{{ elseif visibility_mode == 'blacklist' }}
{{ _should_show = true }}
{{# Check if current page is in blacklist #}}
{{ blacklist_entries }}
{{ if id == _current_entry_id }}
{{ _should_show = false }}
{{ /if }}
{{ /blacklist_entries }}
{{ /if }}

{{# Check for page-specific override if the fieldset is used #}}
{{ if visibility_override }}
{{ if visibility_override == 'show' }}
{{ _should_show = true }}
{{ elseif visibility_override == 'hide' }}
{{ _should_show = false }}
{{ /if }}
{{ /if }}

{{# Render page builder blocks if conditions are met #}}
{{ if _should_show && page_builder }}
{{ if _requested_position == 'before_footer' }}
<div id="{{ _slug }}" class="py-12 md:py-16 lg:py-24 stack-12 md:stack-16 lg:stack-24" data-default-block="{{ _slug }}">
{{ page_builder scope="block" }}
{{ partial src="page_builder/{type}" }}
{{ /page_builder }}
</div>
{{ else }}
{{ page_builder scope="block" }}
{{ partial src="page_builder/{type}" }}
{{ /page_builder }}
{{ /if }}
{{ /if }}

{{ /if }}

{{ /default_blocks:default_blocks }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{#
@name Default blocks
@desc Renders a selected default block
@set page.page_builder.default_blocks
#}}

<!-- /page_builder/_default_blocks.antlers.html -->
{{ default_blocks:default_blocks }}
{{ _slug = label | slugify }}
{{ if _slug == block:default_block && available_as_block }}
{{ page_builder scope="block" }}
{{ partial src="page_builder/{type}" }}
{{ /page_builder }}
{{ /if }}
{{ /default_blocks:default_blocks }}
<!-- End: /page_builder/_default_blocks.antlers.html -->