Version 1
[yaffs-website] / web / themes / contrib / bootstrap / bootstrap.drush.inc
1 <?php
2 /**
3  * @file
4  * Drupal Bootstrap Drush commands.
5  */
6 use Drupal\bootstrap\Bootstrap;
7 use Drupal\bootstrap\Theme;
8 use Drupal\Component\Serialization\Yaml;
9
10 /**
11  * Implements hook_drush_command().
12  */
13 function bootstrap_drush_command() {
14   $items['bootstrap-generate-docs'] = [
15     'description' => dt('Generates markdown documentation for the Drupal based code.'),
16     'arguments' => [
17       'type' => 'The specific type of documentation to generate, defaults to "all". Can be: "all", "settings".',
18     ],
19     'aliases' => ['bs-docs'],
20   ];
21   return $items;
22 }
23
24 /**
25  * Generates markdown documentation.
26  *
27  * @param string $type
28  */
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'));
35       if ($ret) {
36         drush_log('Successfully generated documentation for: ' . $type, 'success');
37       }
38       else {
39         drush_log('Unable to generate documentation for: ' . $type, 'error');
40       }
41     }
42     else {
43       drush_log('Invalid documentation type: ' . $type, 'error');
44     }
45   }
46 }
47
48 /**
49  * Generates settings documentation.
50  *
51  * @param \Drupal\bootstrap\Theme $bootstrap
52  *   The theme instance of the Drupal Bootstrap base theme.
53  */
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';
59   $output[] = '';
60   $output[] = 'To override a setting, open `./config/install/THEMENAME.settings.yml` and add the following:';
61   $output[] = '';
62   $output[] = '```yaml';
63   $output[] = '# Settings';
64   $output[] = '';
65   $output[] = 'settings:';
66   $output[] = '  SETTING_NAME: SETTING_VALUE';
67   $output[] = '```';
68
69   // Determine the groups.
70   $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);
74     if (!$_groups) {
75       continue;
76     }
77     $groups[implode(' > ', $_groups)][] = $setting->getPluginDefinition();
78   }
79
80   // Generate a table of each group's settings.
81   foreach ($groups as $group => $settings) {
82     $output[] = '';
83     $output[] = '---';
84     $output[] = '';
85     $output[] = "### $group";
86     $output[] = '';
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) {
91       $output[] = '  <tr>';
92       $output[] = '    <td class="col-xs-3">' . $definition['id'] . '</td>';
93       $output[] = '    <td>';
94       $output[] = '      <div class="help-block">' . str_replace('&quote;', '"', $definition['description']) . '</div>';
95       $output[] = '      <pre class=" language-yaml"><code>' . Yaml::encode([$definition['id'] => $definition['defaultValue']]) . '</code></pre>';
96       $output[] = '    </td>';
97       $output[] = '  </tr>';
98     }
99     $output[] = '  </tbody>';
100     $output[] = '</table>';
101   }
102
103   // Ensure we have link references at the bottom.
104   $output[] = '';
105   $output[] = '[Drupal Bootstrap]: https://www.drupal.org/project/bootstrap';
106   $output[] = '[Bootstrap Framework]: http://getbootstrap.com';
107
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;
110 }