Here are some examples of how we can use CMS.
You can only use sibling field values as conditional field keys.
Toggle button group visibility with a direct sibling field:
class Cookie extends Module {
public static function getFields() {
return [
'show_button' => new TrueFalse(['label' => 'Should show button?']),
'button' => new Group([
'text' => new Text(),
],
[
'conditional_logic' => [
// or group - one of first level arrays needs to be truthy to show field
[
// and group - all second level conditions need to be truthy to show field
[
// field value is the field key you've defined
'field' => 'show_button',
'operator' => '==',
'value' => '1'
]
]
],
]),
];
}
}
Toggle button group visibility with a nested field in a sibling group field:
class Cookie extends Module {
public static function getFields() {
return [
'content' => new Wysiwyg(),
'show_button_group' => new Group([
'show_button' => new TrueFalse(['label' => 'Should show button?']),
]),
'button' => new Group([
'text' => new Text(),
],
[
'conditional_logic' => [
// can be single level array, if depends on only one field value
// field can be array, must be in correct order (from outside in)
'field' => ['show_button_group', 'show_button'],
'operator' => '==',
'value' => '1'
],
]),
];
}
}
<?php
namespace gotoAndPlay\Templates;
use gotoAndPlay\Models\Page;
use gotoAndCore\Fields\GoogleMap;
use gotoAndCore\Fields\GravityForm;
class Contact extends Page {
protected function getView() {
return '@view-contact';
}
public static function getFields() {
return [
'map' => new GoogleMap(),
'form' => new GravityForm(),
];
}
}
When using under a model all the fields will be automatically added under the models post type.
post
)<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Fields\Relationship;
class Article extends Post {
protected function getView() {
return '@view-article';
}
public static function getFields() {
return [
'relatedArticles' => new Relationship([
'post_type' => Article::getType(),
]),
];
}
}
user
)<?php
namespace gotoAndPlay\Models;
use gotoAndPlay\Modules\ContentOneColumn;
class Author extends Post {
protected function getView() {
return '@view-user';
}
public static function getFields() {
return [
'description' => new ContentOneColumn(),
];
}
}
category
)<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Fields\Image;
class Category extends Term {
public static $taxonomy = 'category';
protected function getView() {
return '@view-category';
}
public static function getFields() {
return [
'image' => new Image(),
];
}
}
product
)<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Fields\Number;
class Product extends Post {
protected static $type = 'product';
public function getPrice() {
return sprintf('%s€', $this->priceInEuros);
}
protected function getView() {
return '@view-product';
}
public static function getPostTypeArgs() {
return [
'public' => true,
];
}
public static function getFields() {
return [
'priceInEuros' => new Number(),
];
}
}
To create an options page we have to extend OptionsPage
class and add a static method getName
which will add the page to WordPress admin.
To retrieve data from options page use static method get
on the options page class. Here is an example how to get the cookie data from Cookie options page $cookie = Cookie::get('cookie')
.
<?php
namespace gotoAndPlay\Settings;
use gotoAndCore\Models\OptionsPage;
use gotoAndPlay\Modules\Cookie;
class Cookie extends OptionsPage {
public static function getFields() {
return [
'cookie' => new Cookie(),
];
}
public static function getName() {
return __('Cookie', 'gotoandplay');
}
}
For the menu example we add a title field under the main menu and a background color select for menu items.
To do so we have to create a menu class and an menu item class.
Under the menu class we tell which menu item class to use with the method getMenuItemClass
.
<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Models\NavMenu;
use gotoAndCore\Fields\Text;
class MainMenu extends NavMenu {
public static $location = 'main';
public static $menuItemClass = MainMenuItem::class;
public static function getFields() {
return [
'title' => new Text(),
];
}
}
<?php
namespace gotoAndPlay\Models;
use gotoAndCore\Models\NavMenuItem;
use gotoAndCore\Fields\ColorPicker;
class MainMenuItem extends NavMenuItem {
public static $location = 'main';
public static function getFields() {
return [
'backgroundColor' => new ColorPicker(),
];
}
}
If there are some other cases where we need to use custom fields we can always add public static method getFieldGroupSettings
and specify custom locations or settings.