【问题标题】:Where is my twig template getting its variables?我的树枝模板在哪里获取它的变量?
【发布时间】:2019-01-16 18:23:10
【问题描述】:

我正在尝试对这个 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


}

这么看,似乎有一些注册登录的功能,而不是变量

【问题讨论】:

  • 搜索模板名称$template = $twig-&gt;load('index.html');(本例为index.html)然后找到render方法在哪里调用echo $template-&gt;render(['the' =&gt; 'variables', 'go' =&gt; 'here']);twig.symfony.com/doc/2.x/api.html
  • 搜索更少,course
  • 模板中的@see template_preprocess_node() 有帮助吗?
  • 您查看过 CMS 吗?
  • 以 field_* 开头的机器名称通常是在您在 CMS 上为内容类型创建新字段时生成的。登录到 cms (/user) 并搜索结构 -> 内容类型 ->(您的内容类型),然后管理字段。

标签: php symfony drupal twig


【解决方案1】:

答案在 CMS 中。

以 field_* 开头的机器名称通常是在您在 CMS 上为内容类型创建新字段时生成的。

登录到 cms (/user) 并搜索结构 -> 内容类型 ->(您的内容类型),然后管理字段。

你应该找到它。

【讨论】:

  • 如果我要设置一个布尔值,它将查询数据库以查看学生在当前日期完成了多少学分。我该如何设置?它也会在 CMS 方面吗?
  • 如果您在 CMS 中创建此字段,您可以将其调用到您的模板文件中。通常是内容。“field_your_machine_name”
猜你喜欢
  • 2016-02-14
  • 2023-01-18
  • 2013-05-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
相关资源
最近更新 更多