Last Call for Craft 4

Max Myers, Senior Platform Developer

Article Categories: #Code, #Content Management, #Security, #Performance

Posted on

Why now is the time to upgrade from Craft 3.

Craft 3 is great. It brought many improvements and truly modernized Craft. It introduced the Craft world to dotenv files, revamped the structure of the app, and brought along a simplified, composer-based install. It also required the use of PHP 7 (but you should upgrade to PHP 8 as soon as possible). If your site is still on Craft 3, however, there’s a huge issue: there will be no more security updates! Here’s why now is the time to upgrade from Craft 3.

Craft 3 has reached its end-of-life


Pixel & Tonic, the team behind Craft, stopped supporting Craft 3 on April 30, 2023. This means that there have been no new features developed for the core of Craft 3. That may be fine if you’re comfortable with a stagnant site, but the more important issue here is security. Craft 3 will stop receiving critical security updates on April 30, 2024. If a vulnerability is found in Craft, a plugin, or an older version of PHP, your site could be a target for hackers. Sometimes that means malicious actors could remotely execute their own code on your site, making it behave in a way you didn’t intend. Attackers could also trigger a DoS (denial of service) attack on your site through a vulnerability in a plugin.

Why should you use Craft 4?


Craft 4, much like its predecessor, improves upon an already great content management system. Besides the security benefits of using the latest version of Craft, you’ll also see performance and accessibility improvements along with a large focus on author experience.

Improved performance with saving entries

Craft 4 is much faster for saving entries with complex content models using Matrix, Neo, or Super Table fields with multiple levels of nesting. You’ll notice a substantial difference when creating drafts or revisions.

Accessibility  

Craft 4 has made huge strides towards making the CMS accessible to all. The team has pushed to make Craft WCAG AA compliant, going so far as to hire a Lead Accessibility Engineer to help achieve this goal. There have been improvements to the author experience with a focus on increased contrast and more compatibility for assistive device support like screen readers (there’s a new alternative text field for assets now).

Author Experience

There are some big changes to the control panel, making it even easier for authors to get work done. Here’s a few examples of those improvements:


Element Index Filtering

It’s easier than ever to find the elements you’re looking for. Craft 4 includes many built-in element condition rules. You can filter by Entry Type, Author, and more.

Element Editor Slideouts

Ever wish you could edit an entry while you’re editing another entry without having to browse to another page? In Craft 4, you can edit entries within entries thanks to improvements to the element editor slideout. It’s easy to edit your content no matter what page you’re on.

Conditional Fields and Relations

Craft 4 has the power to make fields visible or editable conditionally. You can set field layouts or tabs to show only if certain conditions are met, like a user belonging to a certain user group or the status of an entry. Relational fields can now have more settings for controlling which content should be relatable beyond the source settings in Craft 3. For instance, you can specify that you only want the author to be able to select entries from the Work section with further qualifiers, like only entries that have a field that starts with a certain phrase, or entries from the Posts channel that have a thumbnail added. These are really powerful tools that can help you control your content and the way it gets edited.


Upgrading to Craft 4
 

So what do you think, are you convinced? If you are, here’s what you need to do to prepare for your upgrade:

Make sure your site is running PHP 8

  • We’ve discussed this previously, but this is both a requirement of Craft 4 and a good security practice if you’re still running PHP 7 (or earlier).
  • Once you’ve upgraded to PHP 8 and handled any deprecations in custom code, modules, or plugins, you’re in a good position to upgrade Craft.


Look over your environment variables

  • Make sure you clean up any unused variables and account for any new ones your site might need for running Craft 4.


Upgrade to the latest version of Craft 3

  • It is important that you commit and deploy this change before proceeding so that any migrations can run prior to the upgrade.
  • In addition, Craft 3.7.40.1 included a Craft 4 Upgrade Utility which, when run, will show you all of your plugins and whether there’s a compatible version for Craft 4. If you have plugins that aren’t compatible, you’ll need to find replacements. 

Check your custom modules for compatibility

  • We like to use a combination of tools to accomplish this. Our IDE of choice, PhpStorm, includes some code inspection that will give hints on any issues with your code compatibility.
  • Craft also includes some Rector rules for upgrading 3 to 4. These can automatically update some code and are super helpful.
  • I’d also recommend using PHPStan (trying to get to level 1 or 2 is a good goal) as well as phpstan/phpstan-deprecation-rules to auto-catch deprecations.

Update your app’s files

  • ./craft and ./web/index.php files have changed, so make sure to grab the latest changes versions from the Craft Starter Project.

Use .collect() and .count()

  • Now is a good time to make some improvements to your code. Craft 4 brings an implementation of Laravel Collections which allows you to write some cleaner code. You can replace instances of if field and if field | length > 0 with .collect() and .count(). So much cleaner!

Add eager-loading in your templates

  • We love using Blitz Recommendations to check for eager-loading opportunities and make sure your template queries are as optimized as possible.

Check your cache configuration

If you’re using Blitz Cache, double check your config as some things have shifted a bit in Craft 4.

Check for visual regressions

  • You don’t want anything to change the frontend appearance of your site when upgrading Craft. At Viget, we’ve grown quite fond of BackstopJS. It’s easy to integrate and run tests to compare versions of your pages to ensure any changes you make don’t break your design.

Update your assets for accessibility

  • Craft 4 added an altText field to assets. Now is also a good time to add alt text to your assets. _Don’t do this if your site has translations, however.

Check your config files

  • Some settings have changed in Craft 4. Be sure to look closely at your ./config directory and make any necessary changes.

Update your asset volumes

  • The way craft handles asset volumes has also changed in Craft 4. Filesystems and Volumes are now separated and require some additional configuration.

If you’re using DDEV (we highly recommend it), there are some extra steps you can take. The command ddev snapshot will take a backup at that point in time, making it easy to undo database changes if you need to go back for any reason. You can also update your project type to craftcms instead of php after upgrading. This project type is more opinionated about some settings that work well with Craft 4.



After you’ve had a chance to go through all the notes above, you’re ready to upgrade! Follow the official Craft 4 upgrade guide to make sure you don’t miss anything and your site upgrade should be smooth. For most sites, the upgrade can be performed in a day or two by your developer (more complicated sites can take a bit longer, but it’s still not that scary — I promise). The best part is, the upgrade to Craft 4 is free with your existing license. 

Don’t fall behind on your site’s critical maintenance — upgrade now. Craft 5 is already in beta, so you’ll be in a much better position to upgrade if your site is already running Craft 4. 

If you still have questions or would like assistance from Craft experts who have upgraded a multitude of large, complex websites, get in touch.

Max Myers

Max is a Senior Platform Developer based in Michigan with extensive experience building robust e-commerce platforms and rebuilding vintage Kawasaki motorcycles.

More articles by Max

Related Articles