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

4.2.2 2024-12-11 09:11 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')

Email

{{ 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']