1

More of a general understanding question then a problem to solve question.

In Drupal core version 10.X.X, is there a PHP file in which a user can freely run global PHP commands that would effect any webpage in the website?

An example command is:

<?php
    echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
?>

I ask about a file where the very last PHP in the queue would be executed, similar to how the very last JS or CSS in the queue would be globally executed at say behavior.js or style.css.

A "Test any PHP globally here, in the last line of the file" kind of file.

How to run global PHP?

0

2 Answers 2

3

Really global would be index.php. But don't change this file. Drupal doesn't want you to change any core files, so they've implemented stacked middlewares where you can run your PHP code before or after the Drupal kernel:

  public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE): Response {
    // Run code before executing the Drupal kernel
    $response = $this->httpKernel->handle($request, $type, $catch);
    // Run code after executing the Drupal kernel
    return $response;
  }

Use drush gen middleware to generate the scaffolding including the service definition:

 mymodule.middleware:
    class: Drupal\mymodule\MymoduleMiddleware
    tags:
      - { name: http_middleware, priority: 1000 }

Use a very high priority if you want to run the first or rather the last PHP code.

0

Yes. Implement an event subscriber as directed in this change record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.