0

I am inserting a variable called "creditCheck" into a twig template called node--course.html.twig

I start in a class caled FormaRegis which extedns a abstract class called FormaEntity

This is the function:

namespace Drupal\docebo_login;

use Drupal\docebo_login\FormaEntity;


class FormaRegis extends FormaEntity {
.
.
.
public function completionCheck() {
        $check = false;

         if (parent::accessCheck()) {
            $sql = "SELECT count(*) as count FROM learning_courseuser
                    WHERE DATE_FORMAT(CURDATE(),'%d/%m/%Y') = DATE_FORMAT(date_complete,'%d/%m/%Y')
                    AND idUser =  " . $_SESSION['public_area_idst'];

            return $sql;
          }
          else {
            return "";
          }
}

In abstract class FormaEntity I have this function which is supposed to take the sql string from the previous function, completionCheck() and executes the mysql query and returns and object. Here is the function:

namespace Drupal\docebo_login;

use Drupal\Core\Access\AccessResult;

abstract class FormaEntity {
.
.
.
public function getCCresult() {
       if ($this->completionCheck() == "") {
          \Drupal\Core\Database\Database::setActiveConnection();
          return false;
       }

       $result = $this->connection->query($this->completionCheck())->fetch();
         \Drupal\Core\Database\Database::setActiveConnection();

       if ($result > 5 ) {
         $check = "fail";
       }
       else {
         $check = "pass";
       }

       return $check;
    }

In another class called FormaNotification which also extends FormaEntity write function that will be able to calls the function getCCresult() from the abstract class FormaEntity This is the function:

namespace Drupal\docebo_login;

use Drupal\docebo_login\FormaEntity;

class FormaNotification extends FormaEntity {
.
.
.
public function getCreditResult() {

         return parent::getCCresult();
    }

Lastly in my .theme file I wrote a function called txhs_preprocess_node__course(&$variables) which calls the previous function and is supposed to fetch the information I retrived from the database. But it does not. Here is my theme function:

use Drupal\docebo_login\FormaNotification;
use Drupal\docebo_login\FormaMyCourse;
use Drupal\docebo_login\FormaRegis;
use Drupal\Core\Url;
.
.
.
function txhs_preprocess_node__course(&$variables) {

  $noti = new FormaNotification();
  var_dump($noti->getCreditResult());
  exit;

  if ($noti->completionCheck() == "fail") {
      $check = "fail";
  }
  else {
    $check = "pass";
  }
   // $variables['creditCheck'] = "HI";

}

I have tried a whole slew of things such as renaming the functions, moving the functions around the theme file...but I really don't know what I am doing wrong.

I am running php 7.2.11, with mysql 5.0.12 with drupal 8.5.6

I expect an out to be an object with a number, but instead i get an error:

The website encountered an unexpected error. Please try again later. Error: Call to undefined method Drupal\docebo_login\FormaNotification::completionCheck() in Drupal\docebo_login\FormaEntity->getCCresult() (line 106 of modules/custom/docebo_login/src/FormaEntity.php).

baikho
  • 5,203
  • 4
  • 40
  • 47
Mat
  • 67
  • 1
  • 3
  • 17
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/187079/discussion-on-question-by-mat-steuernagle-call-to-undefined-method-problem-in-dr). – Samuel Liew Jan 21 '19 at 23:25

1 Answers1

0

Ok, so you have a base class & 2 subclasses:

  • abstract class FormaEntity > FormaRegis
  • abstract class FormaEntity > FormaNotification

When calling getCreditResult() on new FormaNotification() it fails because the first line in FormaEntity::getCCresult() is calling a method that doesn't exist in that abstract class, being completionCheck(). A little further down, $noti->completionCheck(), would also fail for the same reason.

AFAICT from your code, the completionCheck() is only specified in FormaRegis, which is causing your issue. The parent class and neither FormaNotification have access to that.

baikho
  • 5,203
  • 4
  • 40
  • 47