AdvancedCustomFields / AdvancedCustomFields/acf

Key piece of info missing in the 6.0 docs about rendering block support utility classes

Open
#716 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
945
Forks
197
PR merge metrics
No merged PRs in 30d

Description

First and foremost; I really love what the team has done with ACF 6.0. Yet, I noticed one little thing that has been left out of the docs and tutorials shown here and on ACF site.

I'm pretty experienced with block themes and theme.json, and when I read that wp's native supports and attributes are now available, I was very excited. It works great in the editor, but then on front-end: no utility class in the block!

Yet, I have not found any statements about this. Just adding $className doesn't add the new attributes to the block. We have to retrieve the blocks properties by code and add them to the block class for the front end. Makes total sense, but not for everyone.

I'm quite sure that devs who don't know theme.json and utility classes very well, would benefit from adding this crucial piece of info to the dev notes / guides.

To solve the problem, I've written a bit of code for this, maybe it helps!

function acf_get_block_class($block, $className = false) {

  $classNames = [];

  if ( $className ) {
    $classNames = [$className];
  }

  if ( !empty($block['backgroundColor']) ) {
    $classNames[] = 'has-background has-' . $block['backgroundColor'] . '-background-color';
  }

  if ( !empty($block['textColor']) ) {
    $classNames[] = 'has-text-color has-' . $block['textColor']. '-text-color';
  }

  if ( !empty($block['align']) ) {
    $classNames[] = 'align' . $block['align'];
  }

  if( !empty($block['alignText']) ) {
    $className .= ' has-text-align-' . $block['alignText'];
  }

  if( !empty($block['alignContent']) ) {
    $className .= ' is-vertically-aligned-' . $block['alignContent'];
  }

  return implode( ' ', $classNames );
}

// then on the block:

$className = 'acf-block'; 
// add your className logic here
echo '<div class="' . hum_get_block_class( $block, $className ) . '">'

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the 6.0 dev notes and guides about block supports, theme.json, and front-end rendering. Document that editor support attributes do not automatically produce front-end utility classes, include the relevant block properties and the reported helper example, and verify that the guidance clearly explains how developers can render those classes.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.