StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
796 stars 62 forks source link

ACF blocks are available on pages but not posts #132

Open wearebattalion opened 3 years ago

wearebattalion commented 3 years ago

I've created a number of ACF groups (example below) as gutenberg blocks which I need to use across pages and posts, however the blocks are only available in pages and not posts, and they don't even appear when I search for them.

I've included an example of an ACF group I've created as a block below:

<?php

namespace App;
use StoutLogic\AcfBuilder\FieldsBuilder;

$newsletter = new FieldsBuilder('newsletter');

$newsletter
    ->addText('heading')
    ->addWysiwyg('content')
    ->setLocation('block', '==', 'acf/newsletter');

acf_add_local_field_group($newsletter->build());

functions.php

add_action('init', function () {
  if (function_exists('acf_add_local_field_group')) {
      // Gutenberg blocks
      collect(glob(config('theme.dir') . '/app/fields/blocks/*.php'))->map(function ($field) {
          return require_once($field);
      })->map(function ($field) {
          if ($field instanceof FieldsBuilder) {
              acf_add_local_field_group($field->build());
          }
      });
  }
});

Just to confirm, I need this block to be available on posts as well as pages.

stevep commented 3 years ago

I haven't used blocks, nor have I tried to use ACF with blocks, so my knowledge on the requirements of getting them working is very small.

Where is the block itself getting defined, when I search ACF's docs, there is a function called acf_register_block_type, and in that you can pass the post types which it should be available to:

https://www.advancedcustomfields.com/resources/acf_register_block_type/#parameters

cheestudio commented 3 years ago

Just going to leave this here, but we have a lightweight "framework" that works seamlessly with ACF Builder and ACF Blocks, in case anybody wants to use it OR pick it apart for their own use! 👍🏻 It will auto register blocks that are located within a certain folder, and has built in template toggles for determining which post types the block will be available on.

https://github.com/cheestudio/startup-blocks