Modulo by Zero Error when Migrating to Joomla 4

We are currently migrating a Joomla 3 website to Joomla 4. Among the hundreds of errors that we saw during the migration process, there was this one:

Modulo by Zero

This error was interesting, because it was just weird and very short. It meant that we were dividing by zero somewhere in the code, but where? Before giving you the details of our investigation, let us explain the modulo concept for the non-developers/non-mathematicians who are reading this post.

Modulo means the integer remainder of a division of one number by the other. For example, 9 Modulo 2 (this is written 9 % 2 in PHP code) is 1, 73 Modulo 7 is 3, and 170 Modulo 19 is 18 (19 is a prime number). Of course, some people may think this function is useless, but it is very handy for all developers, and for many of those who work with numbers.

So, what caused the modulo problem?

When we see any problem, we first switch the template to a basic one, to see if the problem is caused by the template (we later disable other extensions until the problem is no more). So, in this scenario, we switched to the Cassiopeia default Joomla 4 template (by the way, in case you are wondering just like we were wondering, Cassiopeia is the name of a constellation) and the problem was suddenly no more. Investigating the problem deeper, we found that it was caused by the following line in the default file of the featured layout:

$rowcount= (((int)$key-1) % (int) $this->columns) +1;

In the previous version of Joomla (Joomla 3), $this->columns used to return a default value of 1 (when left empty in the backend), in Joomla 4, $this->columns returns just empty, which translates to zero when cast to integer. This very change caused the problem.

But how was the problem fixed?

Fixing the problem simply consisted of adding the following 2 lines of code just before the problematic line:

if (empty($this->columns))
	$this->columns = 1;

Of course, there was a big bunch of other problems after fixing this one, but, it was a step in the right direction.

Now if you have the same problem (or any other problem) while trying to migrate your website to Joomla 4, then you can just contact us. Our fees are always competitive, we are very knowledgeable in Joomla, and we are extremely humble (while the last 2 sentences may seem like an oxymoron, they really are not)!

No comments yet.

Leave a comment