settings.php
file on freshly installed Drupal 8. $config_directories['active'] = 'sites/default/files/config_XXXX/active'; $config_directories['staging'] = 'sites/default/files/config_XXXX/staging';
XXXX
is a long hash generated to protect your configuration from access from the web.active
directory means storing your current configuration. I said “implied” because by default this directory is empty, and the current configuration is stored in a database. This is for security and performance reasons. However, you can change this behavior using the Configuration Tools module (config_tools). (Don't you remember that there is a module for everything ?;))staging
directory is the place from where the configuration is imported / synchronized. This directory is also empty. If you do not use the option to import / export configurations.Tip: Git
Thesites/default/files
directory is most often included in the.gitignore
file and is ignored by Git, but you most likely want your configuration files to be managed by a version control system. The recommended way to place the configurations directory is outside the directory where Drupal is installed, so that it is not accessible to the web. And the easiest way is to move the configurations directory to thesites/default
directory (but leave the hash in its name for security).
admin/config/development/configuration
path. The “Full Import / Export” tab is the most interesting here. The complete site configuration can be exported as a .tar.gz archive in the Export sub-tab. Then you can use this archive to import the configuration on another instance of the site.staging
directory. After that, you need to start synchronization on the Synchronize tab, where you can also see the changes you have made. drush config-export # staging drush config-import # staging
# file: system.site.yml uuid: c78fd9aa-b327-4514-9d00-bc72a1f40f27 name: 'My cool site' mail: mailbox@example.com slogan: 'Drupal 8 rules!' page: 403: "" 404: "" front: node admin_compact_mode: false weight_select_max: 100 langcode: en
system.site
. This is the unit of measurement for configuration. All configuration names should have at the beginning the name of the module or theme, in this case system
.Tip: Best Practices
If your module / theme does not have a lot of settings, it is recommended to store them in one configuration with the name your_module_name.settings.
$config = \Drupal::config('system.site'); // Instance of Drupal\Core\Config\Config $front_page = $config->get('page.front'); // 'node' $page_settings = $config->get('page'); // array( // '403' => '', // '404' => '', // 'front' => 'node', // ) $config->set('page.front', 'my-front-page'); $config->save();
$value = variable_get('my_variable_name', 'my_default_value');
config/install
directory of your module.hook_install()
.contact.form.feedback.yml
, the value of recipients
set to an empty array. Then, in contact_install()
, the actual value is set.config/install
directory.views.view.my_cool_view.yml
)config/install
directorysettings.php
file can replace configurations at the environment level. Just remember how we prohibited sending emails from development servers in Drupal 7: $conf['mail_system']['default-system'] = 'DevelMailLog';
$config['system.mail']['interface']['default'] = 'devel_mail_log';
config.factory.override
tag and implement the ConfigFactoryOverrideInterface
class. You can find an example in the Language module, which translates some strings stored in configurations (this way, when you call Drupal::config(...)->get(...)
, you actually get the translated string instead of the original one) .ConfigFormBase
, which should be used instead of the standard FormBase
. It has the ConfigFormBase::config()
method, which returns the initial configurations and which, again, should be used instead of the usual Drupal::config()
.Config::getRawData()
method. And for more complex cases, look at the implementation of the ConfigFormBaseTrait::config()
method .Tip: Best Practices
When you work from a class that extends a base class, examine all the methods at your disposal (available from the parent classes) and use them instead of the globally available methods. The example above demonstrates how to get the initial configurations using the::config()
method when you are working from the child classConfigFormBase
. Another example: many classes have a built-in method::t()
, which should be used instead of the global functiont()
. There are always reasons for this, take a look at an example .
ConfigEvents
class ( documentation ). For an example, see the implementation of the class Drupal\language\EventSubscriber\ConfigSubscriber
( documentation ).hook_config_import_steps_alter()
. An example can be found in the Field module.# file: core / modules / system / config / install / system.maintenance.yml message: '! site is currently under maintenance. We should be back shortly. Thank you for your patience. ' langcode: en # file: core / modules / system / config / schema / system.schema.yml system.maintenance: type: mapping label: 'Maintenance mode' mapping: message: type: text label: 'Message to display when in maintenance mode' langcode: type: string label: 'Default language' # file: core / config / schema / core.data_types.schema.yml text: type: string label: 'Text' translatable: true # ... string: class: '\ Drupal \ Core \ TypedData \ Plugin \ DataType \ String' label: 'String'
system.maintenance
configuration, as well as their types:message
is a string available for translation, displayed in the site maintenance modelangcode
is a string indicating the language used for the site maintenance mode.Tip: Translations
Thetranslatable
property is used to mark string data types as available for translation. Such strings are automatically processed by the Language module. There are two basic "translatable" types of strings that you probably want to use:
label
: human-readable line, no markup, containing only one line of texttext
: human-readable string, may contain HTML markup and several lines of text
ImageStyle
from the Image module.Source: https://habr.com/ru/post/248629/
All Articles