You are here

public function GenerateForm::submitForm in AT Tools 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

at_theme_generator/src/Form/GenerateForm.php, line 302
Generator form.

Class

GenerateForm
@file Generator form.

Namespace

Drupal\at_theme_generator\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // Generate a new theme.
  if (!empty($values['generate']['generate_machine_name'])) {
    $fileOperations = new FileOperations();
    $directoryOperations = new DirectoryOperations();

    // Prepare form values and set them into variables.
    $machine_name = $values['generate']['generate_machine_name'];
    $friendly_name = Html::escape($values['generate']['generate_friendly_name']);
    $subtheme_type = $values['generate']['generate_type'];
    $clone_source = $values['generate']['generate_clone_source'] ?: '';
    $templates = $values['generate']['options']['generate_templates'];
    $block_config = $values['generate']['options']['generate_block_config'];
    $uikit = $values['generate']['options']['generate_uikit'];
    $color = $values['generate']['options']['generate_color'];
    $theme_settings_file = $values['generate']['options']['generate_themesettingsfile'];
    $description = preg_replace('/[^\\p{Latin}\\d\\s\\p{P}]/u', '', Html::escape($values['generate']['options']['generate_description']));
    $version = Html::escape($values['generate']['options']['generate_version']);

    // Initialize variables.
    $source = '';
    $source_theme = '';

    // Generated date time for descriptions.
    $datetime = \Drupal::service('date.formatter')
      ->format(REQUEST_TIME, 'custom', 'D jS, M, Y - G:i');

    // Generic description if the desc field is not set.
    $generic_description = 'Sub theme of AT Core';

    // Path to themes
    $path = drupal_get_path('theme', 'at_core');
    $at_generator_path = drupal_get_path('module', 'at_theme_generator');

    // Path to where we will save the cloned theme.
    // This could be configurable?
    $at_core_path_parts = explode("/", $path);
    if (in_array('contrib', $at_core_path_parts)) {
      $target_path = array(
        'themes',
        'custom',
      );
    }
    else {
      $target_path = array(
        'themes',
      );
    }
    $target_dir = $directoryOperations
      ->directoryPrepare($target_path);
    $target = "{$target_dir}/{$machine_name}";
    if (!is_writable('themes')) {
      drupal_set_message(t('The "themes" directory is not writable. To generate a new theme reset permissions for the "@themespath" directory so it is writable, e.g. chmod themes 0755.', array(
        '@themespath' => base_path() . 'themes',
      )), 'error');
      return;
    }

    // Array of UIKit tools.
    $uikit_tools = array(
      'package.json',
      '.csslintrc',
      'Gruntfile.js',
      'Gemfile',
      'Gemfile.lock',
    );

    // Standard type variables.
    if ($subtheme_type === 'standard') {
      $source_theme = 'THEMENAME';
      $source = $at_generator_path . '/starterkits/starterkit';
    }

    // Clone variables.
    if ($subtheme_type === 'clone') {
      $source_theme = $clone_source;
      $source = drupal_get_path('theme', $source_theme);
    }

    // Recursively scan the config directory for config files.
    $configuration = $directoryOperations
      ->directoryScanRecursive("{$source}/config");

    // Files to strip replace strings
    $info_file = "{$target}/{$machine_name}.info.yml";
    $library_file = "{$target}/{$machine_name}.libraries.yml";
    $shortcodes_file = "{$target}/{$machine_name}.shortcodes.yml";

    // Begin generation.

    //------------------------------------------------------------------------------------------------

    // Recursively copy the source theme.
    if (is_dir($source)) {
      $directoryOperations
        ->directoryRecursiveCopy($source, $target);
    }

    // Generated CSS files.
    $generated_css = $directoryOperations
      ->directoryScan("{$source}/styles/css/generated");
    foreach ($generated_css as $old_css_file) {
      $new_css_file = str_replace($source_theme, $machine_name, $old_css_file);
      $fileOperations
        ->fileRename("{$target}/styles/css/generated/{$old_css_file}", "{$target}/styles/css/generated/{$new_css_file}");
    }

    // UIKit and Color
    if ($subtheme_type === 'standard') {

      // UIKit
      if ($uikit === 0) {
        $directoryOperations
          ->directoryRemove("{$target}/styles/uikit");

        // remove files like GEM, Gruntfile.js etc
        foreach ($uikit_tools as $tool) {
          unlink("{$target}/{$tool}");
        }

        // Delete the maps directory and all map files.
        $directoryOperations
          ->directoryRemove("{$target}/styles/css/components/maps");
        $component_css_files = $directoryOperations
          ->directoryScan("{$target}/styles/css/components");
        foreach ($component_css_files as $component_file_key => $component_file) {
          $map_string = '/*# sourceMappingURL=maps/' . str_replace('.css', '.css.map', $component_file) . ' */';
          if (file_exists("{$target}/styles/css/components/{$component_file}")) {
            $fileOperations
              ->fileStrReplace("{$target}/styles/css/components/{$component_file}", $map_string, '');
          }
        }
      }

      // Color.
      if ($color === 0) {
        $directoryOperations
          ->directoryRemove("{$target}/color");
      }
    }

    // Templates.
    if ($subtheme_type === 'standard') {
      $fileOperations
        ->fileStrReplace("{$target}/templates/generated/page.html.twig", 'THEMENAME', $machine_name);
      if ($templates === 1) {
        $directoryOperations
          ->directoryRecursiveCopy("{$path}/templates", "{$target}/templates");
      }
    }
    if ($subtheme_type === 'clone') {
      $cloned_templates = $directoryOperations
        ->directoryScan("{$target}/templates/generated");
      foreach ($cloned_templates as $cloned_template) {
        $fileOperations
          ->fileStrReplace("{$target}/templates/generated/{$cloned_template}", $source_theme, $machine_name);
      }
    }

    // .theme
    if ($subtheme_type === 'standard') {
      $fileOperations
        ->fileRename("{$target}/{$source_theme}.theme", "{$target}/{$machine_name}.theme");
      $fileOperations
        ->fileStrReplace("{$target}/{$machine_name}.theme", 'HOOK', $machine_name);
    }
    if ($subtheme_type === 'clone') {
      $fileOperations
        ->fileRename("{$target}/{$source_theme}.theme", "{$target}/{$machine_name}.theme");
      $fileOperations
        ->fileStrReplace("{$target}/{$machine_name}.theme", $source_theme, $machine_name);
    }

    // libraries
    $fileOperations
      ->fileRename("{$target}/{$source_theme}.libraries.yml", $library_file);

    // theme-settings.php
    if ($subtheme_type === 'standard') {
      if ($theme_settings_file === 1) {
        $fileOperations
          ->fileStrReplace("{$target}/theme-settings.php", 'HOOK', $machine_name);
      }
      else {
        $directoryOperations
          ->directoryRemove("{$target}/theme-settings.php");
      }
    }
    if ($subtheme_type === 'clone') {
      $fileOperations
        ->fileStrReplace("{$target}/theme-settings.php", $source_theme, $machine_name);
    }

    // Config.
    $new_config_file = '';
    foreach ($configuration as $config_path => $config_files) {
      if (is_dir("{$target}/config/{$config_path}")) {
        foreach ($config_files as $config_file) {
          $new_config_file = str_replace($source_theme, $machine_name, $config_file) ?: '';
          $fileOperations
            ->fileRename("{$target}/config/{$config_path}/{$config_file}", "{$target}/config/{$config_path}/{$new_config_file}");
          $fileOperations
            ->fileStrReplace("{$target}/config/{$config_path}/{$new_config_file}", 'TARGET', $target);
          $fileOperations
            ->fileStrReplace("{$target}/config/{$config_path}/{$new_config_file}", $source_theme, $machine_name);
        }
      }
    }
    if ($subtheme_type === 'standard') {
      if ($block_config === 0) {
        if (is_dir("{$target}/config/optional")) {
          $directoryOperations
            ->directoryRemove("{$target}/config/optional");
        }
      }
    }
    if ($subtheme_type === 'clone') {
      $source_config = \Drupal::config($source_theme . '.settings')
        ->get();

      // Empty if the source theme has never been installed, in which case it
      // should be safe to assume there is no new configuration worth saving.
      if (!empty($source_config)) {

        // Remove the default config hash.
        if (array_key_exists('_core', $source_config)) {
          unset($source_config['_core']);
        }
        $old_config = "{$target}/config/install/{$machine_name}.settings.yml";
        $new_config = Yaml::encode($source_config);
        $find_generated_files = "themes/{$source_theme}/styles/css/generated";
        $replace_generated_files = "themes/{$machine_name}/styles/css/generated";
        $new_config = str_replace($find_generated_files, $replace_generated_files, $new_config);
        $fileOperations
          ->fileReplace($new_config, $old_config);
        $fileOperations
          ->fileStrReplace($old_config, $source_theme, $machine_name);
      }
    }

    // Info.
    $fileOperations
      ->fileRename("{$target}/{$source_theme}.info.yml", $info_file);

    // Shortcodes.
    $fileOperations
      ->fileRename("{$target}/{$source_theme}.shortcodes.yml", $shortcodes_file);

    // Parse, rebuild and save the themes info.yml file.
    $theme_info_data = \Drupal::service('info_parser')
      ->parse($info_file);

    // Name and theme type.
    $theme_info_data['name'] = "{$friendly_name}";
    $theme_info_data['type'] = "theme";
    $clone_quotes = array(
      'The shroud of the Dark Side has fallen. Begun, this clone war has.',
      'Blind we are, if creation of this clone army we could not see.',
      'The first step to correcting a mistake is patience.',
      'A single chance is a galaxy of hope.',
      'A very wise jedi once said nothing happens by accident.',
      'Smaller in number we are but larger in mind.',
    );
    $cq = array_rand($clone_quotes);
    $clone_quote = $clone_quotes[$cq];

    // Description.
    $base_theme = $theme_info_data['base theme'];
    if ($subtheme_type === 'clone') {
      $description = $description ? $description . '<br />Clone of: ' . $source_theme : '<i>' . $clone_quote . '</i>' . '<br />Clone of: ' . $source_theme;
    }
    $description = $description ?: $generic_description;
    $theme_info_data['description'] = "{$description}.<br />Base theme: {$base_theme} <br />Machine-name: {$machine_name} <br />Generated: {$datetime}";

    // alt text.
    $theme_info_data['alt text'] = "Screenshot for {$friendly_name}";

    // Version.
    $theme_info_data['version'] = $version ? str_replace(' ', '-', trim($version)) : '8.x-1.0';

    // Regions.
    foreach ($theme_info_data['regions'] as $region_key => $region_name) {
      $theme_info_data['regions'][$region_key] = "{$region_name}";
    }

    // Unset stuff we don't want or need.
    unset($theme_info_data['hidden']);
    unset($theme_info_data['project']);
    unset($theme_info_data['datestamp']);

    // Libraries.
    if (isset($theme_info_data['libraries-extend']['quickedit/quickedit'])) {
      $theme_info_data['libraries-extend']['quickedit/quickedit'] = array(
        $machine_name . '/quickedit',
      );
    }

    // Save the info file.
    $rebuilt_info = $fileOperations
      ->fileBuildInfoYml($theme_info_data);
    $fileOperations
      ->fileReplace($rebuilt_info, $info_file);

    // Set messages, however we may need more validation?

    //----------------------------------------------------------------------

    // system message for Reports.
    $logger_message = t('A new theme <b>@theme_name</b>, with then machine name: <code><b>@machine_name</b></code>, has been generated.', array(
      '@theme_name' => $friendly_name,
      '@machine_name' => $machine_name,
    ));
    \Drupal::logger('at_generator')
      ->notice($logger_message);

    // Message for the user.
    drupal_set_message(t("<p>A new theme <b>@theme_name</b>, with then machine name: <code><b>@machine_name</b></code>, has been generated.</p><p>You can find your theme here: <code><b>@theme_path</b></code> </p><p>Click the List tab to view the themes list and enable your new theme.</p>", array(
      '@theme_name' => $friendly_name,
      '@machine_name' => $machine_name,
      '@theme_path' => $target,
      '@performance_settings' => base_path() . 'admin/config/development/performance',
    )), 'status');

    // Refresh data.
    system_list_reset();
    \Drupal::service('theme_handler')
      ->rebuildThemeData();
  }
  else {
    drupal_set_message(t('Bummer, something went wrong with the machine name, please try again or contact support.'));
  }
}