You are here

public function WebformCliService::drush_webform_docs in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::drush_webform_docs()

Implements drush_hook_COMMAND().

Overrides WebformCliServiceInterface::drush_webform_docs

File

src/Commands/WebformCliService.php, line 963

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_docs() {

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $html_directory_path = drupal_get_path('module', 'webform') . '/html';
  $images_directory_path = "{$html_directory_path}/images";

  // Create the /html directory.
  if (!file_exists($html_directory_path)) {
    $file_system
      ->mkdir($html_directory_path);
  }
  if (!file_exists($images_directory_path)) {
    $file_system
      ->mkdir($images_directory_path);
  }

  // Generate docs from MarkDown using the README module's ReadmeManager.

  /** @var \Drupal\readme\ReadmeManagerInterface $readme_manager */
  $readme_manager = \Drupal::service('readme.manager');
  $markdown = [
    'features' => 'docs/FEATURES.md',
  ];
  foreach ($markdown as $markdown_name => $markdown_path) {
    $markdown_html = $readme_manager
      ->getHtml('webform', $markdown_path);
    $markdown_html = preg_replace('#^\\s*<h2>[^<]+</h2>\\s*#', '', $markdown_html);
    $markdown_html = $this
      ->_drush_webform_docs_tidy($markdown_html);
    file_put_contents("{$html_directory_path}/webform-{$markdown_name}.html", $markdown_html);
  }

  // Generate docs from WebformHelpManager.

  /** @var \Drupal\webform\WebformHelpManagerInterface $help_manager */
  $help_manager = \Drupal::service('webform.help_manager');
  $help = [
    'videos' => $help_manager
      ->buildVideos(TRUE),
    'addons' => $help_manager
      ->buildAddOns(TRUE),
    'libraries' => $help_manager
      ->buildLibraries(TRUE),
    'comparison' => $help_manager
      ->buildComparison(TRUE),
  ];
  $index_html = '<h1>Webform Help</h1><ul>';
  foreach ($help as $help_name => $help_section) {
    $help_html = \Drupal::service('renderer')
      ->renderPlain($help_section);
    $help_html = $this
      ->_drush_webform_docs_tidy($help_html);
    if ($help_name === 'videos') {

      // Download YouTube thumbnails so that they can be updated to
      // https://www.drupal.org/files/
      preg_match_all('#https://img.youtube.com/vi/([^/]+)/0.jpg#', $help_html, $matches);
      foreach ($matches[0] as $index => $image_uri) {
        $file_name = 'webform-youtube-' . $matches[1][$index] . '.jpg';
        copy($image_uri, "{$images_directory_path}/{$file_name}");
        $help_html = str_replace($image_uri, "https://www.drupal.org/files/{$file_name}", $help_html);
      }
    }
    file_put_contents("{$html_directory_path}/webform-{$help_name}.html", $help_html);
    $index_html .= "<li><a href=\"webform-{$help_name}.html\">webform-{$help_name}.html</a></li>";
  }
  $index_html .= '</ul>';
  file_put_contents("{$html_directory_path}/index.html", $this
    ->_drush_webform_docs_tidy($index_html));
  $this
    ->drush_print("Documents generated to '/{$html_directory_path}'.");
}