Porting https://github.com/joomla/joomla-cms/pull/3914 part 2