Jump to content

Tips for creating reusable settings


FireWire
 Share

Recommended Posts

Sharing a strategy that I have found useful when working with blocks and block settings.

I use the block settings feature heavily and in many instances most blocks have some of the same settings fields. To help make managing these easier, I created a workflow to help me manage these and it's saved me a good amount of time while being able to reuse settings between projects very easily. In my case, some of these are:

  • Spacing between blocks/sections on the page
  • Background/accent colors
  • Content location within blocks
  • Block presentation

My approach is to create a dedicated class with static methods that return a settings array. Example:

<?php
// /site/init.php

/**
 *  This creates a namespace for a /site/templates/RockPageBuilderSupport directory
 */
wire('classLoader')->addNamespace('RockPageBuilderSupport', __DIR__ . '/templates/RockPageBuilderSupport');

This file contains the fast helper methods that create settings fields

<?php

// /site/templates/RockPgeBuilderSupport/BlockSettings.php

namespace RockPageBuilderSupport;

use ProcessWire\RockFieldsField;

/**
 * Reusable block settings
 */
class BlockSettings
{
    public static function sectionPresentation(
        RockFieldsField $field,
        array $config = [],
        array $additionalValues = [],
    ): array {
        $name = 'section_presentation';

        return [
            'name' => $name,
            'label' => 'Section Presentation',
            'value' => $field->input($name, 'select', [
                '*normal' => 'Normal',
                'standalone' => 'Standalone',
                'standalone_drop_shadow' => 'Standalone + Drop Shadow',
                ...$additionalValues,
            ]),
            ...$config,
        ];
    }

    public static function bodyWidth(
        RockFieldsField $field,
        array $config = [],
        array $additionalValues = [],
    ): array {
        $name = 'body_width';

        return [
            'name' => $name,
            'label' => 'Body Width',
            'value' => $field->input($name, 'select', [
                '*constrained' => 'Constrained',
                'full' => 'Full',
                ...$additionalValues,
            ]),
            ...$config,
        ];
    }

    public static function backgroundColor(
        RockFieldsField $field,
        array $config = [],
        array $additionalValues = [],
    ): array {
        $name = 'background_color';

        return [
            'name' => $name,
            'label' => 'Background Color',
            'value' => $field->input($name, 'select', [
                '*white' => 'White',
                'seafoam' => 'Seafoam',
                'champagne' => 'Champagne',
                'none' => 'None',
                ...$additionalValues,
            ]),
            ...$config,
        ];
    }

    // ...as many commonly used settings methods as you need
}

Settings are easily reusable in any block.

<?php

declare(strict_types=1);

namespace RockPageBuilderBlock;

use ProcessWire\RockFieldsField;
use RockPageBuilder\{Block, BlockSettingsArray};
use RockPageBuilderSupport\BlockSettings;

class BlogFeed extends Block {

    const prefix = "rpb_blogfeed_";

    /**
     * Block config info
     */
    public function info(): array
    {
        return [
            'title' => 'News Article Feed',
            'description' => 'An array of blog posts',
            // ...other info
        ];
    }

    /**
     * Runtime block settings
     */
    public function settingsTable(RockFieldsField $field): BlockSettingsArray
    {
        $settings = $this->getDefaultSettings($field);

        $settings->add(
            BlockSettings::bodyWidth($field)
        );

        $settings->add(
            BlockSettings::bodyLocationVertical($field)
        );

        $settings->add(
            BlockSettings::actionLocationVertical($field, config: [
                'label' => "Below 'view all' link"
            ])
        );

        $settings->add(
            BlockSettings::sectionPresentation($field, additionalValues: [
                'full' => 'Full news feed design',
            ])
        );

        return $settings;
    }

    // ... ommitted for brevity
}

This has helped me easily manage common settings for 20 different blocks by editing the settings in one place. By adding the $config and $additionalValues parameters to the BlockSettings methods, overrides can be added when individual blocks need customization. Thanks to the runtime nature of block settings, making these changes on the fly is extremely easy.

This has saved me a lot of time and helps keep things organized. I was able to carry over the bulk of my work with settings from one project to another and it really helped out a lot.

Would love to hear if others out there have developed some tips and tricks that help you build your sites!

Link to comment
Share on other sites

Ok nice. Just wanted to make sure I understand exactly why you are not using the available approach.

But I like your idea. I'm thinking maybe RPB could load a file like /site/templates/RockPageBuilder/blocksettings.php like this:

<?php
return [
  'foo' => [ ... ],
  'bar' => [ ... ],
  'baz' => [ ... ],
];

Which would make it then possible to set block settings like this:

<?php
public function settingsTable(RockFieldsField $field) {
  $settings = $this->getDefaultSettings();
  $settings
    ->add('foo')
    ->add('baz');
  return $settings;
}

 

  • Like 1
Link to comment
Share on other sites

9 minutes ago, bernhard said:

Just wanted to make sure I understand exactly why you are not using the available approach

@bernhard It was completely my oversight, I didn't know that RPB already had a solution for managing settings to be used globally. So it wasn't due to anything other than not knowing about it ahead of time.

9 minutes ago, bernhard said:

Which would make it then possible to set block settings like this:

I like this implementation! Very elegant. Having it in the RPB templates folder is great and that array definition style is really intuitive.

Link to comment
Share on other sites

Ok not exactly an hour - took a bit longer than expected... as always 🙈😅

Here you go: Please grab v5.11.0 here https://www.baumrock.com/en/releases/rockpagebuilder/

And RTFM check out the docs here: https://www.baumrock.com/en/processwire/modules/rockpagebuilder/docs/settings/#adding-settings-globally

Thx for the idea! Enjoy! 🙂 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...