• Stars
    star
    310
  • Rank 134,926 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.

ACF Composer

Latest Stable Version Total Downloads Build Status

ACF Composer is the ultimate tool for creating fields, blocks, widgets, and option pages using ACF Builder alongside Sage 10.

Screenshot

Features

  • πŸ”₯ Encourages clean structuring for creating fields with Sage 10 and ACF.
  • πŸ”₯ Instantly generate working fields, blocks, widgets, and option pages. Batteries included.
  • πŸ”₯ Instantly generate re-usable field group partials.
  • πŸ”₯ Blocks and widgets are fully rendered using Blade with a native Sage 10 feel for passing view data.
  • πŸ”₯ Blocks are automatically generated with <InnerBlocks /> support if ACF v5.9.0+ is installed.
  • πŸ”₯ Automatically hooks widgets with WP_Widget making them instantly ready to use.
  • πŸ”₯ Automatically sets field location on blocks, widgets, and option pages.
  • πŸ”₯ Globally set default field type and field group settings. No more repeating ['ui' => 1] on every select field.

Requirements

Installation

Install via Composer:

$ composer require log1x/acf-composer

Usage

Getting Started

Start by publishing the config/acf.php configuration file using Acorn:

$ wp acorn vendor:publish --provider="Log1x\AcfComposer\Providers\AcfComposerServiceProvider"

Generating a Field

To create your first field, start by running the following generator command from your theme directory:

$ wp acorn acf:field Example

This will create src/Fields/Example.php which is where you will create and manage your first field group.

Taking a glance at the generated Example.php stub, you will notice that it has a simple list configured.

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->setLocation('post_type', '==', 'post');

        $example
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $example->build();
    }
}

Proceed by checking the Add Post for the field to ensure things are working as intended – and then get to work.

Generating a Field Partial

A field partial consists of a field group that can be re-used and/or added to existing field groups.

To start, let's generate a partial called ListItems that we can use in the Example field we generated above.

$ wp acorn acf:partial ListItems
<?php

namespace App\Fields\Partials;

use Log1x\AcfComposer\Partial;
use StoutLogic\AcfBuilder\FieldsBuilder;

class ListItems extends Partial
{
    /**
     * The partial field group.
     *
     * @return array
     */
    public function fields()
    {
        $listItems = new FieldsBuilder('listItems');

        $listItems
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $listItems;
    }
}

Looking at ListItems.php, you will see out of the box it consists of an identical list repeater as seen in your generated field.

A key difference to note compared to an ordinary field is the omitting of ->build() instead returning the FieldsBuilder instance itself.

This can be utilized in our Example field by passing the ::class constant to ->addFields().

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;
use App\Fields\Partials\ListItems;

class Example extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->setLocation('post_type', '==', 'post');

        $example
            ->addFields($this->get(ListItems::class));

        return $example->build();
    }
}

Generating a Block

Generating a block is generally the same as generating a field as seen above.

Start by creating the block field using Acorn:

$ wp acorn acf:block Example
<?php

namespace App\Blocks;

use Log1x\AcfComposer\Block;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Block
{
    /**
     * The block name.
     *
     * @var string
     */
    public $name = 'Example';

    /**
     * The block description.
     *
     * @var string
     */
    public $description = 'Lorem ipsum...';

    /**
     * The block category.
     *
     * @var string
     */
    public $category = 'common';

    /**
     * The block icon.
     *
     * @var string|array
     */
    public $icon = 'star-half';

    /**
     * Data to be passed to the block before rendering.
     *
     * @return array
     */
    public function with()
    {
        return [
            'items' => $this->items(),
        ];
    }

    /**
     * The block field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $example->build();
    }

    /**
     * Return the items field.
     *
     * @return array
     */
    public function items()
    {
        return get_field('items') ?: [];
    }
}

You may also pass --construct to the command above to generate a stub with the block properties set within the constructor. This can be useful for localization, etc.

$ wp acorn acf:block Example --construct

When running the block generator, one difference to a generic field is an accompanied View is generated in the resources/views/blocks directory.

@if ($items)
  <ul>
    @foreach ($items as $item)
      <li>{{ $item['item'] }}</li>
    @endforeach
  </ul>
@else
  <p>No items found!</p>
@endif

<div>
  <InnerBlocks />
</div>

Like the field generator, the example block contains a simple list repeater and is working out of the box.

Block Preview View

While $block->preview is an option for conditionally modifying your block when shown in the editor, you may also render your block using a seperate view.

Simply duplicate your existing view prefixing it with preview- (e.g. preview-example.blade.php).

Generating a Widget

Important
With WordPress 5.8, Blocks can now be used as widgets making this feature somewhat deprecated as you would just make a block instead.

If you are on the latest WordPress and would like to use the classic widget functionality from ACF Composer, you will need to opt-out of the widget block editor.

Creating a sidebar widget using ACF Composer is extremely easy. Widgets are automatically loaded and rendered with Blade, as well as registered with WP_Widget which is usually rather annoying.

Start by creating a widget using Acorn:

$ wp acorn acf:widget Example
<?php

namespace App\Widgets;

use Log1x\AcfComposer\Widget;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Widget
{
    /**
     * The widget name.
     *
     * @var string
     */
    public $name = 'Example';

    /**
     * The widget description.
     *
     * @var string
     */
    public $description = 'Lorem ipsum...';

    /**
     * Data to be passed to the widget before rendering.
     *
     * @return array
     */
    public function with()
    {
        return [
            'items' => $this->items(),
        ];
    }

    /**
     * The widget title.
     *
     * @return string
     */
    public function title() {
        return get_field('title', $this->widget->id);
    }

    /**
     * The widget field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->addText('title');

        $example
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $example->build();
    }

    /**
     * Return the items field.
     *
     * @return array
     */
    public function items()
    {
        return get_field('items', $this->widget->id) ?: [];
    }
}

Similar to blocks, widgets are also accompanied by a view generated in resources/views/widgets.

@if ($items)
  <ul>
    @foreach ($items as $item)
      <li>{{ $item['item'] }}</li>
    @endforeach
  </ul>
@else
  <p>No items found!</p>
@endif

Out of the box, the Example widget is ready to go and should appear in the backend.

Generating an Options Page

Creating an options page is similar to creating a regular field group in additional to a few configuration options available to customize the page (most of which, are optional.)

Start by creating an option page using Acorn:

$ wp acorn acf:options Example
<?php

namespace App\Options;

use Log1x\AcfComposer\Options as Field;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Field
{
    /**
     * The option page menu name.
     *
     * @var string
     */
    public $name = 'Example';

    /**
     * The option page document title.
     *
     * @var string
     */
    public $title = 'Example | Options';

    /**
     * The option page field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->addRepeater('items')
                ->addText('item')
            ->endRepeater();

        return $example->build();
    }
}

Optionally, you may pass --full to the command above to generate a stub that contains additional configuration examples.

$ wp acorn acf:options Options --full

Once finished, you should see an Options page appear in the backend.

All fields registered will have their location automatically set to this page.

Custom Stub Generation

To customize the stubs generated by ACF Composer, you can easily publish the stubs directory using Acorn:

$ wp acorn acf:stubs

The publish command generates all available stubs by default. However, each stub file is optional. When a stub file doesn't exist in the stubs/acf-composer directory, the default stub provided by the package will be used.

Default Field Settings

One of my personal favorite features of ACF Composer is the ability to set field type as well as field group defaults. Any globally set default can of course be over-ridden by simply setting it on the individual field.

Global

Taking a look at config/acf.php, you will see a few pre-configured defaults:

'defaults' => [
    'trueFalse' => ['ui' => 1],
    'select' => ['ui' => 1],
],

When setting trueFalse and select to have their ui set to 1 by default, it is no longer necessary to repeatedly set 'ui' => 1 on your fields. This takes effect globally and can be overridden by simply setting a different value on a field.

Field Group

It is also possible to define defaults on individual field groups. This is done by simply defining $defaults in your field class.

/**
 * Default field type settings.
 *
 * @return array
 */
protected $defaults = ['ui' => 0];

My Defaults

Here are a couple defaults I personally use. Any prefixed with acfe_ are related to ACF Extended.

'defaults' => [
    'fieldGroup' => ['instruction_placement' => 'acfe_instructions_tooltip'],
    'repeater' => ['layout' => 'block', 'acfe_repeater_stylised_button' => 1],
    'trueFalse' => ['ui' => 1],
    'select' => ['ui' => 1],
    'postObject' => ['ui' => 1, 'return_format' => 'object'],
    'accordion' => ['multi_expand' => 1],
    'group' => ['layout' => 'table', 'acfe_group_modal' => 1],
    'tab' => ['placement' => 'left'],
    'sidebar_selector' => ['default_value' => 'sidebar-primary', 'allow_null' => 1]
],

Bug Reports

If you discover a bug in ACF Composer, please open an issue.

Contributing

Contributing whether it be through PRs, reporting an issue, or suggesting an idea is encouraged and appreciated.

License

ACF Composer is provided under the MIT License.

More Repositories

1

acf-builder-cheatsheet

A cheatsheet for use with ACF Builder.
262
star
2

navi

A developer-friendly alternative to the WordPress NavWalker.
PHP
253
star
3

sage-directives

A set of Blade directives for use with Roots Sage.
PHP
235
star
4

poet

Configuration-based post type, taxonomy, block category, and block registration for Sage 10.
PHP
148
star
5

sage-svg

A simple package for using inline SVGs with Sage 10.
PHP
91
star
6

acf-phone-number

A real ACF phone number field powered by libphonenumber and intl-tel-input
PHP
91
star
7

acf-editor-palette

A Gutenberg-like editor palette color picker field for Advanced Custom Fields.
PHP
80
star
8

blade-svg-sage

A simple package to add support for Blade SVG by Adam Wathan to Roots Sage.
PHP
71
star
9

modern-login

A whitelabeled and modernized wp-login.php
CSS
69
star
10

acf-field-boilerplate

Modernized PSR-2 boilerplate for creating custom fields for ACF5.
PHP
58
star
11

socialproof

A fluent interface for retrieving follower counts from social API's.
PHP
39
star
12

tailwindcss-pseudo

Tailwind CSS plugin to generate pseudo selector variants.
JavaScript
38
star
13

sage-html-forms

Create forms using HTMLForms.io and Sage 10 Blade components
PHP
37
star
14

pagi

A better WordPress pagination.
PHP
36
star
15

wp-smtp

Simple package for handling WordPress SMTP with .env when using the Roots stack.
PHP
33
star
16

acf-move-wp-editor

This is a simple ACF Field that moves the WordPress content editor of a post or page to the location of this field.
PHP
32
star
17

modern-acf-options

A modern approach to ACF Theme Options
CSS
30
star
18

crumb

A simple breadcrumb package for Sage 10.
PHP
29
star
19

bulma.styl

A Stylus translation of a modern CSS framework based on Flexbox
CSS
26
star
20

sage-password-protected

A simple password protection package for use with Roots Sage.
PHP
18
star
21

tailwindcss-container-sizes

Simple Tailwind plugin to generate container sizes
JavaScript
18
star
22

acf-fluent-helpers

Simple set of helper functions for ACF Fluent auto-loaded with Composer using generic function names.
PHP
14
star
23

wordpress-ui

Here lives a modernized approach to the WordPress Admin UI
CSS
14
star
24

sage-eject-blocks

Eject Sage 10 editor scripts to a optional lazy-loaded plugin.
PHP
13
star
25

plugin-meta

A simple meta package to install a few plugins I use regularly along with a couple related filters.
PHP
13
star
26

acf-blocks

PHP
12
star
27

docker-rtorrent-flood

Dockerfile
11
star
28

tailwindcss-border-styles

Tailwind CSS plugin to generate individual border side style classes.
JavaScript
8
star
29

acf-sidebar-selector

PHP
7
star
30

acf-encrypted-password

A better ACF Password Field that is encrypted using bcrypt.
PHP
7
star
31

gutentweaks

JavaScript
6
star
32

pomf

A simple implementation of the Pomf API.
PHP
4
star
33

acf-move-yoast

This is a simple ACF Field that moves the Yoast SEO metabox to the location of this field.
PHP
4
star
34

envoyer-deploy-commands

Simple deployment with Envoyer using Artisan
PHP
3
star
35

dotfiles

My personally used dotfiles on macOS
PHP
3
star
36

docker-mopidy-iris

Dockerfile
2
star
37

laravel-mix-jigsaw

Jigsaw's build tasks written as a Laravel Mix plugin.
JavaScript
2
star
38

nofollow-pretty-links

PHP
2
star
39

redirection-variables

PHP
2
star
40

wp-offload-media-avatars

A simple WP Media Offload integration for local avatar plugins.
PHP
2
star
41

rom-cli

A simple CLI tool to fetch prices from ROM Exchange
PHP
1
star
42

android

My personally used Android setup
1
star
43

allow-unsafe-links

Prevents WordPress from automatically adding noopener and noreferrer to outbound links.
PHP
1
star
44

acf-move-related-posts

This is a simple ACF Field that moves the Related Posts for WordPress metabox to the location of this field.
PHP
1
star
45

log1x.com

The current iteration of my personal website.
Vue
1
star
46

blade-filetype-icons

A package to easily make use of dmhendricks/file-icon-vectors in your Laravel Blade views.
PHP
1
star