tombroucke / acf-objects
Convert return values from get_field() to objects with easy-to-use methods
Installs: 5 503
Dependents: 3
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 2
Open Issues: 0
Type:package
Requires
- php: ^8.0
- illuminate/support: *
- nesbot/carbon: *
- spatie/ray: ^1.41
Requires (Dev)
- laravel/pint: ^1.18
- php-stubs/acf-pro-stubs: ^6.2
- phpunit/phpunit: ^11.4
- szepeviktor/phpstan-wordpress: ^1.3
This package is auto-updated.
Last update: 2024-12-11 09:13:04 UTC
README
This package converts the return values of ACF to objects with easy-to-use methods.
Instead of calling get_field('selector')
, you can use the AcfObjects facade: AcfObjects::getField('selector')
Installation
composer require tombroucke/acf-objects
You might have to clear wp acorn cache: wp acorn optimize:clear
Usage
Checkbox
When getting the value for a Checkbox field, an Illuminate/Support/Collection
will be returned.
$checkboxValues = AcfObjects::getField('checkbox');
ColorPicker
{{ AcfObjects::getField('color_picker') }}
DatePicker
When getting the value for a DatePicker field, a Carbon
instance will be returned. If the field has no value, a FallbackField
will be returned.
AcfObjects::getField('date')
@if(AcfObjects::getField('date')->isSet()) AcfObjects::getField('date')->format(get_option('date_format)) @endif
DateTimePicker
When getting the value for a DateTimePicker field, a Carbon
instance will be returned. If the field has no value, a FallbackField
will be returned.
AcfObjects::getField('date_time')
{{ AcfObjects::getField('email')->obfuscate() }}
File
{{ AcfObjects::getField('file')->url() }} {{ AcfObjects::getField('file')->title() }} {{ AcfObjects::getField('file')->filesize() }}
Gallery
When getting the value for a Group field, an Illuminate/Support/Collection
will be returned.
@foreach (AcfObjects::getField('gallery') as $image) <a href="{{ $image->url('large') }}"> {!! $image->image('medium') !!} </a> @endforeach
Google Maps
{{ AcfObjects::getField('google_map')->address() }} {{ AcfObjects::getField('google_map')->lat() }} {{ AcfObjects::getField('google_map')->long() }}
Group
$settings = AcfObjects::getField('settings') ->default([ 'foo' => 'bar' ]); echo $settings->get('foo');
{{ AcfObjects::getField('settings')->get('name') }}
Image
{!! AcfObjects::getField('image') ->url('medium'); !!} {!! AcfObjects::getField('image') ->attributes(['class' => 'w-100 h-100 object-fit-cover']) ->image('thumbnail'); !!} {!! AcfObjects::getField('image') ->default(asset('image/placeholder.jpg')->uri()) ->image('thumbnail'); !!}
Link
{{ AcfObjects::getField('link')->url() }} @php $link = AcfObjects::getField('link'); @endphp @if($link->isSet()) <a href="{{ $link->url() }}" target="{{ $link->target() }}"> {{ $link->title() }} </a> @endif // or @if($link->isSet()) {!! $link->link() !!} @endif
Number
{{ AcfObjects::getField('number') }}
Repeater
When getting the value for a Repeater field, an Illuminate/Support/Collection
will be returned.
AcfObjects::getField('repeater')
@unless(AcfObjects::getField('repeater')->isEmpty()) <ul> @foreach(AcfObjects::getField('repeater') as $item) <li>{!! $item['name'] !!}</li> @endforeach </ul> @endunless
Text
{{ AcfObjects::getField('text')->default(get_the_title()) }}
TextArea
{{ AcfObjects::getField('text_area') }}
Upgrading to v4.x from v3.x
Use new facade
- Acf::getField( + AcfObjects::getField(
Use new facade and remove deprecated get_field method
- Acf::get_field( + AcfObjects::getField(
Use new facade
- use Otomaties\AcfObjects\Acf; + use Otomaties\AcfObjects\Facades\AcfObjects;
Array access for repeaters instead of the get()
method.
- $repeater->get('sub_field') + $repeater['sub_field']