composer require tombroucke/acf-objects
- In sage 10, Acorn should be able to locate src/AcfObjectsServiceProvider.php. If it doesn't, you should add this provider to the providers array in /config/app.php:
Otomaties\AcfObjects\AcfObjectsServiceProvider::class
- To use this library without Acorn, use this snippet:
add_filter('acf/format_value', function ($value, $post_id, $field) {
$value = \Otomaties\AcfObjects\Acf::findClassByFieldType($value, $post_id, $field);
return $value;
}, 99, 3);
Wherever you want to use ACF Objects, import the Otomaties\AcfObjects\Acf class:
<?php use Otomaties\AcfObjects\Acf; ?>
<?php if(Acf::getField('fieldname')->isSet()): ?>
<?php endif; ?>
<?php if(!Acf::getField('fieldname')->isEmpy()): ?>
<?php endif; ?>
<?php echo Acf::getField('date')->format('d/m/Y'); ?>
<?php echo Acf::getField('file')->url(); ?>
<?php echo Acf::getField('file')->title(); ?>
<?php echo Acf::getField('file')->filesize(); ?>
<ul>
<?php foreach (Acf::getField('gallery') as $image): ?>
<li><?php echo $image->attributes(['class' => 'd-none'])->image(); ?></li>
<?php endforeach; ?>
</ul>
<?php echo Acf::getField('gallery')[1]->image(); ?>
<?php echo Acf::getField('google_map')->address(); ?>
<?php echo Acf::getField('google_map')->lat(); ?>
<?php echo Acf::getField('google_map')->lat(); ?>
...
<?php echo Acf::getField('group')->get('text'); ?>
<?php echo Acf::getField('image')->url('medium'); ?>
<?php echo Acf::getField('image')->image('medium'); ?>
<a href="<?php echo Acf::getField('image')->url('full'); ?>">
<?php echo Acf::getField('image')->attributes(['class' => 'w-100'])->image('thumbnail'); ?>
</a>
<?php echo Acf::getField('image')->default(48, 'thumbnail')->image('thumbnail'); ?>
<?php echo Acf::getField('image')->default('https://picsum.photos/150/150')->image('thumbnail'); ?>
<?php echo Acf::getField('link')->link(); // Will output an a-tag with href, target & title ?>
<?php echo Acf::getField('link')->attributes(['class' => 'btn btn-primary','data-foo' => 'bar'])->link(); ?>
<a href="<?php echo Acf::getField('link')->url(); ?>" target="<?php echo Acf::getField('link')->target(); ?>">
<?php echo Acf::getField('link')->title(); ?>
</a>
<table class="table">
<?php foreach (Acf::getField('repeater') as $key => $row): ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $row->get('text'); ?></td>
<td><?php echo $row->get('image')->image('thumbnail'); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php if (isset(Acf::getField('repeater')[0])): ?>
<?php echo Acf::getField('repeater')[0]->get('text'); ?>
<?php endif; ?>
<?php echo Acf::getField('text'); ?>
<?php echo Acf::getField('text')->default('Default text'); ?>
<?php echo Acf::getField('text', 51); ?>
During development of this package, I stumbled upon ACF Fluent by samrap, which works great. The biggest difference between ACF Fluent and this package, is the ability to easily display images: Acf::getField('image')->image('thumbnail')
and a more intuitive way to iterate over repeater & gallery fields.