Drupal 8 Module Settings and Mapping | Drupal 8

Drupal 8 Module Settings and Mapping

Submitted by editor on Wed, 06/07/2017 - 11:24
Question

How to define default configuration values / Settings of a drupal 8 module ?
How to set configuration values at the module installation ?

For the examples, here we use the module simple_analytics as example.
Module name : simple_analytics
Configuration object: simple_analytics.settings

Settings and mapping files structure.

MODULE-ROOT
|-- config
|    |-- install
|        |-- MYMODULE.CONFIGURATION.yml
|    |-- schema
|        |-- MYMODULE.schema.yml
.
.
.

// Example. (simple_analytics)
simple_analytics
|-- config
|    |-- install
|        |-- simple_analytics.settings.yml
|    |-- schema
|        |-- simple_analytics.schema.yml
.
.
.

Settings file.

This file contains the default settings of the module.
Example: (config/install/simple_analytics.settings.yml)
# Init settings for simple_analytics module.
sa_tracker: true
track_auth: true
track_admin: false
displaystat: 30
google-id: ''
piwik-id: ''
custom: ''
piwik-uri: ''
# External libs.
lib_chartist: false

Mapping file

This file define data structure (mapping and data types)
Example : (config/schema/simple_analytics.schema.yml)
# Schema for the configuration files of the simple_analytics module.
simple_analytics.settings:
  type: config_object
  label: 'Simple Analytics settings'
  mapping:
    sa_tracker:
      type: boolean
      label: 'Internal tracker ON / OFF'
    track_auth:
      type: boolean
      label: 'Track authenticated users'
    track_admin:
      type: boolean
      label: 'Track admin pages'
    lib_chartist:
      type: boolean
      label: 'Chartist-JS lib present and activates'
    displaystat:
      type: integer
      label: 'Duration of the statistic page'
    google-id:
      type: string
      label: 'Google analytics ID'
    piwik-uri:
      type: string
      label: 'Piwik analytics URL'
    piwik-id:
      type: string
      label: 'Piwik analytics ID'
    custom:
      type: string
      label: 'Custom tracking code'

      
Data types

config_object    (As an array)
boolean
email
integer
float
string
uri

Fore more details see Drupal Doc :
https://www.drupal.org/docs/8/api/configuration-api/configuration-schemametadata

 

Add new comment