file bootstrap.drush.inc

Drupal Bootstrap Drush commands.

Source
  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. * Implements hook_drush_command().
  11. */
  12. function bootstrap_drush_command() {
  13. $items['bootstrap-generate-docs'] = [
  14. 'description' => dt('Generates markdown documentation for the Drupal based code.'),
  15. 'arguments' => [
  16. 'type' => 'The specific type of documentation to generate, defaults to "all". Can be: "all", "settings".',
  17. ],
  18. 'aliases' => ['bs-docs'],
  19. ];
  20. return $items;
  21. }
  22. /**
  23. * Generates markdown documentation.
  24. *
  25. * @param string $type
  26. */
  27. function drush_bootstrap_generate_docs($type = 'all') {
  28. $types = $type === 'all' ? ['settings'] : [$type];
  29. foreach ($types as $type) {
  30. $function = "_drush_bootstrap_generate_docs_$type";
  31. if (function_exists($function)) {
  32. $ret = $function(Bootstrap::getTheme('bootstrap'));
  33. if ($ret) {
  34. drush_log('Successfully generated documentation for: ' . $type, 'success');
  35. }
  36. else {
  37. drush_log('Unable to generate documentation for: ' . $type, 'error');
  38. }
  39. }
  40. else {
  41. drush_log('Invalid documentation type: ' . $type, 'error');
  42. }
  43. }
  44. }
  45. /**
  46. * Generates settings documentation.
  47. *
  48. * @param \Drupal\bootstrap\Theme $bootstrap
  49. * The theme instance of the Drupal Bootstrap base theme.
  50. */
  51. function _drush_bootstrap_generate_docs_settings(Theme $bootstrap) {
  52. $output[] = '<!-- @file Overview of theme settings for Drupal Bootstrap based themes. -->';
  53. $output[] = '<!-- @defgroup -->';
  54. $output[] = '<!-- @ingroup -->';
  55. $output[] = '# Theme Settings';
  56. $output[] = '';
  57. $output[] = 'To override a setting, open `./config/install/THEMENAME.settings.yml` and add the following:';
  58. $output[] = '';
  59. $output[] = '```yaml';
  60. $output[] = '# Settings';
  61. $output[] = '';
  62. $output[] = 'settings:';
  63. $output[] = ' SETTING_NAME: SETTING_VALUE';
  64. $output[] = '```';
  65. // Determine the groups.
  66. $groups = [];
  67. foreach ($bootstrap->getSettingPlugin() as $setting) {
  68. // Only get the first two groups (we don't need 3rd, or more, levels).
  69. $_groups = array_slice($setting->getGroups(), 0, 2, FALSE);
  70. if (!$_groups) {
  71. continue;
  72. }
  73. $groups[implode(' > ', $_groups)][] = $setting->getPluginDefinition();
  74. }
  75. // Generate a table of each group's settings.
  76. foreach ($groups as $group => $settings) {
  77. $output[] = '';
  78. $output[] = '---';
  79. $output[] = '';
  80. $output[] = "### $group";
  81. $output[] = '';
  82. $output[] = '<table class="table table-striped table-responsive">';
  83. $output[] = ' <thead><tr><th class="col-xs-3">Setting name</th><th>Description and default value</th></tr></thead>';
  84. $output[] = ' <tbody>';
  85. foreach ($settings as $definition) {
  86. $output[] = ' <tr>';
  87. $output[] = ' <td class="col-xs-3">' . $definition['id'] . '</td>';
  88. $output[] = ' <td>';
  89. $output[] = ' <div class="help-block">' . str_replace('&quote;', '"', $definition['description']) . '</div>';
  90. $output[] = ' <pre class=" language-yaml"><code>' . Yaml::encode([$definition['id'] => $definition['defaultValue']]) . '</code></pre>';
  91. $output[] = ' </td>';
  92. $output[] = ' </tr>';
  93. }
  94. $output[] = ' </tbody>';
  95. $output[] = '</table>';
  96. }
  97. // Ensure we have link references at the bottom.
  98. $output[] = '';
  99. $output[] = '[Drupal Bootstrap]: https://www.drupal.org/project/bootstrap';
  100. $output[] = '[Bootstrap Framework]: http://getbootstrap.com';
  101. // Save the generated output to the appropriate file.
  102. return file_put_contents(realpath($bootstrap->getPath() . '/docs/Theme-Settings.md'), implode("\n", $output)) !== FALSE;
  103. }

Functions

Namesort descending Description
bootstrap_drush_command Implements hook_drush_command().
drush_bootstrap_generate_docs Generates markdown documentation.
_drush_bootstrap_generate_docs_settings Generates settings documentation.