corcel / acf

Advanced Custom Fields (ACF) plugin for Corcel
127 stars 100 forks source link

ACF Options page #48

Open omzy opened 7 years ago

omzy commented 7 years ago

How do we retrieve values from an Options page?

https://www.advancedcustomfields.com/resources/options-page/

I added a gallery field within an options page, when I do Option::get('gallery') it outputs an array (which is correct), however that array only contains key => value pairs, like this:

image

How do I get the actual images?

jgrossi commented 7 years ago

@omzy83 no no, for ACF stuff only use $post->acf->gallery('field') not Option::get(). You should receive a collection of images ;-)

omzy commented 7 years ago

@jgrossi I think you misunderstood...

I created the gallery in the Options page because it will be used on different pages on the site. I.e the same gallery with the same images reused across different pages.

jgrossi commented 7 years ago

@omzy83 hum good call, I don't think we already have get_option() covered... it's easy to implement but I think it's not working yet...

omzy commented 7 years ago

As a workaround for now, I have done this which seems to work:

<?php

namespace App\Models;

use App\Models\Post;

class Option extends \Corcel\Model\Option
{
    public static function getGallery()
    {
        // 'options_gallery' is the name of my Options page field
        $gallery_option = self::get('options_gallery');

        $gallery = [];

        foreach ($gallery_option as $index => $id) {
            $post = Post::find($id);
            $image = $post->url;

            $gallery[] = $image;
        }

        return $gallery;
    }
}
jgrossi commented 7 years ago

@omzy83 cool, but maybe we can think in a general method to handle this.

OwenMelbz commented 6 years ago

@jgrossi hey has this moved forwards at all? would really like to use this in a active project!

jgrossi commented 6 years ago

@OwenMelbz not actually, maybe someone can create a PR for that?

tbruckmaier commented 6 years ago

I am currently working on a pr for this, unfortunately it requires a bunch of changes. The more complex fields like repeater or even image need to retrieve additional data from wp_posts and wp_postmeta. The logic for this is currently in the single field classes. The acf option page stores its data in wp_options though, that's why its not working with the current approach. My approach is to move the logic for additional fields to a new layer (called "repository" now in lack of a better name), so we have a PostRepository (with the current logic from the single Field classes) and a new OptionPageRepository. The single fields do interact with the database only through their repository, so we should be fine there.