简体   繁体   English

我的树枝模板在哪里获取它的变量?

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

I am trying to sort of reverse engineer to this twig template to get back to where the the variables are being set in the first place so I can add more variables.我正在尝试对这个 Twig 模板进行逆向工程,以回到最初设置变量的位置,以便我可以添加更多变量。 This is a Drupal8 project.这是一个 Drupal8 项目。 The beginning of the twig template "node--course.html.twig" is seen below.树枝模板“node--course.html.twig”的开头如下所示。 Its where I see the variables being set.它在哪里我看到正在设置的变量。

{#
/**
 * @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 %}

I have tried taking words that appear to be unique like我试过用看起来很独特的词,比如

field_tax_credit_hours field_tax_credit_hours

And search the project to see maybe where its being set but it only appears in other twig files.并搜索项目以查看它的设置位置,但它仅出现在其他树枝文件中。 I also looked at the whole page as a whole to see maybe I can search for where the output is coming from.我还查看了整个页面,看看我是否可以搜索输出的来源。 For example they very start of my page begins with:例如,他们一开始我的页面就以:

<!-- returning result -->

When I search that it points me to a php function called getResult()当我搜索它时,它指向一个名为 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;
    }

searching for rowBase() i found this function:搜索 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 ""; 
       }

    }

So it appears to not set the variables I need but instead returns the users info if the are logged in. So I am now stuck and don't really know where to go from here.所以它似乎没有设置我需要的变量,而是在登录时返回用户信息。所以我现在卡住了,不知道从哪里开始。 I have been a PHP developer for a year or so and only did a team treehouse drupal course so it was very basic.我已经做了一年左右的 PHP 开发人员,只参加了一个团队树屋 drupal 课程,所以它非常基础。

I noticed the page also returns this我注意到页面也返回了这个

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

But searching for the string "BEGIN OUTPUT" returns nothing.但是搜索字符串“BEGIN OUTPUT”什么也没有返回。 So I don't know where to go from here.所以我不知道从这里去哪里。

It seems that the variables are coming from the "content" object but searching content in the project has WAY too many results to go through.似乎变量来自“内容”对象,但在项目中搜索内容的结果太多了。 Any ideas where to look would be fantastic.任何去哪里寻找的想法都会很棒。

UPDATE更新

I was asked to look into template_preprocess_node我被要求查看 template_preprocess_node

and got this:得到了这个:

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


}

So looking at it, it appears to have some function of registration sign in and not the variables所以看着它,它似乎有一些注册登录的功能而不是变量

The answer is into CMS.答案是进入 CMS。

The machine name starting with field_* usually is made when you create a new field into a content type on CMS.以 field_* 开头的机器名称通常是在您在 CMS 上将新字段创建为内容类型时生成的。

Login into cms (/user) and search for structure-> content types -> (your content types) and then manage fields.登录 cms (/user) 并搜索结构-> 内容类型->(您的内容类型),然后管理字段。

You should find it.你应该找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM