- How to upgrade Drupal to PHP 8 with Composer
- Step by step upgrade guide
- Step 1: upgrade everything to latest versions + database updates
- Step 2: upgrade your host machine / VM / containers to PHP 8
- Step 3: set your composer.json PHP version
- Step 3: Check your code for compatibility
- Drupal 10 features
- Is there a Drupal 10?
- Drupal 10 release date
- What are the PHP requirements for Drupal 10?
- What are the database requirements for Drupal 10?
- New front-end theme
- Drupal 9.1 and its compatibility with PHP 8 – Learn what’s new and how to check compatibility
- What’s new with PHP 8 (Notable Changes)
- 1. JIT Compiler
- 2. The null safe operator
- 3. Named argument
- 4. Match expression
- How to perform a Compatibility Check with PHP 8 on Drupal
- STEP 1: Drupal Installation
- STEP 2: Installing the required Package
- STEP 3: Compatibility Check
How to upgrade Drupal to PHP 8 with Composer
Drupal 10 (release end 2022) needs PHP 8. It will not be possible to run Drupal 10 on PHP 7 or earlier.
- Drupal 8 does not support PHP8, you will have issues
- Starting from Drupal 9.3, Drupal is compatible with both > PHP7.1 and > PHP8.1
Step by step upgrade guide
Step 1: upgrade everything to latest versions + database updates
The first thing you would need to do is take everything to the latest versions. This means: the latest Drupal core (> 9.3) and packages
Step 2: upgrade your host machine / VM / containers to PHP 8
There are several ways you can host your local projects.
For most Docker / Vagrant solutions (like Lando, Drupal VM, etc.) this is frequently configurable in a file. Here’s an example for Lando:
Now restart your container / virtual machines!
Step 3: set your composer.json PHP version
Now that the PHP version of your server is updated, it’s time to make the change in your composer.json file.
Change the PHP version in the require section of composer.json.
"require": < "php": ">=8.1", "drush/drush": "^10.3", . >,
In the config section of composer.json change the PHP version. Platform config will help make sure that your host PHP version and project PHP version stay in their lane.
composer update --with-all-dependencies
Just like you are used to.
Step 3: Check your code for compatibility
Below commands will check for both current/future Drupal AND current PHP version deprecations
composer require --dev phpstan/phpstan phpstan/extension-installer mglaman/phpstan-drupal phpstan/phpstan-deprecation-rules
Run the command to check deprecations in your custom modules
Check deprecations in custom modules and theme
phpstan analyze web/modules/custom
phpstan analyze web/themes/custom
This will return a list in this form:
------ -------------------------------------------------------------------- Line mymodule/src/Form/CustomConfigForm.php ------ -------------------------------------------------------------------- 160 Call to deprecated function file_save_data(): in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::writeData() instead. 264 Method Drupal\vito\Form\CustomConfigForm::FormatExcelLine() should return array but return statement is missing. ------ -------------------------------------------------------------------- Line mymodule/mymodule.module ------ --------------------------------------------- 123 Call to deprecated function date_sunrise(). ------ ---------------------------------------------
Fix the errors and check again.
Some typical errors you will encounter:
Drupal 10 features
Drupal 10 is the next major iteration of our beloved open-source web content management system. In this post, I answer some of the most common and important questions about the upcoming release.
Is there a Drupal 10?
No, not yet. As of today (April 19, 2022), there is only an early alpha release of Drupal 10. That is not something you want to use for building your next project. The early alpha release is intended for module and theme developers to test whether their code is compatible with new changes.
Drupal 10 release date
The scheduled release date for Drupal 10 is December 14, 2022.
Upgrading CKEditor from version 4 to version 5 has proved more difficult than originally thought, and that’s why releasing Drupal 10 in August is no longer possible. The CKEditor 5 code has been written from scratch, so it’s no wonder that integration with Drupal is taking a long time.
What are the PHP requirements for Drupal 10?
Drupal 10 will require PHP 8.1. That means that it won’t be possible to run Drupal 10 on PHP 8.0 or earlier. Just don’t use versions 8.1.0 to 8.1.5 because these versions have a bug that may cause fatal errors on your website. PHP version 8.1.6 is recommended.
Check the official change record to find out more information.
What are the database requirements for Drupal 10?
Drupal 10 requires MariaDB, MySQL/Percona, PostgreSQL or SQLite database.
Database | Version | Required extensions |
---|---|---|
MariaDB | 10.3.7+ | PDO database |
MySQL/Percona | 5.7.8+ | PDO database |
PostgreSQL | 12+ | pg_trgm |
SQLite | 3.26+ | json1 |
Did you know that MongoDB is supported by a contributed module? There is no Drupal 10 compatible release yet, but that might change very soon.
New front-end theme
The default front-end theme for Drupal 10 will be Olivero, described as a clean, accessible, and flexible Drupal front-end theme. Olivero has a modern look and feel and is currently available as a stable theme in Drupal 9, so you can already use it.
Drupal 9.1 and its compatibility with PHP 8 – Learn what’s new and how to check compatibility
PHP 8 is here and is now supported in Drupal 9.1 and its dependencies! November 2020 saw the big release of PHP 8. We call it a big release because of the exciting new features and optimizations it comes loaded with (which we will be talking about shortly).
Drupal 8.9 and 9.0 are however marked incompatible with PHP 8. They are still compatible with PHP 7.3 and PHP 7.4 – which happens to be the last major PHP update. PHP 7.4 will stop receiving active support from the community from November 2021. And thus, updating your website to Drupal 9.1 will be a good idea now.
Drupal 10, which is scheduled to release in June 2022, will mandate compatibility with PHP 8. Read on to find out about the amazing features PHP 8 has to offer and how you can check if your Drupal version is compatible with PHP 8.
What’s new with PHP 8 (Notable Changes)
1. JIT Compiler
JIT stands for just-in-time compilation. Starting from PHP 5.5, Zend VM became part of PHP. But 8.0 introduced JIT to address some long struggling PHP performance issues. For better performance PHP was based on OPCache using OPCode. This is precompiled code given to the processor as commands. However, it is not very native to the machine language. On the other hand, JIT provides actual machine code with a mechanism to work together with OPCache. JIT does not work automatically. We need to configure this in the php.ini file.
2. The null safe operator
You must be familiar with the null coalescing operator (??) which worked as:
It checks for the null value of $var1 and returns it. If it is not null, it returns $var2. But it does not work with method calls. Here, the null safe operator comes into the picture.
$value = $obj->getData()?->getValue();
Here you can call the getValue() method; even if no method $obj->getData() returns null, the code will not crash. It will return null. On the other hand, using the null coalescing operator:
$value = $obj->getData()->getValue() ?? null;
3. Named argument
PHP 8 allows you to now pass named arguments to functions. It does not depend upon the argument order. Instead, you can pass the argument name.
function named_arg_example(String $arg1, $string $arg2, $string $arg3) <> named_arg_example( arg1: ‘arg1 value’, arg3: ‘arg3 value’, arg2: ‘arg2 value’, );
4. Match expression
Match expression is like the switch statement, except that it does not require a break statement.
$value = match($check) < 0 =>‘Value is zero’, 1, 2, 3 => ‘Value is non zero and less than 4’’ >
There are many other great new features added to PHP 8 like the constructor property promotion, Attributes, Constant type errors for internal functions, Saner string to number comparison, etc. More details can be found here.
How to perform a Compatibility Check with PHP 8 on Drupal
You can use this method to check if your version of Drupal is compatible with PHP 8 or not. For that, you will need to first make sure you have the required package – phpcompatibility. For more information on this, visit here.
Next, you should already be having Drupal installed. If not, you will need to install Drupal 9 in your system. Using composer to install Drupal is the recommended way. For information about composer installation please refer this document.
STEP 1: Drupal Installation
Use this Composer command to install recommended version of Drupal
composer create-project drupal/recommended-project [my_site_name_dir]
You will need to change [my_site_name_dir] with the folder name you want to install Drupal into.
STEP 2: Installing the required Package
After installing Drupal, you will have composer.json in your Drupal root directory. Open it in text editor and add the following code:
If you already have require-dev section in your composer.json file, just add
«phpcompatibility/php-compatibility»: «*» to the list.
Next, you need to provide location info to the PHP code sniffer by adding the following lines to composer.json
It will install phpcompatibility and other required packages.
STEP 3: Compatibility Check
Now, use this command to check PHP compatibility for the project
vendor/bin/phpcs -p [directorypath] --standard=PHPCompatibility --runtime-set testVersion [php version] --extensions=[file extensions] --report-full==[path/to/report-file]
You need to replace the directory path with the directory path that the test will run on. In our case it is ‘.’ because we want to run the test in current directory (All Drupal files and folder). You should also replace the [php version] with the version you want to check compatibility with — which in this case will be 8.0. Replace the [file extensions] with file extensions like php, module, inc, install, etc. The report-full gives you the flexibility to store the report log in a file. So you will need to provide the path for the log file.
So, for our case, the command will be:
vendor/bin/phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 8.0 --extensions=php,module,install,inc --report-full==./drupal9-php8-compatibility.txt
It will take a few minutes and you will get a drupal9-php8-compatibility.txt file, where you can check the reported log.
PHP 8 has been a major change as it has brought about some useful and attractive features and optimizations. Drupal has been adopting modern technologies and libraries since Drupal 8 and is riding along the technology wave ever since. Drupal 9.1 released in December 2020 and supports PHP 8. Time to upgrade to Drupal 9 now to leverage the best of opensource!
If you haven’t already migrated to Drupal 8, now is the time. Migrate to Drupal 8 and then enjoy easy updates to Drupal 9 and the subsequent versions.