Skip to content

Group of Settings

Loïc Antignac edited this page Jun 28, 2022 · 23 revisions

This entity is used to create content for custom native options pages. A native option page (or several) must therefore first have been created to attach the group(s) of setting fields that will be created.
Fields are react components.
Several groups of settings can be declared for the same options page: they will be displayed on the page in tabs, each group representing a tab.
The component used to display groups of settings is the TabPanel component.
    
image   

Once the the plugin is created, the declaration(s) can be added:

<?php
/**
 * Plugin Name:       Example Groups of Settings
 * Author:            My Name
 * Text Domain:       wax-custom-content
 * Domain Path:       /languages
 */
defined( 'ABSPATH' ) || exit;

// If you already have an autoload (for example if you are on Bedrock) you can remove these 3 lines, otherwise leave them.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
	require __DIR__ . '/vendor/autoload.php';
}

use Webaxones\Core\Entities\Entities;

Webaxones\Core\Library::init( 'webaxones-content' );

/**
 * Custom "Legal informations" settings group for company option page
 * attention, a declaration for a native option page must exist before these settings declarations
 */
$declarations[] = [
	'entity'   => 'Webaxones\Core\Option\SettingGroup',
	'labels'   => [
		'group_label'            => __( 'Legal Informations', 'webaxones-content' ),
		'company_name_label'     => __( 'Company name', 'webaxones-content' ),
		'company_name_help'      => __( 'Full name of the company', 'webaxones-content' ),
		'company_adr1_label'     => __( 'Company address', 'webaxones-content' ),
		'company_adr1_help'      => __( 'Address #1 of the company', 'webaxones-content' ),
		'company_add_addr_label' => __( 'Additional address', 'webaxones-content' ),
		'company_add_addr_help'  => __( 'Additional address of the company', 'webaxones-content' ),
		'opening_day_hour_label' => __( 'Opening day/hour', 'webaxones-content' ),
		'opening_day_hour_help'  => __( 'Opening day/hour of the company', 'webaxones-content' ),
		'freewifi_label'         => __( 'Free wifi?', 'webaxones-content' ),
		'freewifi_help'          => __( 'Free wifi on site?', 'webaxones-content' ),
		'swimmingpool_label'     => __( 'Swimming pool?', 'webaxones-content' ),
		'swimmingpool_help'      => __( 'Swimming pool on site?', 'webaxones-content' ),
		'the_photo_label'        => __( 'Swimming pool photo', 'webaxones-content' ),
	],
	'settings' => [
		'slug'      => 'wax-company-settings-legal',
		'page_slug' => 'wax-company-settings',
		'label'     => 'group_label',
		'fields'    => [
			[
				'slug'   => 'wax_company_settings_company_name',
				'type'   => 'text', /*'textarea', 'number', 'email', 'datetime-local', 'checkbox', 'toggle', 'media'*/
				'labels' => [
					'label' => 'company_name_label',
					'help'  => 'company_name_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_company_addr',
				'type'   => 'text',
				'labels' => [
					'label' => 'company_adr1_label',
					'help'  => 'company_adr1_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_company_add_addr',
				'type'   => 'textarea',
				'labels' => [
					'label' => 'company_add_addr_label',
					'help'  => 'company_add_addr_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_opening_day_hour',
				'type'   => 'datetime-local',
				'labels' => [
					'label' => 'opening_day_hour_label',
					'help'  => 'opening_day_hour_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_freewifi',
				'type'   => 'checkbox',
				'labels' => [
					'label' => 'freewifi_label',
					'help'  => 'freewifi_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_swimmingpool',
				'type'   => 'toggle',
				'labels' => [
					'label' => 'swimmingpool_label',
					'help'  => 'swimmingpool_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_the_photo',
				'type'   => 'image',
				'labels' => [
					'label' => 'the_photo_label',
					'help'  => '',
				],
			],
		],
	],
];

/**
 * Custom "Contact informations" settings group for company option page
 */
$declarations[] = [
	'entity'   => 'Webaxones\Core\Option\SettingGroup',
	'labels'   => [
		'group_label'        => __( 'Contact Informations', 'webaxones-content' ),
		'phone_number_label' => __( 'Phone number', 'webaxones-content' ),
		'phone_number_help'  => __( 'Phone number of the company', 'webaxones-content' ),
		'mail_label'         => __( 'E-mail address', 'webaxones-content' ),
		'mail_help'          => __( 'E-mail address of the company', 'webaxones-content' ),
	],
	'settings' => [
		'slug'      => 'wax-company-settings-contact',
		'page_slug' => 'wax-company-settings',
		'label'     => 'group_label',
		'fields'    => [
			[
				'slug'   => 'wax_company_settings_phone_number',
				'type'   => 'number',
				'labels' => [
					'label' => 'phone_number_label',
					'help'  => 'phone_number_help',
				],
			],
			[
				'slug'   => 'wax_company_settings_mail',
				'type'   => 'email',
				'labels' => [
					'label' => 'mail_label',
					'help'  => 'mail_help',
				],
			],
		],
	],
];

Entities::process( $declarations );

Settings

As for all entities, settings starts with the slug key which identifies the entity and must be unique.

The page_slug key must be the slug defined in the native option page declaration.

The label key must not be modified since it contains the link to the label of the group, label which will appear in the tab item.

The fields key contains an array of fields to create.

Each field uses 3 keys:

  • slug to identify the field (must be unique)
  • type to identify the type of field to display : must exactly match one of the values in the table below.
  • labels with 2 sub-values:
    • label: label displayed above the field
    • help: help message displayed below the field (if the field must not have a help message, give it an empty string '')

text and textarea fields accept an additional key called args which takes an array of the optional arguments that an HTML5 input type text and an HTML5 textarea can support.
Example for text field:

'args'   => [
	'maxLength'   => '8',
	'minLength'   => '4',
	'pattern'     => '[a-z]{4,8}',
	'placeholder' => 'username',
	'size'        => '10',
	'required'    => true,
],

Setting field types

Field Key for type
Text text
Number number
Email email
Textarea textarea
DateTime datetime-local
Checkbox checkbox
Toggle toggle
Image image
Select Data selectData
Section grouping fields section
Select Data with scroll pagination (Limited to 3 scrolls) selectDataScroll
Section of fields section
Repeater repeater

Clone this wiki locally