Skip to content

Commit

Permalink
Added option for collection + provided information in readme & servic…
Browse files Browse the repository at this point in the history
…e provider
  • Loading branch information
Kevin Meijer committed Nov 19, 2024
1 parent 3f97e1f commit 85a1f79
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 11 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
## Features

The page builder contains a:
This addon adds the following features:

- Pages Collection
- Hero Banner Component
- Text Component
- Image + text Component
- USP Component
- Form Component

## Install

Expand All @@ -18,19 +21,17 @@ composer require justbetter/statamic-page-builder-kit

## How to Use

Simply install the addon and add the `component_page_builder` fieldset to a blueprint.
You can use the page builder by including it like this:
When making use of the collections provided by this addon, the page builder will already be generated for you.
If you want to add the page builder to an existing blueprint, you can do so by adding the `statamic-page-builder-kit::page_builder` fieldset to the blueprint.
You can use the page builder in your templates by including it like this:

``` blade
@include('statamic-page-builder::page_builder')
@include('statamic-page-builder-kit::page_builder')
```

## Todo

- Add Pages Collection
- Add Form Component
- Add image slider Component
- Add image + text Component
- Add CTA blocks Component
- Add accordeon Component
- Add Blog collection + category taxonomy + Component (Add-on)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"require": {
"php": "^8.1|^8.2|^8.3",
"statamic/cms": "^5.0",
"laravel/framework": "^10.0 || ^11.0",
"tormjens/eventy": "^0.9.4",
"rapidez/blade-directives": "^0.7.0"
},
Expand Down
12 changes: 11 additions & 1 deletion config/statamic-page-builder-kit.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Boot Collections
|--------------------------------------------------------------------------
|
| When enabled, the package will automatically create and configure the
| pages collection with the default structure. Set to false if you want
| to manage the collection configuration manually.
|
*/
'boot_collections' => true,
];
1 change: 0 additions & 1 deletion resources/fieldsets/button.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,3 @@ fields:
visibility: visible
replicator_preview: true
hide_display: false
width: 50
10 changes: 10 additions & 0 deletions resources/fieldsets/component_general_form.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: 'Component - General - Form'
fields:
-
import: 'statamic-page-builder-kit::title'
-
handle: form
field:
max_items: 1
type: form
display: Formulier
23 changes: 23 additions & 0 deletions resources/fieldsets/title.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
title: Title
fields:
-
handle: title
field:
buttons:
- bold
- italic
- removeformat
- anchor
- h1
- h2
- h3
- h4
- h5
- h6
- underline
- strikethrough
- small
remove_empty_nodes: false
type: bard
display: Titel
localizable: true
15 changes: 15 additions & 0 deletions resources/fieldsets/url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
title: 'URL'
fields:
-
handle: url
field:
collections:
- pages
type: link
display: 'URL'
localizable: true
listable: hidden
instructions_position: above
visibility: visible
replicator_preview: true
hide_display: false
4 changes: 4 additions & 0 deletions resources/views/page_builder/general_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Available variables:
- $title
- $form
-->
19 changes: 17 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public function bootAddon(): void
$this
->bootConfig()
->bootViews()
->bootPageBuilder()
->bootCollections();
->bootPageBuilder();

if (config('justbetter.statamic-page-builder-kit.boot_collections', true)) {
$this->bootCollections();
}
}

public function bootConfig(): self
Expand All @@ -46,13 +49,16 @@ public function bootViews(): self

public function bootCollections(): self
{
// Try to find the existing pages collection
$pagesCollection = CollectionFacade::findByHandle('pages');

// Create or update the pages collection if it doesn't exist or has no configuration file
if (! $pagesCollection || ! File::exists($pagesCollection->path())) {
if (! $pagesCollection) {
$pagesCollection = CollectionFacade::make('pages');
}

// Set up the default configuration for the pages collection
$pagesData = [
'title' => __('Pages'),
'sites' => array_keys(Site::all()->toArray()),
Expand All @@ -74,10 +80,12 @@ public function bootCollections(): self
],
];

// Write the configuration to the collection file
$file = YAML::file($pagesCollection->path());
File::put($pagesCollection->path(), $file->dump($pagesData));
}

// Ensure the page blueprint exists, copy from package resources if it doesn't
if (! Blueprint::find('collections/pages/page')) {
$pageBlueprint = Blueprint::make('collections/pages/page');
File::copyDirectory(__DIR__.'/../resources/blueprints/collections/pages/', File::dirname($pageBlueprint->path()));
Expand All @@ -88,27 +96,34 @@ public function bootCollections(): self

public function bootPageBuilder(): self
{
// Get all available fieldsets
$pageBuilderComponents = FieldsetFacade::all();

// Filter and group the fieldsets that are marked as page builder components
$pageBuilderComponents = $pageBuilderComponents
->filter(fn ($fieldset) => $this->fieldsetIsComponent($fieldset))
->mapToGroups(fn ($fieldset) => [
$this->getFieldsetGroup($fieldset) => [$this->getFieldsetName($fieldset) => $fieldset],
])
->toBase();

// Format the groups for display in the UI
$groups = $pageBuilderComponents
->map(fn ($fieldsets, $group) => [
'display' => __(Str::headline($group)),
'sets' => $this->getGroupFieldsets($fieldsets),
])->toArray();

// Get the page builder fieldset from the package
$pageBuilderFieldset = FieldsetFacade::find('statamic-page-builder-kit::page_builder');
$pageBuilderContent = $pageBuilderFieldset?->contents();

// Update the fieldset with the newly organized component groups
if (! empty($pageBuilderContent['fields']) && ! empty($pageBuilderContent['fields'][0]['field']['sets'])) {
$pageBuilderContent['fields'][0]['field']['sets'] = $groups;
}

// Save the updated fieldset configuration without triggering events
$pageBuilderFieldset
?->setContents($pageBuilderContent ?? [])
?->saveQuietly();
Expand Down

0 comments on commit 85a1f79

Please sign in to comment.