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