Redo of https://github.com/joomla/joomla-cms/pull/4375