The basic idea to solve the problem contains of three basic ideas:
map_credits() part as a string: as long as you create it as a function, QGIS 3.16 will not recognize the function and prompt an error.
Then use the function
eval() (see documentation) to convert the string to a function.
eval()-function should be called only for QGIS versions larger than 3.x (I don't know exactly when
map_credits() was introduced). Use an additional
if-condition for that. So the string containing
map_credits() will only be evaluated (converted from string to actual function) for QGIS versions > 3.x.
The expression will look something like this (see the
map_credits() part on line 11) - this exact expression works in my QGIS 3.16 installation.
Be aware: inside the
eval() function, everythis is a string, enclosed in single quotes
'. Single quotes that appear inside this string must be masked by two additional single quotes - thus
''' (3 single quotes) evaluates as
' (1 single quote),
'''' (4 single quotes) as
'' (2 single quotes = empty string).
@qgis_short_version < 3.18,
WHEN @qgis_short_version <3.22 -- change the QGIS version here
THEN 'Fond de carte: © Swisstopo'
WHEN @qgis_short_version <3.22
THEN ''Fond de carte: © Swisstopo''
WHEN map_credits(''carte1'') IS NULL
ELSE ''source: © ''||array_to_string( array_sort( map_credits(''carte1'')), '', '')