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
Changes from 1 commit
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
104 changes: 46 additions & 58 deletions src/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,65 +49,53 @@ public static function make($name, Model $post, $type = null)
$type = $fakeText->fetchFieldType($key);
}

$default_types = [
Copy link
Member

@jgrossi jgrossi Apr 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to an class variable (static):

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,
];

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')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to check if the Config class exists before using it. That's the first error when running phpunit. I suggest you to check if the class exists or if the config() function exists like I did on Model.php class. Please, take a look.

$custom_types = \Config::get('corcel.acf.custom_types');
}

$types = array_merge($default_types, $custom_types);

if (!empty($types[$type])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest here:

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

    return $field;
}

return null;

$field = new $types[$type]($post);
} else {
return null;
}

$field->process($name);
Expand Down