Unverified Commit 3fb1c69d authored by Alex Pott's avatar Alex Pott
Browse files

task: #3619733 Deprecate .module file extension

By: nicxvan
By: larowlan
By: berdir
By: xjm
By: alexpott
By: catch
By: ressa
parent c80b5d1f
Loading
Loading
Loading
Loading
Loading
+7 −5
Changes for core/lib/Drupal/Core/Hook/Attribute/ExtensionFileIsConverted.php: 7 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -7,14 +7,16 @@
/**
 * Defines a ExtensionFileIsConverted attribute object.
 *
 * This prevents deprecation messages for .theme files to allow legacy tests or
 * supporting multiple versions of core. It must be on a function in the .theme
 * file. This must only be used for legacy support, the extension must fully
 * work without the file in versions >=11.3.0. If the minimum supported version
 * of core is 11.3.0 then this attribute must not be used.
 * This prevents deprecation messages for .theme or .module files to allow
 * legacy tests or supporting multiple versions of core. It must be on the
 * first function in the .theme or .module file. This must only be used for
 * legacy support, the extension must fully work without the file in versions
 * >=11.3.0. If the minimum supported version of core is 11.3.0 then this
 * attribute must not be used.
 *
 * @see https://www.drupal.org/node/3581222
 * @see https://www.drupal.org/node/3551652
 * @see https://www.drupal.org/node/3619765
 */
#[\Attribute(\Attribute::TARGET_FUNCTION)]
class ExtensionFileIsConverted {}
+60 −10
Changes for core/lib/Drupal/Core/Hook/HookCollectorPass.php: 60 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Utility\OpCodeCache;
use Drupal\Core\Extension\ProceduralCall;
use Drupal\Core\Hook\Attribute\ExtensionFileIsConverted;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Hook\Attribute\HookAttributeInterface;
use Drupal\Core\Hook\Attribute\HookDependsOnModule;
@@ -95,6 +96,15 @@ class HookCollectorPass extends HookCollectorBase implements CompilerPassInterfa
   */
  protected array $preprocessForSuggestions;

  /**
   * Deprecated extension files.
   *
   * These are stored to allow emitting deprecation messages.
   *
   * @var array<string, array<string, true>>
   */
  protected array $deprecatedFiles = [];

  /**
   * Constructor.
   *
@@ -143,6 +153,12 @@ protected function writeToContainer(ContainerBuilder $container): void {
      'preprocess_for_suggestions' => $this->preprocessForSuggestions ?? [],
      'packed_order_operations' => $packed_order_operations,
    ]);

    foreach ($this->deprecatedFiles as $fileExtension => $deprecatedFiles) {
      foreach ($this->deprecatedFiles[$fileExtension] ?? [] as $deprecatedFile => $v) {
        @trigger_error('Code in ' . $deprecatedFile . '.' . $fileExtension . ' is deprecated in drupal:11.5.0 and must be converted before drupal:13.0.0. Use classes instead. See https://www.drupal.org/node/3619765', E_USER_DEPRECATED);
      }
    }
  }

  /**
@@ -328,20 +344,24 @@ protected static function getModuleListPattern(array $module_list): string {
   *   Skip the procedural check for the current module.
   */
  protected function collectModuleHookImplementations($dir, $module, $current_module_preg, $all_modules_preg, bool $skip_procedural): void {
    $hook_file_cache = FileCacheFactory::get('hook_implementations');
    $hook_file_cache = FileCacheFactory::get('hook_implementations:1');
    // List of modules must be included in the cache key to ensure rediscovery
    // takes place when a new module is installed. Some modules define hooks
    // on behalf of other modules and those hooks need to be found.
    // Hash to prevent massive key sizes.
    $procedural_hook_file_cache = FileCacheFactory::get('procedural_hook_implementations:' . hash('xxh3', $all_modules_preg));
    $procedural_hook_file_cache = FileCacheFactory::get('procedural_hook_implementations:1:' . hash('xxh3', $all_modules_preg));

    $procedural_files = array_map(fn (string $extension): string => "$module.$extension", $this->proceduralFileExtensions);
    foreach ($this->getHookFileIterator($dir, $procedural_files) as $fileinfo) {
      assert($fileinfo instanceof \SplFileInfo);
      $extension = $fileinfo->getExtension();
      $fileExtension = $fileinfo->getExtension();
      $filename = $fileinfo->getPathname();

      if ($extension === 'php') {
      if ($fileExtension === 'module' || $fileExtension === 'profile') {
        $this->deprecatedFiles[$fileExtension][pathinfo($filename, PATHINFO_FILENAME)] = TRUE;
      }

      if ($fileExtension === 'php') {
        $cached = $hook_file_cache->get($filename);
        if ($cached) {
          $class = $cached['class'];
@@ -421,19 +441,43 @@ protected function collectModuleHookImplementations($dir, $module, $current_modu
          }
        }
      }
      elseif (!$skip_procedural) {
      else {
        $implementations = $procedural_hook_file_cache->get($filename);
        if ($implementations === NULL) {
          $finder = MockFileFinder::create($filename);
          $parser = new StaticReflectionParser('', $finder);
          $implementations = [];
          $implementations = [
            'hooks' => [],
          ];
          $hasNonLegacyTaggedFunction = FALSE;
          $hasSkipAttribute = FALSE;
          foreach ($parser->getMethodAttributes() as $function => $attributes) {
            if (StaticReflectionParser::hasAttribute($attributes, ProceduralHookScanStop::class)) {
            if (StaticReflectionParser::hasAttribute($attributes, ExtensionFileIsConverted::class)) {
              $implementations['@skip_extension_file_deprecation'] = TRUE;
              break;
            }

            // We cannot skip deprecation when $skip_procedural is TRUE or
            // ProceduralHookScanStop is set because we do not know if there
            // are other api functions.
            // Here we track if we hit a function that does not have a legacy
            // attribute so we can auto suppress the deprecation.
            $legacy_attributes = [LegacyHook::class, LegacyRequirementsHook::class];
            if (!static::hasAnyAttribute($attributes, $legacy_attributes) && (preg_match($current_module_preg, $function, $matches) || preg_match($all_modules_preg, $function, $matches))) {

            if (static::hasAnyAttribute($attributes, $legacy_attributes)) {
              continue;
            }
            $hasNonLegacyTaggedFunction = TRUE;

            // If $skip_procedural is TRUE or ProceduralHookScanStop is set
            // then we do not need to add any functions to the hook cache, but
            // we continue scanning for non hook methods to emit the
            // deprecation.
            if ($skip_procedural || $hasSkipAttribute || StaticReflectionParser::hasAttribute($attributes, ProceduralHookScanStop::class)) {
              $hasSkipAttribute = TRUE;
              continue;
            }
            if ((preg_match($current_module_preg, $function, $matches) || preg_match($all_modules_preg, $function, $matches))) {
              // Skip hooks that are not supported by the new hook system, they
              // do not need to be added to the BC layer. Note that is different
              // from static::checkForProceduralOnlyHooks(). hook_requirements
@@ -456,12 +500,18 @@ protected function collectModuleHookImplementations($dir, $module, $current_modu
              }

              assert($function === $matches['module'] . '_' . $matches['hook']);
              $implementations[] = ['module' => $matches['module'], 'hook' => $matches['hook']];
              $implementations['hooks'][] = ['module' => $matches['module'], 'hook' => $matches['hook']];
            }
          }
          if (!$hasNonLegacyTaggedFunction) {
            $implementations['@skip_extension_file_deprecation'] = TRUE;
          }
          $procedural_hook_file_cache->set($filename, $implementations);
        }
        foreach ($implementations as $implementation) {
        if (isset($implementations['@skip_extension_file_deprecation'])) {
          unset($this->deprecatedFiles[$fileExtension][pathinfo($filename, PATHINFO_FILENAME)]);
        }
        foreach ($implementations['hooks'] as $implementation) {
          $this->addProceduralImplementation($implementation['hook'], $implementation['module']);
        }
      }
+6 −5
Changes for core/lib/Drupal/Core/Hook/ThemeHookCollectorPass.php: 6 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ protected function writeToContainer(ContainerBuilder $container): void {
    ]);

    foreach ($this->deprecatedThemeFiles as $deprecatedThemeFile => $v) {
      @trigger_error('Using ' . $deprecatedThemeFile . '.theme is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use classes instead. See https://www.drupal.org/node/3581222', E_USER_DEPRECATED);
      @trigger_error('Code in ' . $deprecatedThemeFile . '.theme is deprecated in drupal:11.5.0 and must be converted before drupal:13.0.0. Use classes instead. See https://www.drupal.org/node/3581222', E_USER_DEPRECATED);
    }
  }

@@ -223,8 +223,8 @@ protected static function getThemeListPattern(array $themeList): string {
   *   Skip the procedural check for the current theme.
   */
  protected function collectThemeHookImplementations($dir, $theme, $currentThemePreg, $allThemesPreg, bool $shouldSkipProceduralScan): void {
    $hookFileCache = FileCacheFactory::get('theme_hook_implementations');
    $proceduralHookFileCache = FileCacheFactory::get('theme_procedural_hook_implementations:' . $allThemesPreg);
    $hookFileCache = FileCacheFactory::get('theme_hook_implementations:1');
    $proceduralHookFileCache = FileCacheFactory::get('theme_procedural_hook_implementations:1:' . $allThemesPreg);

    foreach ($this->getHookFileIterator($dir, ["$theme.theme"]) as $fileinfo) {
      assert($fileinfo instanceof \SplFileInfo);
@@ -273,7 +273,7 @@ protected function collectThemeHookImplementations($dir, $theme, $currentThemePr
          }
        }
      }
      elseif (!$shouldSkipProceduralScan) {
      else {
        $implementations = $proceduralHookFileCache->get($filename);
        if ($implementations === NULL) {
          $finder = MockFileFinder::create($filename);
@@ -284,8 +284,9 @@ protected function collectThemeHookImplementations($dir, $theme, $currentThemePr
          foreach ($parser->getMethodAttributes() as $function => $attributes) {
            if (StaticReflectionParser::hasAttribute($attributes, ExtensionFileIsConverted::class)) {
              $implementations['@skip_theme_file_deprecation'] = TRUE;
              break;
            }
            if (StaticReflectionParser::hasAttribute($attributes, ProceduralHookScanStop::class)) {
            if ($shouldSkipProceduralScan || StaticReflectionParser::hasAttribute($attributes, ProceduralHookScanStop::class)) {
              break;
            }
            if (!StaticReflectionParser::hasAttribute($attributes, LegacyHook::class) && (preg_match($currentThemePreg, $function, $matches) || preg_match($allThemesPreg, $function, $matches))) {
+2
Changes for core/modules/automated_cron/automated_cron.module: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Hook\Attribute\ExtensionFileIsConverted;

/**
 * Form submission handler for system_cron_settings().
@@ -15,6 +16,7 @@
 * @see https://www.drupal.org/node/3566774
 * @see \Drupal\automated_cron\Hook\AutomatedCronHooks::automatedCronSettingsSubmit()
 */
#[ExtensionFileIsConverted]
function automated_cron_settings_submit(array $form, FormStateInterface $form_state): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no replacement. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  \Drupal::configFactory()->getEditable('automated_cron.settings')
+2
Changes for core/modules/block/block.module: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 */

use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Hook\Attribute\ExtensionFileIsConverted;

/**
 * Assigns an initial, default set of blocks for a theme.
@@ -22,6 +23,7 @@
 *
 * @see https://www.drupal.org/node/3566783
 */
#[ExtensionFileIsConverted]
function block_theme_initialize($theme): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. No replacement is provided. See https://www.drupal.org/node/3566783', E_USER_DEPRECATED);

Loading