Problem/Motivation

Error: Call to undefined method Drupal\Core\Database\Driver\mysql\Connection::getConnection() in Drupal\abjs\Form\AbjsConditionForm->saveCondition() (line 171 of modules/contrib/abjs/src/Form/AbjsConditionForm.php)

Update: also a similar issue when attempting to submit AbjsTestForm.php

Steps to reproduce

Set up new condition at /admin/config/user-interface/abjs/conditions and attempt to save it.

Set up the test at /admin/config/user-interface/abjs and attempt to save it.

Proposed resolution

Comparing the file in question with AbjsExperienceForm.php, which works fine, noticed the following differences.

1. use declaration on line 5 can be removed: 'use Drupal\Core\Database\Database;'
2. Database queries that include '$this->database->getConnection()->' should be simplified to '$this->database->' on AbjsConditionForm.php and AbjsTestForm.php

You're welcome :)

Comments

bobiroka created an issue. See original summary.

bobiroka’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new1.89 KB
bobiroka’s picture

Title: Unecessary getConnection method in AbjsConditionForm.php » Unecessary getConnection method
Issue summary: View changes
StatusFileSize
new8.03 KB
vipin.mittal18’s picture

Assigned: Unassigned » vipin.mittal18
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new6.5 KB
new31.46 KB

Hello Matt Mowers,

I have applied the patch and verified the functioning. It looks good to me and good to apply the patch. Refer attached screengrab

Thanks
Vipin

vipin.mittal18’s picture

avpaderno’s picture

Version: 8.x-1.3 » 8.x-1.x-dev

The patch in #3 applies to the development branch too.

vipin.mittal18’s picture

Version: 8.x-1.x-dev » 8.x-1.3
StatusFileSize
new8.03 KB

I believe the development branch https://www.drupal.org/project/abjs/releases/8.x-1.x-dev doesn't have latest code which 8.x.1.3 has.

I have renamed the patch as per the guidelines and attached in this comment.

avpaderno’s picture

On Drupal.org, a release is created from a Git branch. A release cannot have code that is more recent of the code contained in the corresponding Git branch.

The exact steps to create a new release are:

  • Push the code to the Git branch
  • Add a tag to that branch
  • Create a release from that tag
avpaderno’s picture

Version: 8.x-1.3 » 8.x-1.x-dev
Status: Reviewed & tested by the community » Needs work
diff --git a/src/Form/AbjsConditionForm.php b/src/Form/new.AbjsConditionForm.php
similarity index 94%
rename from src/Form/AbjsConditionForm.php
rename to src/Form/new.AbjsConditionForm.php
index f8685b8..3df9422 100644
--- a/src/Form/AbjsConditionForm.php
+++ b/src/Form/new.AbjsConditionForm.php
@@ -2,7 +2,6 @@

Why is the file renamed? Fixing code in a file doesn't require to rename it.
Drupal would not even use the new.AbjsConditionForm.php file to load the AbjsConditionForm class. A class whose fully qualified name is \Drupal\abjs\Form\AbjsConditionForm is loaded from the src/Form/AbjsConditionForm.php file.

avpaderno’s picture

The patch should also correct code similar to the following one, present in the module classes.

    $db = Database::getConnection();
    $date_Formatter = $this->dateFormatter;

    $output = [];
    $header = [
      $this->t('ID'),
      $this->t('Name'),
      $this->t('Status'),
      $this->t('Conditions'),
      $this->t('Experiences'),
      $this->t('Created'),
      $this->t('Created By'),
      $this->t('Changed'),
      $this->t('Changed By'),
    ];
    $rows = [];
    $active_array = [$this->t('Inactive'), $this->t('Active')];
    $tests = $db->query("SELECT * FROM {abjs_test} ORDER BY active DESC, changed DESC, created DESC")->fetchAll();

Instead, the code should add the dependency on create().

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('database'),
      $container->get('date.formatter'),
      $container->get('renderer')
    );
  }

The class constructor, in this case for the Drupal\abjs\Controller\AbjsDefaultController class, should be accordingly changed.

nsalves’s picture

StatusFileSize
new29.34 KB

Hey guys,
I found that the type of problem described here occurred in more files than the ones fixed in #7 so I attached a new patch that hopefully fixes all the problems with the deprecated method.

nsalves’s picture

Status: Needs work » Needs review
nsalves’s picture

StatusFileSize
new29.59 KB

Forgot to remove an unused class import, new patch attached. All should be fine now.

avpaderno’s picture

At first sight, it seems fine.

inaw’s picture

StatusFileSize
new14.43 KB

I wasn't able to apply the lasted patch #13 https://www.drupal.org/files/issues/2021-03-18/fix_deprecated_method-319... because it has special characters in it.

curl --output - https://www.drupal.org/files/issues/2021-03-18/fix_deprecated_method-3198542-12.patch
��diff --git a/src/Controller/AbjsDefaultController.php b/src/Controller/AbjsDefaultController.php
index 58a674b..49905a1 100644
--- a/src/Controller/AbjsDefaultController.php

I tried to fix it and re-uploaded the patch.

matthieuscarset’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #15 works correctly.

Tested on Drupal 8.9.13 with ABJS1.3.0.

  • thalles committed 586fa89 on 8.x-1.x authored by InaW
    Issue #3198542 by bobiroka, Nelson Alves, vipin.mittal18, InaW,...
thalles’s picture

Thanks everyone!

thalles’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.