1

I am trying to query my database and return a number to store in a twig template called node--course.html.twig.

So I started by writing the function completionCheck() in class FormaRegis.php.

namespace Drupal\docebo_login;

use Drupal\docebo_login\FormaEntity;


class FormaRegis extends FormaEntity {

     public $ID;

  public function setID($ID) {
    $this->ID = $ID;
  }

  public function getID() {
    return $this->ID;
  }

    public function __construct() {
        parent::__construct();
    }


 public function rowBase() {


     if (parent::accessCheck()) {

        $sql = "SELECT * FROM learning_courseuser
                  WHERE idUser = " . $_SESSION['public_area_idst'] .
                 " AND idCourse = " . $this->ID. " ";
        return $sql;
       } else {

            return "";
       }

    }

    public function completionCheck() {
        $check = false;

         if (parent::accessCheck()) {
            $sql = "SELECT 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 "";
          }

    }

}

and in a page called FormaEntity.php I wrote this function:

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;
}

Then in FormaNotification.php I wrote function completionCheck().

namespace Drupal\docebo_login;

use Drupal\docebo_login\FormaEntity;

class FormaNotification extends FormaEntity {

    public function __construct() {
        parent::__construct();
    }

    public function getResult() {

         return parent::accessCheck();
    }

    public function completionCheck() {

         return parent::getCCresult();
    }

}

Then finally, in my MYTHEME.theme file I added the function mytheme_preprocess_node_course(&$variables).

function mytheme_preprocess_node_course (&$variables) {

  $noti = new FormaNotification();

  if ($noti->completionCheck() == "fail") {
    $variables['creditCheck'] = "yes";
  }
  else {
    $variables['creditCheck'] = "no";
  }

}

But when I visit the any page that uses the node--course.html.twig and use {{ kint(creditCheck) }}, it says that the variable is NULL. I am not sure what I am doing wrong. If anyone can point me in the right direction that would be great.

  • You’re not returning any renderable elements as far as I can see? – Kevin Jan 18 at 23:30
  • @kevin not $variables[“credit check”]? – Mat Steuernagle Jan 18 at 23:42
  • Please forgive my ignorance I am brand new to drupal – Mat Steuernagle Jan 18 at 23:42
  • Not that it would solve your issue, but applying Drupal coding standards would at least make your code to be easier readable. – leymannx Jan 19 at 8:34
  • The preprocess function must have two underscores: mytheme_preprocess_node__course. – leymannx Jan 19 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.