## Create the Param class
First, create your Param class by extending the base `Param` class (the class to use depends of your storage)
``` php
<?php
// src/Entity/Param.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Mukadi\SettingsBundle\Entity\Param as Base;
/**
* Param
*
* @ORM\Table(name="param")
*/
class Param extends Base
{
}
Add packages configuration file
Configure the bundle for using this class, and optionnaly specifying the current Object Manager used by the application, if 'manager' key is missing the default Doctrine entity manager will be used:, (*8)
Now that the bundle is configured, the last thing you need to do is update your
database schema because you have added a new entity.
For ORM run the following command.
``` bash
$ php app/console doctrine:schema:update --force
Build the form
Below is a minimal example of the necessary configuration for build a settings form:, (*11)
``` yaml, (*12)
config/packages/mukadi_settings.yaml
mukadi_settings:
param_class: App\Entity\Param
settings:
currency: # the setting key
type: choice # the form field type
options: # options to provide to the field
label: Devise
choices: '(USD: Dollar US,CDF: Francs Congolais)' # use this notation for setting up the choice list
bio:
type: textarea
options:
label: 'About me'
age:
type: integer
options:
label: Age
post:
type: entity
options:
label: Article
choice_label: title
class: App\Entity\Post
admin:
type: email
options:
label: Admin email
date:
type: date
options:
widget: single_text
format: yyyy-MM-dd
birthday:
type: datetime
options:
days: '[1-10]' # range notation, generate values from 1 to 10
months: '[1-12,2]' # range notation, generate values from 1 to 12, the value 2 is incrementation step
years: '(2016,2017)' # array notation
public:
type: toggle
options:
label: 'Show posts ?'
required: false, (*13)
The currently supported types are:
- text : simple text field, save setting as plain text in the database
- integer : input for integer
- toggle : a simple checkbox
- number: input for number (decimals and float)
- textarea: multi-lines text input
- entity: for select an entity in the database. The bundle store only the `id`.
- email: input for email adresses
- datetime: input for date and time
- date: input for date
Usage
-----
Now that you have properly create your settings form. You can render it a custom `SonataAdminBundle` action page, or in any view in your application:
In the controller use the `mukadi_settings.setting` service for getting the form (Or use the `Mukadi\SettingsBundle\Model\Setting` class if you're using the autowiring)
``` php
<?php
// src/Controller/AppController.php
...
$setting = $this->get('mukadi_settings.setting');
$form = $setting->getForm();
And in your view, render the form like any other form:, (*14)
When the form is submitted, in your controller you must handle that request if you want to store the updated settings in your database.
``` php
<?php
// src/Controller/AppController.php
...
$setting = $this->get('mukadi_settings.setting');
$form = $setting->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$setting->saveData($form); # store the update in the database
}
And finally you can retrieve the stored data via the mukadi_settings.setting service like this:, (*16)
php
<?php
// src/Controller/AppController.php
...
$setting = $this->get('mukadi_settings.setting');
$currency = $setting->get('currency'); // return 'USD' or 'CDF'
$bio = $setting->get('bio'); // return a string
$node = $setting->get('post'); // return null or a App\Entity\Post entity as configured, (*17)
The Versions
09/062018
dev-master
9999999-dev
this bundle provide a global app settings form fully customisable