Newer
Older
Mark Halliwell
committed
<?php
Mark Halliwell
committed
Mark Halliwell
committed
/**
* @file
* Drupal Bootstrap Drush commands.
*/
Mark Halliwell
committed
Mark Halliwell
committed
use Drupal\bootstrap\Bootstrap;
use Drupal\bootstrap\Theme;
use Drupal\Component\Serialization\Yaml;
/**
* Implements hook_drush_command().
*/
function bootstrap_drush_command() {
$items['bootstrap-generate-docs'] = [
'description' => dt('Generates markdown documentation for the Drupal based code.'),
Mark Halliwell
committed
'arguments' => [
'type' => 'The specific type of documentation to generate, defaults to "all". Can be: "all", "settings".',
],
'aliases' => ['bs-docs'],
];
return $items;
}
/**
* Generates markdown documentation.
*
* @param string $type
Mark Halliwell
committed
* The type of documentation.
Mark Halliwell
committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
*/
function drush_bootstrap_generate_docs($type = 'all') {
$types = $type === 'all' ? ['settings'] : [$type];
foreach ($types as $type) {
$function = "_drush_bootstrap_generate_docs_$type";
if (function_exists($function)) {
$ret = $function(Bootstrap::getTheme('bootstrap'));
if ($ret) {
drush_log('Successfully generated documentation for: ' . $type, 'success');
}
else {
drush_log('Unable to generate documentation for: ' . $type, 'error');
}
}
else {
drush_log('Invalid documentation type: ' . $type, 'error');
}
}
}
/**
* Generates settings documentation.
*
* @param \Drupal\bootstrap\Theme $bootstrap
* The theme instance of the Drupal Bootstrap base theme.
*/
function _drush_bootstrap_generate_docs_settings(Theme $bootstrap) {
$output[] = '<!-- @file Overview of theme settings for Drupal Bootstrap based themes. -->';
$output[] = '<!-- @defgroup -->';
$output[] = '<!-- @ingroup -->';
$output[] = '# Theme Settings';
$output[] = '';
$output[] = 'To override a setting, open `./config/install/THEMENAME.settings.yml` and add the following:';
$output[] = '';
$output[] = '```yaml';
$output[] = '# Settings';
$output[] = '';
Mark Halliwell
committed
$output[] = 'SETTING_NAME: SETTING_VALUE';
Mark Halliwell
committed
$output[] = '```';
// Determine the groups.
$groups = [
'general' => [],
'components' => [],
'javascript' => [],
'cdn' => [],
'advanced' => [],
];
foreach ($bootstrap->getSettingPlugin() as $setting) {
Mark Halliwell
committed
// Only get the first two groups (we don't need 3rd, or more, levels).
$_groups = array_filter(array_slice($setting->getGroups(), 0, 2, FALSE));
Mark Halliwell
committed
if (!$_groups) {
continue;
}
$groups[array_keys($_groups)[0]][implode(' > ', $_groups)][] = $setting->getPluginDefinition();
Mark Halliwell
committed
}
// Generate a table of each group's settings.
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
foreach ($groups as $subgroups) {
foreach ($subgroups as $group => $settings) {
$output[] = '';
$output[] = '---';
$output[] = '';
$output[] = "### $group";
$output[] = '';
$output[] = '<table class="table table-striped table-responsive">';
$output[] = ' <thead>';
$output[] = ' <tr>';
$output[] = ' <th class="col-xs-3">Setting name</th>';
$output[] = ' <th>Description and default value</th>';
$output[] = ' </tr>';
$output[] = ' </thead>';
$output[] = ' <tbody>';
foreach ($settings as $definition) {
$output[] = ' <tr>';
$output[] = ' <td class="col-xs-3">';
$output[] = $definition['id'];
$output[] = ' </td>';
$output[] = ' <td>';
$output[] = ' <div class="help-block">';
$output[] = str_replace('"e;', '"', wordwrap($definition['description']));
$output[] = ' </div>';
$output[] = ' <pre class=" language-yaml"><code>';
$output[] = wordwrap(Yaml::encode([$definition['id'] => $definition['defaultValue']]));
$output[] = '</code></pre>';
$output[] = ' </td>';
$output[] = ' </tr>';
}
$output[] = ' </tbody>';
$output[] = '</table>';
Mark Halliwell
committed
}
}
// Ensure we have link references at the bottom.
$output[] = '';
$output[] = '[Drupal Bootstrap]: https://www.drupal.org/project/bootstrap';
$output[] = '[Bootstrap Framework]: https://getbootstrap.com/docs/3.4/';
$output[] = '';
Mark Halliwell
committed
// Save the generated output to the appropriate file.
return file_put_contents(realpath($bootstrap->getPath() . '/docs/Theme-Settings.md'), implode("\n", $output)) !== FALSE;