One of the modules I've been working on for over a year now is called aggrid http://www.drupal.org/project/aggrid. This module uses the ag-Grid library https://www.ag-grid.com/. The community portion is full open source, but they have a paid enterprise version with some great features.
That aside, I have some settings hard coded into the .js file and wanted to bring those into Drupal 8 config. At the same time, I didn't want to break existing sites with this update (use an update hook for adding to existing). So, how do you do it?
Problem: My Drupal 8 module needs a few new config settings with important defaults. How do I add them and also allow existing installs to update?
Let's try to keep this short and sweet. I'm going to assume you know how to do a few things.
Assumptions:
- You already have a settings yml file for your config items and you know how to add things to it.
- You already have a form for your settings and you know how to add things to it.
Ok... You added your new items to the yml file... so someone installing fresh would be ok. Then you also added the form items, etc for your config page. Great! Now, for updating the existing installs with defaults. We're going to use hook_update_n for that.
Function: hook_update_n
For demonstration, let's say your module is named "mymodule". Ok, you're going to put your update function either in mymodule.install or if you want to get fancy, you can put it in an include file.
Question: How do you add an update include file to your mymodule.install file?
// mymodule update hooks.
include_once __DIR__ . '/includes/mymoduleinstall.update.inc';
Question: How do I add my new config items and set necessary defaults but use my same install yml file?
<?php
use Symfony\Component\Yaml\Yaml;
/******************************************************************************/
// mymodule-8.x-1.0-alpha4000
/******************************************************************************/
/**
* Issue #123455: Adding some new config fields
*/
function mymodule_update_8001()
{
$file_path = drupal_get_path('module', 'mymodule') . '/config/install/mymodule.settings.yml';
$file_contents = file_get_contents($file_path);
$ymldata = Yaml::parse($file_contents);
$mymodule_config = \Drupal::service('config.factory')->getEditable('mymodule.settings');
$mymodule_config->set('newsetting1', $ymldata['newsetting1']);
$mymodule_config->set('newsetting2', $ymldata['newsetting2']);
$mymodule_config->set('newsetting3', $ymldata['newsetting3']);
$aggrid_config->save();
}
Basically, this update will fetch your yml file. Then, you can just grab the specific settings with defaults and use it for your update! Once you run drush updatedb or the Drupal update URL, it should add your new config items with defaults.
Here's to better Drupal modules with updates instead of just breaking / requiring full new install.
** Wait... didn't you say something about bringing javascript settings into Drupal 8? How do you get items from Drupal through javascript? Ajax?
Pass config settings from Drupal 8 to Javascript
First... you'll need some mymodule.library.yml dependencies.
dependencies:
- core/drupal
- core/drupalSettings
Getting your js file ready for the variables
(function ($, Drupal, drupalSettings) {
const newsetting1 = drupalSettings.mymodule.settings.newsetting1;
const newsetting2 = drupalSettings.mymodule.settings.newsetting2;
const newsetting3 = drupalSettings.mymodule.settings.newsetting3;
})(jQuery, Drupal, drupalSettings);
Finally, let's provide the data to the js file from Drupal through the mymodule.module file.
/**
* Add mymodule settings to variable
* @param $variables
*/
function mymodule_preprocess_html(&$variables) {
// Get config for aggrid
$config = \Drupal::config('mymodule.settings');
// Set the mymodule setting variables
$variables['#attached']['drupalSettings']['mymodule']['settings']['newsetting1'] = $config->get('newsetting1');
$variables['#attached']['drupalSettings']['mymodule']['settings']['newsetting2'] = $config->get('newsetting2');
$variables['#attached']['drupalSettings']['mymodule']['settings']['newsetting3'] = $config->get('newsetting3');
}
Log in to post comments