diff --git a/administrator/components/com_actionlogs/src/Field/ExtensionField.php b/administrator/components/com_actionlogs/src/Field/ExtensionField.php index ec5e03c45d300..46f37013491b4 100644 --- a/administrator/components/com_actionlogs/src/Field/ExtensionField.php +++ b/administrator/components/com_actionlogs/src/Field/ExtensionField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Field\ListField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; @@ -41,7 +40,7 @@ class ExtensionField extends ListField */ public function getOptions() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT ' . $db->quoteName('extension')) ->from($db->quoteName('#__action_logs')) diff --git a/administrator/components/com_actionlogs/src/Field/LogcreatorField.php b/administrator/components/com_actionlogs/src/Field/LogcreatorField.php index ba25bcbf89338..0db00673751ca 100644 --- a/administrator/components/com_actionlogs/src/Field/LogcreatorField.php +++ b/administrator/components/com_actionlogs/src/Field/LogcreatorField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Field\ListField; /** @@ -53,7 +52,7 @@ protected function getOptions() { static::$options[$hash] = parent::getOptions(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Construct the query $query = $db->getQuery(true) diff --git a/administrator/components/com_actionlogs/src/Field/LogtypeField.php b/administrator/components/com_actionlogs/src/Field/LogtypeField.php index 8d1e94420ff0d..6bffb3de8267b 100644 --- a/administrator/components/com_actionlogs/src/Field/LogtypeField.php +++ b/administrator/components/com_actionlogs/src/Field/LogtypeField.php @@ -12,7 +12,6 @@ \defined('_JEXEC') or die; use Joomla\CMS\Application\ApplicationHelper; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Field\ListField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; @@ -42,7 +41,7 @@ class LogtypeField extends ListField */ public function getOptions() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('extension')) ->from($db->quoteName('#__action_logs_extensions')); diff --git a/administrator/components/com_actionlogs/src/Field/PlugininfoField.php b/administrator/components/com_actionlogs/src/Field/PlugininfoField.php index 84b78f2d2c911..6b225cee279e7 100644 --- a/administrator/components/com_actionlogs/src/Field/PlugininfoField.php +++ b/administrator/components/com_actionlogs/src/Field/PlugininfoField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\FormField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; @@ -41,7 +40,7 @@ class PlugininfoField extends FormField */ protected function getInput() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('extension_id')) ->from($db->quoteName('#__extensions')) diff --git a/administrator/components/com_categories/src/Field/CategoryeditField.php b/administrator/components/com_categories/src/Field/CategoryeditField.php index 5b152ad6477f6..4a8ccec8e8d22 100644 --- a/administrator/components/com_categories/src/Field/CategoryeditField.php +++ b/administrator/components/com_categories/src/Field/CategoryeditField.php @@ -173,7 +173,7 @@ protected function getOptions() ? (int) reset($oldCat) : (int) $oldCat; - $db = Factory::getDbo(); + $db = $this->getDatabase(); $user = Factory::getUser(); $query = $db->getQuery(true) diff --git a/administrator/components/com_categories/src/Field/ComponentsCategoryField.php b/administrator/components/com_categories/src/Field/ComponentsCategoryField.php index 02ac5b4d73e50..8fd9838dd588a 100644 --- a/administrator/components/com_categories/src/Field/ComponentsCategoryField.php +++ b/administrator/components/com_categories/src/Field/ComponentsCategoryField.php @@ -41,7 +41,7 @@ class ComponentsCategoryField extends ListField protected function getOptions() { // Initialise variable. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $options = array(); $query = $db->getQuery(true); diff --git a/administrator/components/com_categories/src/Field/Modal/CategoryField.php b/administrator/components/com_categories/src/Field/Modal/CategoryField.php index 10fb8eb30a95e..70c1f5ff18237 100644 --- a/administrator/components/com_categories/src/Field/Modal/CategoryField.php +++ b/administrator/components/com_categories/src/Field/Modal/CategoryField.php @@ -121,7 +121,7 @@ protected function getInput() if ($value) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__categories')) diff --git a/administrator/components/com_config/src/Field/ConfigComponentsField.php b/administrator/components/com_config/src/Field/ConfigComponentsField.php index a153c0cfa2e81..74ff24c9d0210 100644 --- a/administrator/components/com_config/src/Field/ConfigComponentsField.php +++ b/administrator/components/com_config/src/Field/ConfigComponentsField.php @@ -41,7 +41,7 @@ class ConfigComponentsField extends ListField */ protected function getOptions() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('name AS text, element AS value') ->from('#__extensions') diff --git a/administrator/components/com_config/src/Field/FiltersField.php b/administrator/components/com_config/src/Field/FiltersField.php index bd93d5cdef376..052c548face8b 100644 --- a/administrator/components/com_config/src/Field/FiltersField.php +++ b/administrator/components/com_config/src/Field/FiltersField.php @@ -157,7 +157,7 @@ protected function getInput() protected function getUserGroups() { // Get a database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Get the user groups from the database. $query = $db->getQuery(true); diff --git a/administrator/components/com_contact/src/Field/Modal/ContactField.php b/administrator/components/com_contact/src/Field/Modal/ContactField.php index 1130bfe0b754c..a67d64497d0df 100644 --- a/administrator/components/com_contact/src/Field/Modal/ContactField.php +++ b/administrator/components/com_contact/src/Field/Modal/ContactField.php @@ -110,7 +110,7 @@ protected function getInput() if ($value) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('name')) ->from($db->quoteName('#__contact_details')) diff --git a/administrator/components/com_content/src/Field/Modal/ArticleField.php b/administrator/components/com_content/src/Field/Modal/ArticleField.php index 709930415446c..d37a1c3296a67 100644 --- a/administrator/components/com_content/src/Field/Modal/ArticleField.php +++ b/administrator/components/com_content/src/Field/Modal/ArticleField.php @@ -113,7 +113,7 @@ protected function getInput() if ($value) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__content')) diff --git a/administrator/components/com_fields/src/Field/ComponentsFieldgroupField.php b/administrator/components/com_fields/src/Field/ComponentsFieldgroupField.php index 7cfaadc741adb..5be9ceb888fb7 100644 --- a/administrator/components/com_fields/src/Field/ComponentsFieldgroupField.php +++ b/administrator/components/com_fields/src/Field/ComponentsFieldgroupField.php @@ -43,7 +43,7 @@ class ComponentsFieldgroupField extends ListField protected function getOptions() { // Initialise variable. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT a.name AS text, a.element AS value') diff --git a/administrator/components/com_fields/src/Field/ComponentsFieldsField.php b/administrator/components/com_fields/src/Field/ComponentsFieldsField.php index 47f5b9efdad34..b55336b2316e3 100644 --- a/administrator/components/com_fields/src/Field/ComponentsFieldsField.php +++ b/administrator/components/com_fields/src/Field/ComponentsFieldsField.php @@ -43,7 +43,7 @@ class ComponentsFieldsField extends ListField protected function getOptions() { // Initialise variable. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT a.name AS text, a.element AS value') diff --git a/administrator/components/com_fields/src/Field/FieldLayoutField.php b/administrator/components/com_fields/src/Field/FieldLayoutField.php index 2ea362c8579ad..bf45f58c8107c 100644 --- a/administrator/components/com_fields/src/Field/FieldLayoutField.php +++ b/administrator/components/com_fields/src/Field/FieldLayoutField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Folder; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Form\FormField; @@ -49,7 +48,7 @@ protected function getInput() if ($extension) { // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Build the query. diff --git a/administrator/components/com_fields/src/Field/FieldgroupsField.php b/administrator/components/com_fields/src/Field/FieldgroupsField.php index 5475eb4267cda..c8a8b24746f68 100644 --- a/administrator/components/com_fields/src/Field/FieldgroupsField.php +++ b/administrator/components/com_fields/src/Field/FieldgroupsField.php @@ -15,7 +15,6 @@ use Joomla\CMS\Form\Field\ListField; use Joomla\Utilities\ArrayHelper; - /** * Fields Groups * @@ -44,7 +43,7 @@ protected function getOptions() $user = Factory::getUser(); $viewlevels = ArrayHelper::toInteger($user->getAuthorisedViewLevels()); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); $query->select( [ diff --git a/administrator/components/com_fields/src/Model/FieldModel.php b/administrator/components/com_fields/src/Model/FieldModel.php index 53633531ed44c..88d488aafdf12 100644 --- a/administrator/components/com_fields/src/Model/FieldModel.php +++ b/administrator/components/com_fields/src/Model/FieldModel.php @@ -24,6 +24,9 @@ use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Table\Table; use Joomla\Component\Fields\Administrator\Helper\FieldsHelper; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseInterface; +use Joomla\Database\Exception\DatabaseNotFoundException; use Joomla\Database\ParameterType; use Joomla\Registry\Registry; use Joomla\String\StringHelper; @@ -301,7 +304,9 @@ private function checkDefaultValue($data) $node = $dom->appendChild(new \DOMElement('form')); // Trigger the event to create the field dom node - Factory::getApplication()->triggerEvent('onCustomFieldsPrepareDom', array($obj, $node, new Form($data['context']))); + $form = new Form($data['context']); + $form->setDatabase($this->getDatabase()); + Factory::getApplication()->triggerEvent('onCustomFieldsPrepareDom', array($obj, $node, $form)); // Check if a node is created if (!$node->firstChild) @@ -321,6 +326,19 @@ private function checkDefaultValue($data) return true; } + if ($rule instanceof DatabaseAwareInterface) + { + try + { + $rule->setDatabase($this->getDatabase()); + } + catch (DatabaseNotFoundException $e) + { + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); + $rule->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); + } + } + try { // Perform the check diff --git a/administrator/components/com_finder/src/Field/ContentmapField.php b/administrator/components/com_finder/src/Field/ContentmapField.php index 2f72812f33b11..b01ee28a80190 100644 --- a/administrator/components/com_finder/src/Field/ContentmapField.php +++ b/administrator/components/com_finder/src/Field/ContentmapField.php @@ -44,7 +44,7 @@ protected function getGroups() $groups = array(); // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Levels subquery. $levelQuery = $db->getQuery(true); diff --git a/administrator/components/com_finder/src/Field/ContenttypesField.php b/administrator/components/com_finder/src/Field/ContenttypesField.php index 6c0765532abf2..cefd638be56c2 100644 --- a/administrator/components/com_finder/src/Field/ContenttypesField.php +++ b/administrator/components/com_finder/src/Field/ContenttypesField.php @@ -45,7 +45,7 @@ public function getOptions() $lang = Factory::getLanguage(); $options = array(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('id', 'value')) ->select($db->quoteName('title', 'text')) diff --git a/administrator/components/com_finder/src/Field/SearchfilterField.php b/administrator/components/com_finder/src/Field/SearchfilterField.php index d823d298ade41..d784b75cc1f7d 100644 --- a/administrator/components/com_finder/src/Field/SearchfilterField.php +++ b/administrator/components/com_finder/src/Field/SearchfilterField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die(); -use Joomla\CMS\Factory; use Joomla\CMS\Form\Field\ListField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; @@ -41,7 +40,7 @@ class SearchfilterField extends ListField public function getOptions() { // Build the query. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('f.title AS text, f.filter_id AS value') ->from($db->quoteName('#__finder_filters') . ' AS f') diff --git a/administrator/components/com_menus/src/Field/MenuItemByTypeField.php b/administrator/components/com_menus/src/Field/MenuItemByTypeField.php index 9808e55ae352e..5df789ff8640e 100644 --- a/administrator/components/com_menus/src/Field/MenuItemByTypeField.php +++ b/administrator/components/com_menus/src/Field/MenuItemByTypeField.php @@ -188,7 +188,7 @@ protected function getGroups() if ($menuType) { // If the menutype is empty, group the items by menutype. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__menu_types')) diff --git a/administrator/components/com_menus/src/Field/MenuOrderingField.php b/administrator/components/com_menus/src/Field/MenuOrderingField.php index ea8ff5dad96c8..b0c89afd2bd5c 100644 --- a/administrator/components/com_menus/src/Field/MenuOrderingField.php +++ b/administrator/components/com_menus/src/Field/MenuOrderingField.php @@ -51,7 +51,7 @@ protected function getOptions() return false; } - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/administrator/components/com_menus/src/Field/MenuParentField.php b/administrator/components/com_menus/src/Field/MenuParentField.php index b823603431cca..1eaa109792acd 100644 --- a/administrator/components/com_menus/src/Field/MenuParentField.php +++ b/administrator/components/com_menus/src/Field/MenuParentField.php @@ -42,7 +42,7 @@ protected function getOptions() { $options = array(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/administrator/components/com_menus/src/Field/Modal/MenuField.php b/administrator/components/com_menus/src/Field/Modal/MenuField.php index 69b2b21f16864..092c1cc3b3529 100644 --- a/administrator/components/com_menus/src/Field/Modal/MenuField.php +++ b/administrator/components/com_menus/src/Field/Modal/MenuField.php @@ -231,7 +231,7 @@ protected function getInput() if ($value) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__menu')) diff --git a/administrator/components/com_messages/src/Field/UserMessagesField.php b/administrator/components/com_messages/src/Field/UserMessagesField.php index 6f140d98a1794..8f18bd5304463 100644 --- a/administrator/components/com_messages/src/Field/UserMessagesField.php +++ b/administrator/components/com_messages/src/Field/UserMessagesField.php @@ -40,7 +40,7 @@ class UserMessagesField extends UserField protected function getGroups() { // Compute usergroups - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('id') ->from('#__usergroups'); diff --git a/administrator/components/com_newsfeeds/src/Field/Modal/NewsfeedField.php b/administrator/components/com_newsfeeds/src/Field/Modal/NewsfeedField.php index 7faa019326b5c..3dfdaa5a5b186 100644 --- a/administrator/components/com_newsfeeds/src/Field/Modal/NewsfeedField.php +++ b/administrator/components/com_newsfeeds/src/Field/Modal/NewsfeedField.php @@ -111,7 +111,7 @@ protected function getInput() if ($value) { $id = (int) $value; - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('name')) ->from($db->quoteName('#__newsfeeds')) diff --git a/administrator/components/com_newsfeeds/src/Field/NewsfeedsField.php b/administrator/components/com_newsfeeds/src/Field/NewsfeedsField.php index 6aeda151088b0..e85fa5578b1fb 100644 --- a/administrator/components/com_newsfeeds/src/Field/NewsfeedsField.php +++ b/administrator/components/com_newsfeeds/src/Field/NewsfeedsField.php @@ -40,7 +40,7 @@ protected function getOptions() { $options = array(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/administrator/components/com_plugins/src/Field/PluginorderingField.php b/administrator/components/com_plugins/src/Field/PluginorderingField.php index 98cdf1e77fab4..ddf8fe6089189 100644 --- a/administrator/components/com_plugins/src/Field/PluginorderingField.php +++ b/administrator/components/com_plugins/src/Field/PluginorderingField.php @@ -11,7 +11,6 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Field\OrderingField; /** @@ -36,7 +35,7 @@ class PluginorderingField extends OrderingField */ protected function getQuery() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $folder = $this->form->getValue('folder'); // Build the query for the ordering list. diff --git a/administrator/components/com_workflow/src/Field/ComponentsWorkflowField.php b/administrator/components/com_workflow/src/Field/ComponentsWorkflowField.php index d68ae7f0f4ebe..681abc8f7b090 100644 --- a/administrator/components/com_workflow/src/Field/ComponentsWorkflowField.php +++ b/administrator/components/com_workflow/src/Field/ComponentsWorkflowField.php @@ -43,7 +43,7 @@ class ComponentsWorkflowField extends ListField protected function getOptions() { // Initialise variable. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT a.name AS text, a.element AS value') diff --git a/libraries/src/Form/Field/AliastagField.php b/libraries/src/Form/Field/AliastagField.php index 834fb69fd0fdc..98fbf5a8245db 100644 --- a/libraries/src/Form/Field/AliastagField.php +++ b/libraries/src/Form/Field/AliastagField.php @@ -39,7 +39,7 @@ class AliastagField extends ListField protected function getOptions() { // Get list of tag type alias - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/AuthorField.php b/libraries/src/Form/Field/AuthorField.php index cebea649f9743..463f1de3f8d2f 100644 --- a/libraries/src/Form/Field/AuthorField.php +++ b/libraries/src/Form/Field/AuthorField.php @@ -10,8 +10,6 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; - /** * Form Field to load a list of content authors * @@ -51,7 +49,7 @@ protected function getOptions() { static::$options[$hash] = parent::getOptions(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Construct the query $query = $db->getQuery(true) diff --git a/libraries/src/Form/Field/CalendarField.php b/libraries/src/Form/Field/CalendarField.php index 099c66b1d33a2..8a48558d2b604 100644 --- a/libraries/src/Form/Field/CalendarField.php +++ b/libraries/src/Form/Field/CalendarField.php @@ -245,7 +245,7 @@ protected function getInput() { case 'SERVER_UTC': // Convert a date to UTC based on the server timezone. - if ($this->value && $this->value != Factory::getDbo()->getNullDate()) + if ($this->value && $this->value != $this->getDatabase()->getNullDate()) { // Get a date object based on the correct timezone. $date = Factory::getDate($this->value, 'UTC'); @@ -257,7 +257,7 @@ protected function getInput() break; case 'USER_UTC': // Convert a date to UTC based on the user timezone. - if ($this->value && $this->value != Factory::getDbo()->getNullDate()) + if ($this->value && $this->value != $this->getDatabase()->getNullDate()) { // Get a date object based on the correct timezone. $date = Factory::getDate($this->value, 'UTC'); @@ -270,7 +270,7 @@ protected function getInput() } // Format value when not nulldate ('0000-00-00 00:00:00'), otherwise blank it as it would result in 1970-01-01. - if ($this->value && $this->value != Factory::getDbo()->getNullDate() && strtotime($this->value) !== false) + if ($this->value && $this->value != $this->getDatabase()->getNullDate() && strtotime($this->value) !== false) { $tz = date_default_timezone_get(); date_default_timezone_set('UTC'); diff --git a/libraries/src/Form/Field/ChromestyleField.php b/libraries/src/Form/Field/ChromestyleField.php index 3ba8eb377aa26..25698275c592e 100644 --- a/libraries/src/Form/Field/ChromestyleField.php +++ b/libraries/src/Form/Field/ChromestyleField.php @@ -11,7 +11,6 @@ \defined('JPATH_PLATFORM') or die; use Joomla\CMS\Application\ApplicationHelper; -use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Folder; use Joomla\CMS\Form\Form; use Joomla\CMS\HTML\HTMLHelper; @@ -228,7 +227,7 @@ protected function getTemplateModuleStyles() */ protected function getTemplates() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Get the database object and a new query object. $query = $db->getQuery(true); diff --git a/libraries/src/Form/Field/ComponentlayoutField.php b/libraries/src/Form/Field/ComponentlayoutField.php index 9fdf704fdf2fb..122e9449757d1 100644 --- a/libraries/src/Form/Field/ComponentlayoutField.php +++ b/libraries/src/Form/Field/ComponentlayoutField.php @@ -90,7 +90,7 @@ protected function getInput() || $lang->load($extension . '.sys', JPATH_ADMINISTRATOR . '/components/' . $extension); // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Build the query. diff --git a/libraries/src/Form/Field/ComponentsField.php b/libraries/src/Form/Field/ComponentsField.php index 7f38571582fcf..085aa6cbf2246 100644 --- a/libraries/src/Form/Field/ComponentsField.php +++ b/libraries/src/Form/Field/ComponentsField.php @@ -38,7 +38,7 @@ class ComponentsField extends ListField */ protected function getOptions() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/ContenttypeField.php b/libraries/src/Form/Field/ContenttypeField.php index 279714de63d93..294933ed2bb54 100644 --- a/libraries/src/Form/Field/ContenttypeField.php +++ b/libraries/src/Form/Field/ContenttypeField.php @@ -63,7 +63,7 @@ protected function getInput() protected function getOptions() { $lang = Factory::getLanguage(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/EditorField.php b/libraries/src/Form/Field/EditorField.php index be0a77cf69724..8ee458a13de61 100644 --- a/libraries/src/Form/Field/EditorField.php +++ b/libraries/src/Form/Field/EditorField.php @@ -292,7 +292,7 @@ protected function getEditor() $types = $this->editorType; // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Build the query. $query = $db->getQuery(true) diff --git a/libraries/src/Form/Field/FrontendlanguageField.php b/libraries/src/Form/Field/FrontendlanguageField.php index 9349291d3d99a..869f060b65405 100644 --- a/libraries/src/Form/Field/FrontendlanguageField.php +++ b/libraries/src/Form/Field/FrontendlanguageField.php @@ -38,7 +38,7 @@ class FrontendlanguageField extends ListField protected function getOptions() { // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); $query->select('a.lang_code AS value, a.title AS text') diff --git a/libraries/src/Form/Field/MenuField.php b/libraries/src/Form/Field/MenuField.php index c9d7d5753ab1f..a61e55dbaabb5 100644 --- a/libraries/src/Form/Field/MenuField.php +++ b/libraries/src/Form/Field/MenuField.php @@ -46,7 +46,7 @@ protected function getGroups() $accessType = (string) $this->element['accesstype']; $showAll = (string) $this->element['showAll'] === 'true'; - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/MenuitemField.php b/libraries/src/Form/Field/MenuitemField.php index a812ddbae6b24..f644fc1b439c4 100644 --- a/libraries/src/Form/Field/MenuitemField.php +++ b/libraries/src/Form/Field/MenuitemField.php @@ -10,7 +10,6 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; use Joomla\CMS\HTML\HTMLHelper; use Joomla\Component\Menus\Administrator\Helper\MenusHelper; @@ -180,7 +179,7 @@ protected function getGroups() if ($menuType) { // If the menutype is empty, group the items by menutype. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title')) ->from($db->quoteName('#__menu_types')) diff --git a/libraries/src/Form/Field/ModulelayoutField.php b/libraries/src/Form/Field/ModulelayoutField.php index cead3e7e0f81f..9eb1197fee581 100644 --- a/libraries/src/Form/Field/ModulelayoutField.php +++ b/libraries/src/Form/Field/ModulelayoutField.php @@ -88,7 +88,7 @@ protected function getInput() || $lang->load($module . '.sys', $client->path . '/modules/' . $module); // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Build the query. diff --git a/libraries/src/Form/Field/OrderingField.php b/libraries/src/Form/Field/OrderingField.php index f016b33dd7355..35753b471ff17 100644 --- a/libraries/src/Form/Field/OrderingField.php +++ b/libraries/src/Form/Field/OrderingField.php @@ -10,7 +10,6 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\FormField; use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\UCM\UCMType; @@ -172,7 +171,7 @@ protected function getQuery() $title = $ucmMapCommon[0]->core_title; } - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); $query->select([$db->quoteName($ordering, 'value'), $db->quoteName($title, 'text')]) ->from($db->quoteName(json_decode($ucmRow->table)->special->dbtable)) diff --git a/libraries/src/Form/Field/PluginsField.php b/libraries/src/Form/Field/PluginsField.php index f822829f314c8..6904223eeba29 100644 --- a/libraries/src/Form/Field/PluginsField.php +++ b/libraries/src/Form/Field/PluginsField.php @@ -120,7 +120,7 @@ protected function getOptions() if (!empty($folder)) { // Get list of plugins - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/RulesField.php b/libraries/src/Form/Field/RulesField.php index c8552bd395cf3..5966b3a8f5373 100644 --- a/libraries/src/Form/Field/RulesField.php +++ b/libraries/src/Form/Field/RulesField.php @@ -11,7 +11,6 @@ \defined('JPATH_PLATFORM') or die; use Joomla\CMS\Access\Access; -use Joomla\CMS\Factory; use Joomla\CMS\Form\FormField; use Joomla\CMS\Helper\UserGroupsHelper; use Joomla\Database\ParameterType; @@ -200,7 +199,7 @@ protected function getInput() if (empty($this->assetId)) { // Get the component asset id as fallback. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('id')) ->from($db->quoteName('#__assets')) @@ -223,7 +222,7 @@ protected function getInput() if (!$this->isGlobalConfig) { // In this case we need to get the component rules too. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('parent_id')) diff --git a/libraries/src/Form/Field/SqlField.php b/libraries/src/Form/Field/SqlField.php index 36e9af81b0b1a..6a9d286a210c4 100644 --- a/libraries/src/Form/Field/SqlField.php +++ b/libraries/src/Form/Field/SqlField.php @@ -199,7 +199,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null) protected function processQuery($conditions, $filters, $defaults) { // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Get the query object $query = $db->getQuery(true); @@ -279,7 +279,7 @@ protected function getOptions() if ($this->query) { // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Set the query and get the result list. $db->setQuery($this->query); diff --git a/libraries/src/Form/Field/TagField.php b/libraries/src/Form/Field/TagField.php index b7f9b8bdfd786..50adf5d78fe7a 100644 --- a/libraries/src/Form/Field/TagField.php +++ b/libraries/src/Form/Field/TagField.php @@ -136,7 +136,7 @@ protected function getOptions() $prefillLimit = 30; $isRemoteSearch = $this->isRemoteSearch(); - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select( [ diff --git a/libraries/src/Form/Field/TemplatestyleField.php b/libraries/src/Form/Field/TemplatestyleField.php index 49f2e409b758a..2b09c4d8f7343 100644 --- a/libraries/src/Form/Field/TemplatestyleField.php +++ b/libraries/src/Form/Field/TemplatestyleField.php @@ -141,7 +141,7 @@ protected function getGroups() $template = $this->template; // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Build the query. diff --git a/libraries/src/Form/Field/TransitionField.php b/libraries/src/Form/Field/TransitionField.php index 47acaeb8f42e7..b3f87f871dda3 100644 --- a/libraries/src/Form/Field/TransitionField.php +++ b/libraries/src/Form/Field/TransitionField.php @@ -101,7 +101,7 @@ protected function getOptions() $jinput = Factory::getApplication()->input; // Initialise variable. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $extension = $this->extension; $workflowStage = (int) $this->workflowStage; diff --git a/libraries/src/Form/Field/WorkflowstageField.php b/libraries/src/Form/Field/WorkflowstageField.php index 94d4e040fc1a9..2760a7d5ac583 100644 --- a/libraries/src/Form/Field/WorkflowstageField.php +++ b/libraries/src/Form/Field/WorkflowstageField.php @@ -92,7 +92,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null) */ protected function getGroups() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Select distinct stages for existing articles diff --git a/libraries/src/Form/Form.php b/libraries/src/Form/Form.php index 3a1cac1210f54..d6baa18dee275 100644 --- a/libraries/src/Form/Form.php +++ b/libraries/src/Form/Form.php @@ -14,6 +14,10 @@ use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Object\CMSObject; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; +use Joomla\Database\DatabaseInterface; +use Joomla\Database\Exception\DatabaseNotFoundException; use Joomla\Registry\Registry; use Joomla\Utilities\ArrayHelper; @@ -30,6 +34,8 @@ */ class Form { + use DatabaseAwareTrait; + /** * The Registry data store for form fields during display. * @@ -1613,6 +1619,19 @@ protected function loadField($element, $group = null, $value = null) // Load the FormField object for the field. $field = FormHelper::loadFieldType($type); + if ($field instanceof DatabaseAwareInterface) + { + try + { + $field->setDatabase($this->getDatabase()); + } + catch (DatabaseNotFoundException $e) + { + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); + $field->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); + } + } + // If the object could not be loaded, get a text field object. if ($field === false) { diff --git a/libraries/src/Form/FormFactory.php b/libraries/src/Form/FormFactory.php index d97008d07e9c1..27fc62af3ee36 100644 --- a/libraries/src/Form/FormFactory.php +++ b/libraries/src/Form/FormFactory.php @@ -10,6 +10,8 @@ \defined('_JEXEC') or die; +use Joomla\Database\DatabaseAwareTrait; + /** * Default factory for creating Form objects * @@ -17,6 +19,8 @@ */ class FormFactory implements FormFactoryInterface { + use DatabaseAwareTrait; + /** * Method to get an instance of a form. * @@ -29,6 +33,10 @@ class FormFactory implements FormFactoryInterface */ public function createForm(string $name, array $options = array()): Form { - return new Form($name, $options); + $form = new Form($name, $options); + + $form->setDatabase($this->getDatabase()); + + return $form; } } diff --git a/libraries/src/Form/FormField.php b/libraries/src/Form/FormField.php index d328421e3bf69..0c460c6433b73 100644 --- a/libraries/src/Form/FormField.php +++ b/libraries/src/Form/FormField.php @@ -10,11 +10,16 @@ \defined('JPATH_PLATFORM') or die; +use Joomla\CMS\Factory; use Joomla\CMS\Filter\InputFilter; use Joomla\CMS\Form\Field\SubformField; use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\FileLayout; use Joomla\CMS\Log\Log; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; +use Joomla\Database\DatabaseInterface; +use Joomla\Database\Exception\DatabaseNotFoundException; use Joomla\Registry\Registry; use Joomla\String\Normalise; use Joomla\String\StringHelper; @@ -24,8 +29,10 @@ * * @since 1.7.0 */ -abstract class FormField +abstract class FormField implements DatabaseAwareInterface { + use DatabaseAwareTrait; + /** * The description text for the form field. Usually used in tooltips. * @@ -1242,6 +1249,19 @@ public function validate($value, $group = null, Registry $input = null) throw new \UnexpectedValueException(sprintf('%s::validate() rule `%s` missing.', \get_class($this), $type)); } + if ($rule instanceof DatabaseAwareInterface) + { + try + { + $rule->setDatabase($this->getDatabase()); + } + catch (DatabaseNotFoundException $e) + { + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); + $rule->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); + } + } + try { // Run the field validation rule test. @@ -1258,6 +1278,19 @@ public function validate($value, $group = null, Registry $input = null) // Load the subform validation rule. $rule = FormHelper::loadRuleType('Subform'); + if ($rule instanceof DatabaseAwareInterface) + { + try + { + $rule->setDatabase($this->getDatabase()); + } + catch (DatabaseNotFoundException $e) + { + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); + $rule->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); + } + } + try { // Run the field validation rule test. diff --git a/libraries/src/Form/Rule/EmailRule.php b/libraries/src/Form/Rule/EmailRule.php index a4a64b373fe13..65c59f3b5d396 100644 --- a/libraries/src/Form/Rule/EmailRule.php +++ b/libraries/src/Form/Rule/EmailRule.php @@ -11,11 +11,12 @@ \defined('JPATH_PLATFORM') or die; use Joomla\CMS\Component\ComponentHelper; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormRule; use Joomla\CMS\Language\Text; use Joomla\CMS\String\PunycodeHelper; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; use Joomla\Registry\Registry; @@ -24,8 +25,10 @@ * * @since 1.7.0 */ -class EmailRule extends FormRule +class EmailRule extends FormRule implements DatabaseAwareInterface { + use DatabaseAwareTrait; + /** * The regular expression to use in testing a form field value. * @@ -177,7 +180,7 @@ public function test(\SimpleXMLElement $element, $value, $group = null, Registry if ($unique && !$multiple) { // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Get the extra field check attribute. diff --git a/libraries/src/Form/Rule/ExistsRule.php b/libraries/src/Form/Rule/ExistsRule.php index 5ddab544d421a..1852d29eb9960 100644 --- a/libraries/src/Form/Rule/ExistsRule.php +++ b/libraries/src/Form/Rule/ExistsRule.php @@ -10,9 +10,10 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormRule; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Registry\Registry; /** @@ -20,8 +21,10 @@ * * @since 3.9.0 */ -class ExistsRule extends FormRule +class ExistsRule extends FormRule implements DatabaseAwareInterface { + use DatabaseAwareTrait; + /** * Method to test the username for uniqueness. * @@ -56,7 +59,7 @@ public function test(\SimpleXMLElement $element, $value, $group = null, Registry $existsColumn = 'id'; } - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Set and query the database. $exists = $db->setQuery( diff --git a/libraries/src/Form/Rule/UserIdRule.php b/libraries/src/Form/Rule/UserIdRule.php index 84facef6b5e89..870d240a6e2ac 100644 --- a/libraries/src/Form/Rule/UserIdRule.php +++ b/libraries/src/Form/Rule/UserIdRule.php @@ -10,9 +10,10 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormRule; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; use Joomla\Registry\Registry; @@ -21,8 +22,10 @@ * * @since 4.0.0 */ -class UserIdRule extends FormRule +class UserIdRule extends FormRule implements DatabaseAwareInterface { + use DatabaseAwareTrait; + /** * Method to test the validity of a Joomla User. * @@ -50,7 +53,7 @@ public function test(\SimpleXMLElement $element, $value, $group = null, Registry } // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Build the query. diff --git a/libraries/src/Form/Rule/UsernameRule.php b/libraries/src/Form/Rule/UsernameRule.php index 2364068c84b1a..8d5fea60565b4 100644 --- a/libraries/src/Form/Rule/UsernameRule.php +++ b/libraries/src/Form/Rule/UsernameRule.php @@ -10,9 +10,10 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormRule; +use Joomla\Database\DatabaseAwareInterface; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\ParameterType; use Joomla\Registry\Registry; @@ -21,8 +22,10 @@ * * @since 1.7.0 */ -class UsernameRule extends FormRule +class UsernameRule extends FormRule implements DatabaseAwareInterface { + use DatabaseAwareTrait; + /** * Method to test the username for uniqueness. * @@ -41,7 +44,7 @@ class UsernameRule extends FormRule public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null) { // Get the database object and a new query object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Get the extra field check attribute. diff --git a/libraries/src/MVC/Factory/MVCFactory.php b/libraries/src/MVC/Factory/MVCFactory.php index 3d427184bb6db..22dc70e4049c7 100644 --- a/libraries/src/MVC/Factory/MVCFactory.php +++ b/libraries/src/MVC/Factory/MVCFactory.php @@ -141,7 +141,7 @@ public function createModel($name, $prefix = '', array $config = []) } catch (DatabaseNotFoundException $e) { - @trigger_error(sprintf('Database must be set, this will not be catched anymore in 5.0.'), E_USER_DEPRECATED); + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); $model->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); } } @@ -242,7 +242,7 @@ public function createTable($name, $prefix = '', array $config = []) } catch (DatabaseNotFoundException $e) { - @trigger_error(sprintf('Database must be set, this will not be catched anymore in 5.0.'), E_USER_DEPRECATED); + @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); $db = Factory::getContainer()->get(DatabaseInterface::class); } diff --git a/libraries/src/Service/Provider/Form.php b/libraries/src/Service/Provider/Form.php index 4b4fe9107c9dd..9da32019abee2 100644 --- a/libraries/src/Service/Provider/Form.php +++ b/libraries/src/Service/Provider/Form.php @@ -12,6 +12,7 @@ use Joomla\CMS\Form\FormFactory; use Joomla\CMS\Form\FormFactoryInterface; +use Joomla\Database\DatabaseInterface; use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; @@ -39,7 +40,10 @@ public function register(Container $container) FormFactoryInterface::class, function (Container $container) { - return new FormFactory; + $factory = new FormFactory; + $factory->setDatabase($container->get(DatabaseInterface::class)); + + return $factory; }, true ); diff --git a/plugins/content/confirmconsent/src/Field/ConsentBoxField.php b/plugins/content/confirmconsent/src/Field/ConsentBoxField.php index 6cb0dd6fbba33..a750ac4dfd245 100644 --- a/plugins/content/confirmconsent/src/Field/ConsentBoxField.php +++ b/plugins/content/confirmconsent/src/Field/ConsentBoxField.php @@ -242,7 +242,7 @@ protected function getLayoutData() */ private function getAssignedArticleUrl() { - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Get the info from the article $query = $db->getQuery(true) @@ -331,7 +331,7 @@ private function getAssignedMenuItemUrl() if (Multilanguage::isEnabled()) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName(['id', 'language'])) ->from($db->quoteName('#__menu')) diff --git a/plugins/system/privacyconsent/src/Field/PrivacyField.php b/plugins/system/privacyconsent/src/Field/PrivacyField.php index e2e628fcb4dd8..c282407b9cd9a 100644 --- a/plugins/system/privacyconsent/src/Field/PrivacyField.php +++ b/plugins/system/privacyconsent/src/Field/PrivacyField.php @@ -83,7 +83,7 @@ protected function getLayoutData() if ($privacyArticle && Factory::getApplication()->isClient('site')) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName(['id', 'alias', 'catid', 'language'])) ->from($db->quoteName('#__content')) @@ -105,7 +105,7 @@ protected function getLayoutData() if (Multilanguage::isEnabled()) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName(['id', 'language'])) ->from($db->quoteName('#__menu')) diff --git a/plugins/user/profile/src/Field/TosField.php b/plugins/user/profile/src/Field/TosField.php index c9decbbcdb1ad..0455ebc47c0d0 100644 --- a/plugins/user/profile/src/Field/TosField.php +++ b/plugins/user/profile/src/Field/TosField.php @@ -90,7 +90,7 @@ protected function getLabel() $attribs['data-bs-toggle'] = 'modal'; $attribs['data-bs-target'] = '#tosModal'; - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); $query->select($db->quoteName(['id', 'alias', 'catid', 'language'])) diff --git a/plugins/user/terms/src/Field/TermsField.php b/plugins/user/terms/src/Field/TermsField.php index 67328b0049aef..1de1e11b0e68e 100644 --- a/plugins/user/terms/src/Field/TermsField.php +++ b/plugins/user/terms/src/Field/TermsField.php @@ -81,7 +81,7 @@ protected function getLayoutData() if ($termsArticle && Factory::getApplication()->isClient('site')) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName(['id', 'alias', 'catid', 'language'])) ->from($db->quoteName('#__content'))