预处理功能未在主题drupal 8上运行

时间:2019-01-18 19:59:13

标签: php drupal twig

我正在尝试查询数据库并返回一个数字以存储在名为 node--course.html.twig

的树枝模板中

因此,我首先在类 FormaRegis.php

中编写函数 completionCheck()
<?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 "";
          }

    }

}

在一个名为 FormaEntity.php 的页面中,我编写了此函数

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

然后在 FormaNotification.php 中,我编写了函数 completionCheck()

<?php

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

}

然后,最后,在我的.theme文件中,添加了函数 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";
  }

}

但是当我访问使用该节点的任何页面--course.html.twig并使用 {{kint(creditCheck)}} ,它表示该变量为NULL。我不确定自己在做什么错。如果有人能指出我正确的方向,那就太好了。

0 个答案:

没有答案