4 * Drupal Bootstrap Drush commands.
6 use Drupal\bootstrap\Bootstrap;
7 use Drupal\bootstrap\Theme;
8 use Drupal\Component\Serialization\Yaml;
11 * Implements hook_drush_command().
13 function bootstrap_drush_command() {
14 $items['bootstrap-generate-docs'] = [
15 'description' => dt('Generates markdown documentation for the Drupal based code.'),
17 'type' => 'The specific type of documentation to generate, defaults to "all". Can be: "all", "settings".',
19 'aliases' => ['bs-docs'],
25 * Generates markdown documentation.
29 function drush_bootstrap_generate_docs($type = 'all') {
30 $types = $type === 'all' ? ['settings'] : [$type];
31 foreach ($types as $type) {
32 $function = "_drush_bootstrap_generate_docs_$type";
33 if (function_exists($function)) {
34 $ret = $function(Bootstrap::getTheme('bootstrap'));
36 drush_log('Successfully generated documentation for: ' . $type, 'success');
39 drush_log('Unable to generate documentation for: ' . $type, 'error');
43 drush_log('Invalid documentation type: ' . $type, 'error');
49 * Generates settings documentation.
51 * @param \Drupal\bootstrap\Theme $bootstrap
52 * The theme instance of the Drupal Bootstrap base theme.
54 function _drush_bootstrap_generate_docs_settings(Theme $bootstrap) {
55 $output[] = '<!-- @file Overview of theme settings for Drupal Bootstrap based themes. -->';
56 $output[] = '<!-- @defgroup -->';
57 $output[] = '<!-- @ingroup -->';
58 $output[] = '# Theme Settings';
60 $output[] = 'To override a setting, open `./config/install/THEMENAME.settings.yml` and add the following:';
62 $output[] = '```yaml';
63 $output[] = '# Settings';
65 $output[] = 'settings:';
66 $output[] = ' SETTING_NAME: SETTING_VALUE';
69 // Determine the groups.
71 foreach ($bootstrap->getSettingPlugin() as $setting) {
72 // Only get the first two groups (we don't need 3rd, or more, levels).
73 $_groups = array_slice($setting->getGroups(), 0, 2, FALSE);
77 $groups[implode(' > ', $_groups)][] = $setting->getPluginDefinition();
80 // Generate a table of each group's settings.
81 foreach ($groups as $group => $settings) {
85 $output[] = "### $group";
87 $output[] = '<table class="table table-striped table-responsive">';
88 $output[] = ' <thead><tr><th class="col-xs-3">Setting name</th><th>Description and default value</th></tr></thead>';
89 $output[] = ' <tbody>';
90 foreach ($settings as $definition) {
92 $output[] = ' <td class="col-xs-3">' . $definition['id'] . '</td>';
94 $output[] = ' <div class="help-block">' . str_replace('"e;', '"', $definition['description']) . '</div>';
95 $output[] = ' <pre class=" language-yaml"><code>' . Yaml::encode([$definition['id'] => $definition['defaultValue']]) . '</code></pre>';
99 $output[] = ' </tbody>';
100 $output[] = '</table>';
103 // Ensure we have link references at the bottom.
105 $output[] = '[Drupal Bootstrap]: https://www.drupal.org/project/bootstrap';
106 $output[] = '[Bootstrap Framework]: http://getbootstrap.com';
108 // Save the generated output to the appropriate file.
109 return file_put_contents(realpath($bootstrap->getPath() . '/docs/Theme-Settings.md'), implode("\n", $output)) !== FALSE;