Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to define custom acf types #30

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 48 additions & 61 deletions src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@
*/
class FieldFactory
{
protected static $defaultTypes = [
'text' => Text::class,
'textarea' => Text::class,
'number' => Text::class,
'email' => Text::class,
'url' => Text::class,
'password' => Text::class,
'wysiwyg' => Text::class,
'editor' => Text::class,
'oembed' => Text::class,
'embed' => Text::class,
'color_picker' => Text::class,
'select' => Text::class,
'checkbox' => Text::class,
'radio' => Text::class,
'image' => Image::class,
'img' => Image::class,
'file' => File::class,
'gallery' => Gallery::class,
'true_false' => Boolean::class,
'boolean' => Boolean::class,
'post_object' => PostObject::class,
'post' => PostObject::class,
'relationship' => PostObject::class,
'page_link' => PageLink::class,
'taxonomy' => Term::class,
'term' => Term::class,
'user' => User::class,
'date_picker' => DateTime::class,
'date_time_picker' => DateTime::class,
'time_picker' => DateTime::class,
'repeater' => Repeater::class,
'flexible_content' => FlexibleContent::class,
];

private function __construct()
{
}
Expand All @@ -40,7 +75,7 @@ public static function make($name, Model $post, $type = null)
{
if (null === $type) {
$fakeText = new Text($post);
$key = $fakeText->fetchFieldKey($name);
$key = $fakeText->fetchFieldKey($name);

if ($key === null) { // Field does not exist
return null;
Expand All @@ -49,69 +84,21 @@ public static function make($name, Model $post, $type = null)
$type = $fakeText->fetchFieldType($key);
}

$customTypes = [];

switch ($type) {
case 'text':
case 'textarea':
case 'number':
case 'email':
case 'url':
case 'password':
case 'wysiwyg':
case 'editor':
case 'oembed':
case 'embed':
case 'color_picker':
case 'select':
case 'checkbox':
case 'radio':
$field = new Text($post);
break;
case 'image':
case 'img':
$field = new Image($post);
break;
case 'file':
$field = new File($post);
break;
case 'gallery':
$field = new Gallery($post);
break;
case 'true_false':
case 'boolean':
$field = new Boolean($post);
break;
case 'post_object':
case 'post':
case 'relationship':
$field = new PostObject($post);
break;
case 'page_link':
$field = new PageLink($post);
break;
case 'taxonomy':
case 'term':
$field = new Term($post);
break;
case 'user':
$field = new User($post);
break;
case 'date_picker':
case 'date_time_picker':
case 'time_picker':
$field = new DateTime($post);
break;
case 'repeater':
$field = new Repeater($post);
break;
case 'flexible_content':
$field = new FlexibleContent($post);
break;
default: return null;
if (class_exists('\Config') && \Config::has('corcel.acf')) {
$customTypes = \Config::get('corcel.acf')['custom_types'];
}

$field->process($name);
$types = array_merge(self::$defaultTypes, $customTypes);

if (!empty($types[$type])) {
$field = new $types[$type]($post);
$field->process($name);

return $field;
}

return $field;
return null;
}
}
32 changes: 32 additions & 0 deletions src/Laravel/CorcelAcfServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Corcel\Acf\Laravel;

use Illuminate\Support\ServiceProvider;

/**
* Class CorcelAcfServiceProvider
* @package Corcel\Acf\Laravel
*/
class CorcelAcfServiceProvider extends ServiceProvider
{
/**
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/config.php' => config_path('corcel.php'),
], 'config');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}
}
11 changes: 11 additions & 0 deletions src/Laravel/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Corcel\Acf\Field\Text;

return [
'acf' => [
'custom_types' => [
'forms' => Text::class
]
]
];