5 * Drupal Bootstrap Drush commands.
8 use Drupal\bootstrap\Bootstrap;
9 use Drupal\bootstrap\Theme;
10 use Drupal\Component\Serialization\Yaml;
13 * Implements hook_drush_command().
15 function bootstrap_drush_command() {
16 $items['bootstrap-generate-docs'] = [
17 'description' => dt('Generates markdown documentation for the Drupal based code.'),
19 'type' => 'The specific type of documentation to generate, defaults to "all". Can be: "all", "settings".',
21 'aliases' => ['bs-docs'],
27 * Generates markdown documentation.
30 * The type of documentation.
32 function drush_bootstrap_generate_docs($type = 'all') {
33 $types = $type === 'all' ? ['settings'] : [$type];
34 foreach ($types as $type) {
35 $function = "_drush_bootstrap_generate_docs_$type";
36 if (function_exists($function)) {
37 $ret = $function(Bootstrap::getTheme('bootstrap'));
39 drush_log('Successfully generated documentation for: ' . $type, 'success');
42 drush_log('Unable to generate documentation for: ' . $type, 'error');
46 drush_log('Invalid documentation type: ' . $type, 'error');
52 * Generates settings documentation.
54 * @param \Drupal\bootstrap\Theme $bootstrap
55 * The theme instance of the Drupal Bootstrap base theme.
57 function _drush_bootstrap_generate_docs_settings(Theme $bootstrap) {
58 $output[] = '<!-- @file Overview of theme settings for Drupal Bootstrap based themes. -->';
59 $output[] = '<!-- @defgroup -->';
60 $output[] = '<!-- @ingroup -->';
61 $output[] = '# Theme Settings';
63 $output[] = 'To override a setting, open `./config/install/THEMENAME.settings.yml` and add the following:';
65 $output[] = '```yaml';
66 $output[] = '# Settings';
68 $output[] = 'settings:';
69 $output[] = ' SETTING_NAME: SETTING_VALUE';
72 // Determine the groups.
74 foreach ($bootstrap->getSettingPlugin() as $setting) {
75 // Only get the first two groups (we don't need 3rd, or more, levels).
76 $_groups = array_slice($setting->getGroups(), 0, 2, FALSE);
80 $groups[implode(' > ', $_groups)][] = $setting->getPluginDefinition();
83 // Generate a table of each group's settings.
84 foreach ($groups as $group => $settings) {
88 $output[] = "### $group";
90 $output[] = '<table class="table table-striped table-responsive">';
91 $output[] = ' <thead>';
93 $output[] = ' <th class="col-xs-3">Setting name</th>';
94 $output[] = ' <th>Description and default value</th>';
96 $output[] = ' </thead>';
97 $output[] = ' <tbody>';
98 foreach ($settings as $definition) {
100 $output[] = ' <td class="col-xs-3">';
101 $output[] = $definition['id'];
102 $output[] = ' </td>';
104 $output[] = ' <div class="help-block">';
105 $output[] = str_replace('"e;', '"', wordwrap($definition['description']));
106 $output[] = ' </div>';
107 $output[] = ' <pre class=" language-yaml"><code>';
108 $output[] = wordwrap(Yaml::encode([$definition['id'] => $definition['defaultValue']]));
109 $output[] = '</code></pre>';
110 $output[] = ' </td>';
111 $output[] = ' </tr>';
113 $output[] = ' </tbody>';
114 $output[] = '</table>';
117 // Ensure we have link references at the bottom.
119 $output[] = '[Drupal Bootstrap]: https://www.drupal.org/project/bootstrap';
120 $output[] = '[Bootstrap Framework]: https://getbootstrap.com/docs/3.3/';
122 // Save the generated output to the appropriate file.
123 return file_put_contents(realpath($bootstrap->getPath() . '/docs/Theme-Settings.md'), implode("\n", $output)) !== FALSE;