Played around with ACF Pro for the first time. Here’s some directives I came up with:
/**
* Advanced Custom Fields Blade directives
*/
/**
* Create @fields() Blade directive
*/
sage('blade')->compiler()->directive('fields', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
$output = "<?php if (have_rows($expression)) : ?>";
$output .= "<?php while (have_rows($expression)) : ?>";
$output .= "<?php the_row(); ?>";
return $output;
});
/**
* Create @endFields Blade directive
*/
sage('blade')->compiler()->directive('endFields', function () {
return "<?php endwhile; endif; ?>";
});
/**
* Create @field() Blade directive
*/
sage('blade')->compiler()->directive('field', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php the_field($expression); ?>";
});
/**
* Create @getField() Blade directive
*/
sage('blade')->compiler()->directive('getField', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php get_field($expression); ?>";
});
/**
* Create @hasField() Blade directive
*/
sage('blade')->compiler()->directive('hasField', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php if (get_field($expression)) : ?>";
});
/**
* Create @endField Blade directive
*/
sage('blade')->compiler()->directive('endField', function () {
return "<?php endif; ?>";
});
/**
* Create @sub() Blade directive
*/
sage('blade')->compiler()->directive('sub', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php the_sub_field($expression); ?>";
});
/**
* Create @getSub() Blade directive
*/
sage('blade')->compiler()->directive('getSub', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php get_sub_field($expression); ?>";
});
/**
* Create @hasSub() Blade directive
*/
sage('blade')->compiler()->directive('hasSub', function ($expression) {
$expression = strtr($expression, array('(' => '', ')' => ''));
return "<?php if (get_sub_field($expression)) : ?>";
});
/**
* Create @endSub Blade directive
*/
sage('blade')->compiler()->directive('endSub', function () {
return "<?php endif; ?>";
});
@fields() Usage
<ul>
@fields('links')
@hasSub('title')
<li>@sub('title')</li>
@endSub
@hasSub('url')
<li>@sub('url')</li>
@endSub
@endFields
</ul>
@field() Usage
@hasField('test')
@field('test')
@endField
Not shown in my example is @getSub()
and @getField()
allowing you to return the values instead of echoing them.
Shoutout to @Daniel_Willitzer for providing me with ACF Pro so I could create these.
Any suggestions/modifications are welcome. I made these in the last 15 minutes and this is the first time I’ve ever used ACF, so there may be a more effective way to do these.
One thing would be to combine @hasField
and @endField
to make it a bit easier in certain use cases. The main reason I separated them were for styling purposes (i.e. wrapping @field()
in <li></li>
– obviously you don’t want the markup appearing if it doesn’t exist…)
I also learned that you have to strip the parenthesis when passing an $expression
to a Blade directive. Kind of strange… This makes array
's a little hard to accomplish. For that, it looks like the common thing to do is pass an array with comma’s and then explode
the $expression
variable and using it inside of the directive such as $expression = explode(',', $expression)
. You can see a few examples here of people doing that.
It’s not pretty, but in this case where I’m only passing string’s I simply did:
$expression = strtr($expression, array('(' => '', ')' => ''));
That being said, I will probably refactor the other directives I posted a few posts up to accommodate this.