tiny-pixel/fields-composer

Streamlines the processing of getting ACF values into Sage 10 view composers

Fund package maintenance!
Patreon

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 3

Forks: 0

Open Issues: 1

Type:package

dev-master 2019-07-14 21:42 UTC

This package is auto-updated.

Last update: 2024-04-15 03:27:48 UTC


README

Fields Composer is a package intended for use with Sage 10. It provides cached Advanced Custom Fields data to Sage view composers.

Requirements

Sage >= 10.0

PHP >= 7.3

Composer

Installation

Install via composer:

composer require tiny-pixel/fields-composer

Usage

In app/Composers create a new view composer which extends TinyPixel\FieldsComposer\FieldsComposer:

<?php

namespace App\Composers;

use \TinyPixel\FieldsComposer\FieldsComposer;
use \Illuminate\View\View;

class FieldsComposerDemo extends FieldComposer
{
  // ...
}

This works exactly like a normal Sage composer but has out of the box support for Advanced Custom Fields and leverages laravel's Cache facade.

You can access fields using $this->fields() and set an expiration for the fields cache like so:

  // ...

  /**
   * Expiration time of cache in seconds
   *
   * @var int
   */
  public $cacheExpiry = 3600;

  /**
   * Data to be passed to view before rendering.
   *
   * @param  array $data
   * @param  \Illuminate\View\View $view
   * @return array
   */
  public function with($data, $view)
  {
    return $data = [
      'myFields' => $this->fields(),
      'myGroup' => $this->fields('myGroup'),
      'myItem' => $this->fields('myGroup')->item,
      'mySubItem' => $this->fields('myGroup')->item['subItem'],
    ];
  }

  //...

Configure

By default fields are cached to storage/framework/cache/data using the filesystem strategy, but you can change that in config/cache.php (if you want to utilize the database, memcached, redis, etc.) This file should have been copied to your project on install.

Notes

Currently this plugin flushes the entire Laravel Cache when any content is published, edited, deleted, etc. This is true even if the cache is set to 0 in the view composer. This is important to note if you are utilizing the Illuminate Cache elsewhere in your application.