Allow StarterKit forked themes with prefix

Created on 13 September 2024, 6 days ago
Updated 16 September 2024, 2 days ago

Problem/Motivation

When generating a theme using the drupal generate-theme command with any simple and short machine name (e.g., โ€œbasicโ€), a PHP warning and fatal error occur. The issue arises from a str_replace operation that uses an array of find/replace terms, resulting in multiple replacements on the same values, leading to incorrect file paths or names. This causes the .info.yml file to be inaccessible, triggering further errors like passing null to array_merge().

โฏ php public/core/scripts/drupal generate-theme --starterkit galactus --path themes/custom galactus_ta
Generating theme galactus_ta (galactus_ta) from galactus starterkit.
PHP Warning:  file_get_contents(/var/folders/1r/60zvgysn2y7_n__fym7d0_000000gp/T/drupal-starterkit-theme-1af82b4a8b7ab7165cf879b7acf47f9466e37d82d80063.32456974/galactus_ta.info.yml): Failed to open stream: No such file or directory in d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php on line 168

Warning: file_get_contents(/var/folders/1r/60zvgysn2y7_n__fym7d0_000000gp/T/drupal-starterkit-theme-1af82b4a8b7ab7165cf879b7acf47f9466e37d82d80063.32456974/galactus_ta.info.yml): Failed to open stream: No such file or directory in /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php on line 168
PHP Fatal error:  Uncaught TypeError: array_merge(): Argument #1 must be of type array, null given in /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php:170
Stack trace:
#0 /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php(170): array_merge(NULL, Array)
#1 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Command/Command.php(279): Drupal\Core\Command\GenerateTheme->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(1029): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(316): Symfony\Component\Console\Application->doRunCommand(Object(Drupal\Core\Command\GenerateTheme), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(167): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /Users/pittet/Contrib/drupal/d11/public/core/scripts/drupal(30): Symfony\Component\Console\Application->run()
#6 {main}
  thrown in /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php on line 170

Fatal error: Uncaught TypeError: array_merge(): Argument #1 must be of type array, null given in /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php:170
Stack trace:
#0 /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php(170): array_merge(NULL, Array)
#1 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Command/Command.php(279): Drupal\Core\Command\GenerateTheme->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#2 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(1029): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(316): Symfony\Component\Console\Application->doRunCommand(Object(Drupal\Core\Command\GenerateTheme), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /Users/pittet/Contrib/drupal/d11/vendor/symfony/console/Application.php(167): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /Users/pittet/Contrib/drupal/d11/public/core/scripts/drupal(30): Symfony\Component\Console\Application->run()
#6 {main}
  thrown in /Users/pittet/Contrib/drupal/d11/public/core/lib/Drupal/Core/Command/GenerateTheme.php on line 170

~/Contrib/drupal/d11                                                                          16:47:15
โฏ ll /var/folders/1r/60zvgysn2y7_n__fym7d0_000000gp/T/drupal-starterkit-theme-1af82b4a8b7ab7165cf879b7acf47f9466e37d82d80063.32456974/
total 192
...
-rwxr-xr-x   1 pittet  staff    792 Sep 12 16:47 galactusTaTa_ta.breakpoints.yml*
-rw-r--r--   1 pittet  staff   1358 Sep 12 16:47 galactusTaTa_ta.info.yml
-rw-r--r--   1 pittet  staff   6202 Sep 12 16:47 galactusTaTa_ta.libraries.yml
-rw-r--r--   1 pittet  staff  17986 Sep 12 16:47 galactusTaTa_ta.theme
...

As you can see in the temp directory galactusTaTa_ta.info.yml

Steps to reproduce

php core/scripts/drupal generate-theme --starterkit galactus --path themes/custom galactus_ta

Proposed resolution

Refactor the str_replace logic to avoid multiple replacements on the same values. Ensure that the theme generation process handles simple and short machine names correctly without modifying or overwriting necessary values during the find/replace operations.

Remaining tasks

  1. Update the str_replace logic in GenerateTheme.php.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Fixes an issue where theme generation would fail for themes with simple machine names due to incorrect find/replace operations.

๐Ÿ› Bug report
Status

Needs review

Version

11.0 ๐Ÿ”ฅ

Component
Themeย  โ†’

Last updated less than a minute ago

Created by

๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

Live updates comments and jobs are added and updated live.
  • Contributed project blocker

    It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @joelpittet
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

    I'm narrowing down on ๐Ÿ“Œ Making a theme compatible with core's theme generator is too difficult Needs work

    Where this code was introduced in GenerateTheme:

      private static function namePatterns(string $machine_name, string $label): array {
        return [
          'machine_name' => $machine_name,
          'machine_name_camel' => u($machine_name)->camel(),
          'machine_name_pascal' => u($machine_name)->camel()->title(),
          'machine_name_title' => u($machine_name)->title(),
          'label' => $label,
          'label_camel' => u($label)->camel(),
          'label_pascal' => u($label)->camel()->title(),
          'label_title' => u($label)->title(),
        ];
      }
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

    Breaking that out with the Issue Summary example:

    $patterns = [
      'old' => [
        'machine_name' => 'galactus',
        'machine_name_camel' => 'galactus',
        'machine_name_pascal' => 'Galactus',
        'machine_name_title' => 'Galactus',
        'label' => 'Galactus',
        'label_camel' => 'galactus',
        'label_pascal' => 'Galactus',
        'label_title' => 'Galactus',
      ],
      'new' => [
        'machine_name' => 'galactus_ta',
        'machine_name_camel' => 'galactusTa',
        'machine_name_pascal' => 'GalactusTa',
        'machine_name_title' => 'Galactus_ta',
        'label' => 'galactus_ta',
        'label_camel' => 'galactusTa',
        'label_pascal' => 'GalactusTa',
        'label_title' => 'Galactus_ta',
      ],
    ];
    $filename = str_replace($patterns['old'], $patterns['new'], $old_filename);
    
    Why does it always add TaTa_ to the new $filename in these examples
    $old_filename = 'galactus.settings.yml'
    $filename = 'galactusTaTa_ta.settings.yml'
    $old_filename = 'block.block.galactus_mainpagecontent.yml'
    $filename = 'block.block.galactusTaTa_ta_mainpagecontent.yml'
    $old_filename = 'block.block.galactus_pagetitle.yml'
    $filename = 'block.block.galactusTaTa_ta_pagetitle.yml'
    $old_filename = 'block.block.galactus_tabs.yml'
    $filename = 'block.block.galactusTaTa_ta_tabs.yml'
    $old_filename = 'block.block.galactus_primaryadminactions.yml'
    $filename = 'block.block.galactusTaTa_ta_primaryadminactions.yml'
    $old_filename = 'block.block.galactus_main_menu.yml'
    $filename = 'block.block.galactusTaTa_ta_main_menu.yml'
    $old_filename = 'block.block.galactus_breadcrumbs.yml'
    $filename = 'block.block.galactusTaTa_ta_breadcrumbs.yml'
    $old_filename = 'block.block.galactus_help.yml'
    $filename = 'block.block.galactusTaTa_ta_help.yml'
    $old_filename = 'block.block.galactus_drawer.yml'
    $filename = 'block.block.galactusTaTa_ta_drawer.yml'
    $old_filename = 'galactus.schema.yml'
    $filename = 'galactusTaTa_ta.schema.yml'
    $old_filename = 'galactus.theme'
    $filename = 'galactusTaTa_ta.theme'
    $old_filename = 'galactus.libraries.yml'
    $filename = 'galactusTaTa_ta.libraries.yml'
    $old_filename = 'galactus.breakpoints.yml'
    $filename = 'galactusTaTa_ta.breakpoints.yml'
    $old_filename = 'galactus.info.yml'
    $filename = 'galactusTaTa_ta.info.yml'
    

    It applies to more than just files, it's only that this is easy to represent.

    Still working on a solution but it would be nice to know why so many variations for the replacements were needed in the first place.

  • Status changed to Needs review 5 days ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

    This affects themes like https://www.drupal.org/project/basic โ†’ which I would like to be a starterkit theme.

  • Pipeline finished with Failed
    5 days ago
    Total: 580s
    #282888
  • Status changed to Needs work 2 days ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Seems to have an error.

    Could issue summary also be filled out please

    Thanks.

  • Status changed to Needs review 2 days ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joelpittet Vancouver

    @smustgrave, I don't have a fix but the tests do demo the problem.

    Updated the issue summary to help make this clearer

  • Pipeline finished with Failed
    2 days ago
    Total: 791s
    #284883
Production build 0.71.5 2024