Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Drupal 8 Module Settings and Mapping

By editor , 7 June, 2017
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

 

Tags

  • Drupal 8
  • Code
  • Module
Drupal Configuration System (Config API)
Set configuration at the module installation
Drupal Site Settings : Using settings.php

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer