1. Go to this page and download the library: Download tbruckmaier/corcel-acf library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tbruckmaier / corcel-acf example snippets
use \Corcel\Models\Post as BasePost;
use Tbruckmaier\Corcelacf\AcfTrait;
class Post extends BasePost
{
use AcfTrait;
public static function boot()
{
self::addAcfRelations(['title', 'thumbnail']);
parent::boot();
}
}
use Corcel\Models\Post;
use Corcel\Models\Attachment;
use Tbruckmaier\Corcelacf\Models\Text;
use Tbruckmaier\Corcelacf\Models\Image;
$post = Post::find(1);
// post has a text field named "title" and a image field called "thumbnail"
$post->acf_title; // an instance of Text::class
$post->acf_title->value; // "Example title"
$post->acf_thumbnail; // Image::class
$post->acf_thumbnail->value; // an instance of Attachment::class representing the specified image
$post->acf->title; // (string) the parsed value, for instance "Example Page #1"
$post->acf->title(); // an instance of the underlying Text::class
$post->acf->title()->value; // the parsed value, $post->acf->title is a short version of this
$post->acf->title()->internal_value; // the unparsed value, for text's this is the same as value
$post->acf->title()->config; // the acf field config array defined in wordpress, sth like ['type' => 'text', 'instructions' => 'The site title', ...]
$post->acf->thumbnail; // an instance of Attachment::class representing the specified image
$post->acf->thumbnail(); // an instance of the underlying Image::class
$post->acf->thumbnail()->value; // again the same Attachment::class
$post->acf->thumbnail()->internal_value; // the unparsed value from the `postmeta` table, in this case the attachment id
$post->acf->thumbnail()->config; // the thumbnail acf field config array (['type' => 'image', ...])
// laravel model
use \Corcel\Models\Post as BasePost;
use Tbruckmaier\Corcelacf\AcfTrait;
class Post extends BasePost
{
use AcfTrait;
public static function boot()
{
self::addAcfRelations([
'field_1' => [
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
],
]);
parent::boot();
}
}
// functions.php:
// no parameters result in the prefix "options"
acf_add_options_page();
// another option page with a different prefix
acf_add_options_page([
'post_id' => 'additional-options',
]);
// laravel:
use Tbruckmaier\Corcelacf\OptionPage;
// get the option page's field group by id, take it from the url for instance
$optionPage = OptionPage::find(1016);
// ... or find it by its title. This is not the title given to acf_add_options_page(), but the field group name.
$optionPage1 = OptionPage::byTitle('Page option fields')->first();
// load the option data from the database
$optionPage->loadOptions();
// alternatively with a custom prefix
$optionPage1->loadOptions('additional-options');
// get a option
$pageTitle = $optionPage->getOption('page-title'); // "My page"
// or the underlying Field
$pageTitle = $optionPage->getOptionField('page-title'); // Text::class
// works with all fields:
$myRepeater = $optionPage1->getOption('my-repeater'); // Collection
$myRepeater->first()->text; // "Entry #1"
// config/corcel-acf.php
'classMapping' => [
'text' => CustomText::class,
'google_maps' => GoogleMapsField::class,
]
// CustomText.php
class CustomText extends \Tbruckmaier\Corcelacf\BaseField
{
public function getValueAttribute()
{
return htmlentities($this->internal_value);
}
public function getWordsAttribute()
{
return explode(' ', $this->internal_value);
}
}
// Usage
$post->acf->my_text_field(); // CustomText::class
$post->acf->my_text_field; // "one & two"
$post->acf->my_text_field()->words; // ["one", "&", "two"]
use Corcel\Models\Post as BasePost;
use Tbruckmaier\Corcelacf\AcfTrait;
class Post extends BasePost
{
use AcfTrait;
public function thumbnail()
{
return $this->hasAcf('thumbnail');
}
}
$post = Post::find(1);
$post->thumbnail; // Image::class
$post->thumbnail->value; // Attachment
$posts = Post::all()->load('acf_thumbnail');
use Corcel\Models\Post;
use Tbruckmaier\Corcelacf\Models\Text;
use Tbruckmaier\Corcelacf\Models\Repeater;
use Tbruckmaier\Corcelacf\Models\FlexibleContent;
use Tbruckmaier\Corcelacf\Support\RepeaterLayout;
use Tbruckmaier\Corcelacf\Support\FlexibleContentLayout;
$post = Post::find(1);
$post->acf->main_repeater(); // Repeater
$repeaterFields = $post->acf->main_repeater; // Collection of RepeaterLayout
$repeaterFields->first()->title(); // Text::class
$repeaterFields->first()->title; // parsed response "Main repeater title #1"
$repeaterFields->get(1)->title(); // Text::class
$repeaterFields->get(1)->title; // "Main repeater title #2"
$post->acf->main_content(); // FlexibleContent
$fcLayouts = $post->acf->main_content; // Collection of FlexibleContentLayout
$fcLayouts->get(0)->getType(); // layout type of the first block, for example "text_with_image"
$fcLayouts->get(0)->text(); // Text::class
$fcLayouts->get(0)->text; // "Text of the first content block"
$fcLayouts->get(0)->image(); // Image::class
$fcLayouts->get(0)->image; // Attachment::class (linked image)
$fcLayouts->get(1)->getType(); // layout type of the second block, for example "accordion"
$fcLayouts->get(1)->accordion_title; // "Accordion #1"
$fcLayouts->get(1)->accordion_items(); // Repeater::class
$fcLayouts->get(1)->accordion_items; // Collection of RepeaterLayouts
$fcLayouts->get(1)->accordion_items->first()->title; // "First accordion element"
$fcLayouts->get(1)->accordion_items->first()->content; // "First accordion content..."
use Tbruckmaier\Corcelacf\Models\Repeater;
$post = Post::find(1);
// fires 2 queries per iteration
foreach ($post->acf->main_repeater as $layout) {
$layout->foo_image->attachment;
$layout->bar_image->attachment;
}
// preloads all attachment relations, fires 2 queries in total
foreach ($post->acf->main_repeater()->load('foo_image.attachment', 'bar_image.attachment')->value as $layout) {
$layout->foo_image->attachment;
$layout->bar_image->attachment;
}
use Corcel\Models\Post;
use Tbruckmaier\Corcelacf\Models\Group;
use Tbruckmaier\Corcelacf\Models\Text;
use Tbruckmaier\Corcelacf\Models\Repeater;
use Tbruckmaier\Corcelacf\Support\GroupLayout;
$post = Post::find(1);
$post->acf->header_fields(); // Group
$post->acf->header_fields; // GroupLayout
$post->acf->header_fields->title; // "site title"
$post->acf->header_fields->title(); // Text::class
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.