Fix Your Craft CMS Deprecation Errors

We don't want the headache of fixing the deprecated code during the upgrade from Craft 3 to Craft 4, so let's take of it now.

Image

Back in 2018, when Craft 3.0 was released, the Craft CMS offi­cial­ly dep­re­cat­ed some Craft 2 and pri­or con­fig­u­ra­tion set­tings, PHP con­stants, tem­plate tags, tem­plate func­tions, query para­me­ters, query meth­ods, ele­ment prop­er­ties, mod­el meth­ods, and locales methods.

So, a lot of stuff. 

But some of those dep­re­cat­ed items might still be lurk­ing in ours projects. Maybe we’ve avoid­ed look­ing that Dep­re­ca­tion Warn­ings sec­tions of Util­i­ties in the Craft con­trol pan­el. Or new dep­re­ca­tion warn­ings popped up because some old code wasn’t called frequently.

Either way, we need to fix our sh‑t.

Why? Because what is now dep­re­cat­ed and still work­ing in Craft 3 will like­ly be com­plete­ly removed and bro­ken in Craft 4

When the Craft CMS team dep­re­cat­ed a tem­plate func­tion, for exam­ple, they kind­ly log a dep­re­ca­tion warn­ing to alert you, and then call the new func­tion for you so every­thing would work.

But that sup­port for the old func­tion will be gone in Craft 4. And, as a result, your code will just fail and error.

We don’t want the headache of fix­ing the dep­re­cat­ed code dur­ing the upgrade from Craft 3 to Craft 4, so let’s take of it now.

Find­ing Deprec Warnings

The one chal­lenge, espe­cial­ly on larg­er projects with sev­er­al dozen tem­plates, is that dep­re­ca­tion warn­ings are only logged when the code is exe­cut­ed. If you have a infre­quent­ly-vis­it­ed ren­dered tem­plate con­tain­ing some dep­re­cat­ed code, it might not yet be logged. 

For that rea­son, I sug­gest a mul­ti-pronged approach to rid­ding your project of all dep­re­cat­ed code. 

Dep­re­ca­tion Warn­ings Utility

The first method is to use the Dep­re­ca­tion Warn­ings utl­i­tiy in the Craft con­trol pan­el. This gives you a his­tor­i­cal view of all dep­re­ca­tion warn­ings since the log was last cleared. These warn­ings are saved in your Craft CMS data­base, so they per­sist between con­trol pan­el sessions.

P.S. If you want to keep the dep­re­ca­tion warn­ings in your face between now and Craft 4, you can use the Deprec Wid­get we build in the Build­ing a Craft CMS Dash­board Widget

What I do is review the Dep­re­ca­tion Warn­ing Util­i­ty first and, one-by-one, and clear each warn­ing as I fix it. After I clear out all dep­re­ca­tion warn­ings, I then re-test the site and mon­i­tor for any­thing I missed.

Yii Debug Toolbar

One good way to catch any dep­re­ca­tion warn­ings as you work is to use the Yii Debug Toolbar. 

Load your site local­ly and open the debug tool­bar. If you don’t have the debug tool­bar enabled, read this or watch this video to learn how it works.

There’s a Dep­re­ca­tion Warn­ing tab. You can nav­i­gate through the main pages of the site and keep and eye on the tab to see if it reads out any­thing oth­er than zero. 

What’s Dep­re­cat­ed and Going Away in Craft 4?

Here’s a table of the areas of Craft where there are dep­re­ca­tions in Craft 3 and removal in Craft 4.

ItemDescrip­tionDocs
Con­fig settingsEight con­fig set­tings are going away in Craft.Learn more
PHP Con­stantsFor plu­g­in devel­op­ers, two Craft PHP con­stants have changed.Learn more
Tem­plate TagsSome tem­plate tags relat­ed to includ­ing assets will be removed or changed.Learn more
Tem­plate FunctionsSev­er­al tem­plate func­tions are going away com­plete­ly, while oth­ers will have replace­ments. Too many to list!Learn more
Ele­ment QueriesThis is where you’ll like­ly have the major­i­ty of your dep­re­ca­tion warn­ings through­out your Twig tem­plates. The big one is treat­ing queries as arrays. Use .all() for all ele­ment queries now.Learn more
Ele­mentsOne change with the tag ele­ment and the removal of the locale prop­er­ties from all ele­ments (because of mul­ti-site in Craft 3)Learn more

Note: This does not cov­er changes to plu­g­in devel­op­ment in Craft 4.