htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.38k stars 245 forks source link

[Help Wanted] Custom container conditions documentation improvement #422

Open vanilium opened 6 years ago

vanilium commented 6 years ago

Hi, I have problem with define custom condition.

In documentation says about example "Foobar_Condition" conditions, and below exemplify file Foobar_Condition.php, but no anywhere says about where this file must be, where included, and how \Carbon_Fields\Carbon_Fields must find this class.

will be glad get correct solution how do it

Thanks

atanas-dev commented 6 years ago

Hi @vanilium ,

It seems our documentation on the subject is a bit barebones so thanks for letting us know 👍 (I'll keep this ticket open until we improve it)

As for your questions:

but no anywhere says about where this file must be

The file can be anywhere with any namespace - it doesn't matter. The only thing that matters is that the class file is loaded with include_once, require_once or with any autoloading.

how \Carbon_Fields\Carbon_Fields must find this class.

The first argument in following line specifies which class Carbon Fields should use:

\Carbon_Fields\Carbon_Fields::extend( Foobar_Condition::class, function( $container ) { ...

Whenever Carbon Fields needs to use the new condition, it will create a fresh instance using the passed instantiation function (the second argument to Carbon_Fields::extend()).

The condition type is automatically detected based on your condition class name so:

With this Carbon Fields now knows everything it needs:

vanilium commented 6 years ago

Hm, when a include file Foobar_Condition.php anywhere, I got fatal error "Class 'Condition' not found". If I add "use Carbon_Fields\Container\Condition", i got "Class 'Carbon_Fields\Container\Condition' not found". If I declare "namespace Carbon_Fields\Container\Condition;", have next error "Uncaught ReflectionException: Class Foobar_Condition does not exist". Including and extension condition I call use action "after_setup_theme"

P.S: I'm use composer install, not as plugin.

atanas-dev commented 6 years ago

The use statement should be as follows:

use Carbon_Fields\Container\Condition\Condition; // notice the double Condition\Condition

If after this change you still get Class Foobar_Condition does not exist then this means that your custom class is defined too late.

vanilium commented 6 years ago

Thanks @atanas-angelov-dev I resolve my issue, have another mistake and resolved it

Thanks for your time

vanilium commented 6 years ago

Thanks @atanas-angelov-dev I resolve my issue, have another mistake and resolved it

Thanks for your time

vanilium commented 6 years ago

And next question: have any way expand condition for dynamic use, like page template or page parent?

My situation is next: I have post type with custom meta box, where shows some controls (like template or page parent), and i want change visibility state of some cf metaboxes use this condition.

atanas-dev commented 6 years ago

Here's a list of all supported conditions which include post_template and post_parent_id: https://carbonfields.net/docs/containers-condition-types/?crb_version=2-1-0

Carbon Fields currently does not officially support defining your own custom "dynamic" conditions (that do not require you to save the post before showing/hiding) - only static ones.

vanilium commented 6 years ago

Ok, I understand.

Thanks again for help

viterzbayraku commented 5 years ago

Sorry, but I have 500 error with include_once("Foobar_Condition.php") and with code, started from "\Carbon_Fields\Carbon_Fields::extend( Foobar_Condition::class, function( $container ) {"