From 6695718acd733c32a3708ab1aab2d63aeebad023 Mon Sep 17 00:00:00 2001 From: Kevin Kiel Date: Tue, 25 Apr 2017 11:39:40 +0200 Subject: [PATCH 1/4] Make it possible to define custom acf types --- src/FieldFactory.php | 104 +++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 58 deletions(-) diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 13d881f..8da22b6 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -49,65 +49,53 @@ public static function make($name, Model $post, $type = null) $type = $fakeText->fetchFieldType($key); } + $default_types = [ + '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, + ]; - 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; + $custom_types = []; + + if (\Config::has('corcel.acf.custom_types')) { + $custom_types = \Config::get('corcel.acf.custom_types'); + } + + $types = array_merge($default_types, $custom_types); + + if (!empty($types[$type])) { + $field = new $types[$type]($post); + } else { + return null; } $field->process($name); From a7cf6b50015444e6403f890f0e58ce9a5b660ed7 Mon Sep 17 00:00:00 2001 From: Kevin Kiel Date: Fri, 28 Apr 2017 20:47:14 +0200 Subject: [PATCH 2/4] Some cleanup / Extra checks --- src/FieldFactory.php | 89 ++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/FieldFactory.php b/src/FieldFactory.php index 8da22b6..7bad441 100644 --- a/src/FieldFactory.php +++ b/src/FieldFactory.php @@ -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() { } @@ -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; @@ -49,57 +84,21 @@ public static function make($name, Model $post, $type = null) $type = $fakeText->fetchFieldType($key); } - $default_types = [ - '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, - ]; - - $custom_types = []; + $customTypes = []; - if (\Config::has('corcel.acf.custom_types')) { - $custom_types = \Config::get('corcel.acf.custom_types'); + if (class_exists('\Config') && \Config::has('corcel.acf')) { + $customTypes = \Config::get('corcel.acf')['custom_types']; } - $types = array_merge($default_types, $custom_types); + $types = array_merge(self::$defaultTypes, $customTypes); if (!empty($types[$type])) { $field = new $types[$type]($post); - } else { - return null; - } + $field->process($name); - $field->process($name); + return $field; + } - return $field; + return null; } } From 11e3c764d7bf684c705f74842075b35eb11dfd4a Mon Sep 17 00:00:00 2001 From: Kevin Kiel Date: Tue, 2 May 2017 21:02:49 +0200 Subject: [PATCH 3/4] Add a config service provider --- src/Laravel/CorcelAcfServiceProvider.php | 32 ++++++++++++++++++++++++ src/Laravel/config.php | 11 ++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/Laravel/CorcelAcfServiceProvider.php create mode 100644 src/Laravel/config.php diff --git a/src/Laravel/CorcelAcfServiceProvider.php b/src/Laravel/CorcelAcfServiceProvider.php new file mode 100644 index 0000000..1e042e9 --- /dev/null +++ b/src/Laravel/CorcelAcfServiceProvider.php @@ -0,0 +1,32 @@ +publishes([ + __DIR__ . '/config.php' => config_path('corcel.php'), + ], 'config'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + // + } +} diff --git a/src/Laravel/config.php b/src/Laravel/config.php new file mode 100644 index 0000000..af5b363 --- /dev/null +++ b/src/Laravel/config.php @@ -0,0 +1,11 @@ + [ + 'custom_types' => [ + 'forms' => Text::class + ] + ] +]; \ No newline at end of file From 2fac773e80a4af262b9225175facd1e68d0a7d94 Mon Sep 17 00:00:00 2001 From: Kevin Kiel Date: Tue, 2 May 2017 21:04:48 +0200 Subject: [PATCH 4/4] Add extra enter --- src/Laravel/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Laravel/config.php b/src/Laravel/config.php index af5b363..08f0a45 100644 --- a/src/Laravel/config.php +++ b/src/Laravel/config.php @@ -8,4 +8,4 @@ 'forms' => Text::class ] ] -]; \ No newline at end of file +];