簡體   English   中英

我的樹枝模板在哪里獲取它的變量?

[英]Where is my twig template getting its variables?

我正在嘗試對這個 Twig 模板進行逆向工程,以回到最初設置變量的位置,以便我可以添加更多變量。 這是一個 Drupal8 項目。 樹枝模板“node--course.html.twig”的開頭如下所示。 它在哪里我看到正在設置的變量。

{#
/**
 * @file
 * Default theme implementation to display a node.
 *
 * Available variables:
 * - node: Full node entity.
 *   - id: The node ID.
 *   - bundle: The type of the node, for example, "page" or "article".
 *   - authorid: The user ID of the node author.
 *   - createdtime: Time the node was published formatted in Unix timestamp.
 *   - changedtime: Time the node was changed formatted in Unix timestamp.
 * - label: The title of the node.
 * - content: All node items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - author_picture: The node author user entity, rendered using the "compact"
 *   view mode.
 * - metadata: Metadata for this node.
 * - date: Themed creation date field.
 * - author_name: Themed author name field.
 * - url: Direct URL of the current node.
 * - display_submitted: Whether submission information should be displayed.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - node: The current template type (also known as a "theming hook").
 *   - node--type-[type]: The current node type. For example, if the node is an
 *     "Article" it would result in "node--type-article". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - node--view-mode-[view_mode]: The View Mode of the node; for example, a
 *     teaser would result in: "node--view-mode-teaser", and
 *     full: "node--view-mode-full".
 *   The following are controlled through the node publishing options.
 *   - node--promoted: Appears on nodes promoted to the front page.
 *   - node--sticky: Appears on nodes ordered above other non-sticky nodes in
 *     teaser listings.
 *   - node--unpublished: Appears on unpublished nodes visible only to site
 *     admins.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - content_attributes: Same as attributes, except applied to the main
 *   content tag that appears in the template.
 * - author_attributes: Same as attributes, except applied to the author of
 *   the node tag that appears in the template.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 * - view_mode: View mode; for example, "teaser" or "full".
 * - teaser: Flag for the teaser state. Will be true if view_mode is 'teaser'.
 * - page: Flag for the full page state. Will be true if view_mode is 'full'.
 * - readmore: Flag for more state. Will be true if the teaser content of the
 *   node cannot hold the main body content.
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_node()
 *
 * @todo Remove the id attribute (or make it a class), because if that gets
 *   rendered twice on a page this is invalid CSS for example: two lists
 *   in different view modes.
 *
 * @ingroup themeable
 */
#}

{# {{ kint() }} #}

<article id="node-{{ node.id }}" {{ attributes }}>
{{node}}
  {{ title_prefix }}
  {% if not page %}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
  {% endif %}
  {{ title_suffix }}

  {% if node.field_packaging.value == '1' %}

    {% set image = content.field_image %}
    {% set ce = content.field_tax_credit_hours %}
    {% set goal = content.field_goal %}
    {% set target_audience = content.field_audience %}
    {% set objectives = content.field_objectives %}
    {% set accreditation = content.field_accreditation %}
    {% set disclosure = content.field_disclosure_statement %}
    {# {% set references_old = content.field_references %} #}
    {% set references = content.field_references_par %}
    {% set appendix = content.field_appendix %}
    {% set faculty = content.field_faculty %}
    {% set related_courses = content.field_related_courses %}
    {# set suggested_courses = content.field_suggested_courses #}
    {% set additional = content.field_callout %}
    {% set expiration = node.field_expiration.value %}

我試過用看起來很獨特的詞,比如

field_tax_credit_hours

並搜索項目以查看它的設置位置,但它僅出現在其他樹枝文件中。 我還查看了整個頁面,看看我是否可以搜索輸出的來源。 例如,他們一開始我的頁面就以:

<!-- returning result -->

當我搜索它時,它指向一個名為 getResult() 的 php 函數

public function getResult() {
       if ($this->rowBase() == "") {
          print "<!-- rowBase empty -->";
          \Drupal\Core\Database\Database::setActiveConnection();
          return false;
       }
       print "<!-- returning result -->";
             $result = $this->connection->query($this->rowBase())->fetchAll();
         \Drupal\Core\Database\Database::setActiveConnection();

       return $result;
    }

搜索 rowBase() 我找到了這個函數:

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

    }

所以它似乎沒有設置我需要的變量,而是在登錄時返回用戶信息。所以我現在卡住了,不知道從哪里開始。 我已經做了一年左右的 PHP 開發人員,只參加了一個團隊樹屋 drupal 課程,所以它非常基礎。

我注意到頁面也返回了這個

<!-- BEGIN OUTPUT from 'themes/custom/site/templates/node--course.html.twig' -->

但是搜索字符串“BEGIN OUTPUT”什么也沒有返回。 所以我不知道從這里去哪里。

似乎變量來自“內容”對象,但在項目中搜索內容的結果太多了。 任何去哪里尋找的想法都會很棒。

更新

我被要求查看 template_preprocess_node

得到了這個:

function site_preprocess_node(&$variables) {

  $node = \Drupal::routeMatch()->getParameter('node');

  if ($node && $node->getType() == 'course') {
    $noti = new FormaNotification();
    print "HERE";
    print_r($noti->getResult());
    exit;
    if ($noti->getResult()) {
      $variables['signIn'] = "yes";
        if ($noti->getFormaAdmin())
          $variables['is_forma_admin'] = "yes";
        else
        $variables['is_forma_admin'] = "no";
    } else {
      $variables['signIn'] = "no";
      $variables['is_forma_admin'] = "no";

    }

  $current_url = Url::fromRoute('<current>');
    $variables['signURL']  = 'http://' . $_SERVER['HTTP_HOST'] . $current_url->toString();


   if ($node->get('field_packaging')->getValue()[0]['value'] == '2') {
         $variables['regis'] = true;
   } else {
          $reg = new FormaRegis();
          $reg->setConnection('docebo');

          $reg->setID($node->get('field_docebo_course_id')->getValue()[0]['value']);
           $result = $reg->getResult();

            if(!empty($result)) {
                $variables['regis'] = true;
            } else {
               $variables['regis'] = false;
            }

   }



  } // course


}

所以看着它,它似乎有一些注冊登錄的功能而不是變量

答案是進入 CMS。

以 field_* 開頭的機器名稱通常是在您在 CMS 上將新字段創建為內容類型時生成的。

登錄 cms (/user) 並搜索結構-> 內容類型->(您的內容類型),然后管理字段。

你應該找到它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM