【问题标题】:drupal 8 enable twig debug not working?drupal 8启用树枝调试不起作用?
【发布时间】:2017-03-14 18:44:45
【问题描述】:

我正在努力解决这个问题:我不知道为什么 twig 调试不起作用:
Drupal 版本 8.2.6

文件夹结构

文件夹权限

Settings.php

$settings['hash_salt'] = 'DEVELOPMENT_SALT';
$settings['update_free_access'] = FALSE;
$settings['file_public_base_url'] = 'http://localhost/files';
$settings['file_public_path'] = 'sites/default/files';
$settings['file_private_path'] = 'sites/default/private';
$settings['file_scan_ignore_directories'] = [
  'node_modules',
  'bower_components',
];
if (file_exists(__DIR__ . '/../development/settings.development.php'))     {
  include __DIR__ . '/../development/settings.development.php';
}

/../development/settings.development.php

assert_options(ASSERT_ACTIVE, TRUE);
\Drupal\Component\Assertion\Handle::register();

/**
 * Enable local development services.
 */
$settings['container_yamls'][] = __DIR__ . '/development.services.yml';

$databases['default']['default'] = array(
  'database' => 'dbname',
  'username' => 'dbusername',
  'password' => 'pw',
  'prefix' => '',
  'host' => '127.0.0.1',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

$settings['hash_salt'] = 'DEVELOPMENT';

$config['system.logging']['error_level'] = 'verbose';
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] =     'cache.backend.null';
$settings['extension_discovery_scan_tests'] = TRUE;
$settings['rebuild_access'] = TRUE;
$settings['skip_permissions_hardening'] = TRUE;

development.services.yml

parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

我已经检查/完成的事情:

  • 检查是否所有这些文件都已加载 -> 它们是。
  • 检查了 development.services.yml 的缩进 -> 签出。
  • 清除所有不同的缓存,重建它们,...

如果我更改模板,它会更改,但不会调试。

奇怪的是: 站点/默认/文件和站点/默认/私有不被 Drupal 使用。

【问题讨论】:

  • 我建议你可以在drupal.stackexchange.com提出你的问题
  • 以防万一您对调试有什么期望?在大多数情况下,vijay 的答案足以向您显示模板信息(如果您查看源代码)。如果你想显示可用的变量,你当然需要将 {{ dump() }} 添加到你的树枝模板中(当你有很多数据时要小心,这可能会给你一个内存错误,在这种情况下 coderate 的答案应该会有所帮助)

标签: drupal twig drupal-8


【解决方案1】:

我建议不要将 development.services.yml 文件重命名为 services.yml,因为这会导致您的所有开发代码/配置/设置都在生产环境中应用和运行。而是按设计使用开发文件。

这是我们的标准设置:

/sites/default/settings.php(在版本控制中)在最底部包含以下内容,因此我们只在应该拥有它的实例上加载开发内容。例如不生产:

/**
 * Load local development override configuration, if available.
 *
 * Keep this code block at the end of this file to take full effect.
 */
if (file_exists(__DIR__ . '/settings.local.php')) {
  include __DIR__ . '/settings.local.php';
}

我们的/sites/default/settings.local.php(不在版本控制中)至少包含以下内容:

<?php

/**
 * Disable css and js preprocessing.
 */
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

/**
 * Disable Render Cache and Dynamic Page Cache.
 */
$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

/**
 * Enable local development services.
 */
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';

/**
 * Enable access to rebuild.php.
 */
$settings['rebuild_access'] = TRUE;

最后,我们的/sites/development.services.yml(在版本控制中)文件包含以下内容:

# Local development services.
#
# To activate this feature, follow the instructions at the top of the
# 'example.settings.local.php' file, which sits next to this file.
parameters:
  http.response.debug_cacheability_headers: true
  twig.config:
    debug: true
    auto_reload: true
    cache: false
services:
  cache.backend.null:
    class: Drupal\Core\Cache\NullBackendFactory

这对我们来说非常有效。

【讨论】:

    【解决方案2】:

    Twig Debuggin 没有按预期工作,我做了你所做的所有事情,但它没有工作。所以我在所有站点中搜索了 debug:false 并找到了这个文件:core/core.services.xml

    我更改了那里的值并且它起作用了!

    这不是一个好方法,因为我们正在修改核心,但它会帮助解决这个问题的人!

    【讨论】:

      【解决方案3】:

      要启用 Twig 调试,您需要复制(“default.services.yml”)并将其重命名为“services.yml”。并将“debug: false”设置为true。

      例如:services.yml

      parameters:
        session.storage.options:
          # Default ini options for sessions.
          #
          # Some distributions of Linux (most notably Debian) ship their PHP
          # installations with garbage collection (gc) disabled. Since Drupal depends
          # on PHP's garbage collection for clearing sessions, ensure that garbage
          # collection occurs by using the most common settings.
          # @default 1
          gc_probability: 1
          # @default 100
          gc_divisor: 100
          #
          # Set session lifetime (in seconds), i.e. the time from the user's last
          # visit to the active session may be deleted by the session garbage
          # collector. When a session is deleted, authenticated users are logged out,
          # and the contents of the user's $_SESSION variable is discarded.
          # @default 200000
          gc_maxlifetime: 200000
          #
          # Set session cookie lifetime (in seconds), i.e. the time from the session
          # is created to the cookie expires, i.e. when the browser is expected to
          # discard the cookie. The value 0 means "until the browser is closed".
          # @default 2000000
          cookie_lifetime: 2000000
          #
          # Drupal automatically generates a unique session cookie name based on the
          # full domain name used to access the site. This mechanism is sufficient
          # for most use-cases, including multi-site deployments. However, if it is
          # desired that a session can be reused across different subdomains, the
          # cookie domain needs to be set to the shared base domain. Doing so assures
          # that users remain logged in as they cross between various subdomains.
          # To maximize compatibility and normalize the behavior across user agents,
          # the cookie domain should start with a dot.
          #
          # @default none
          # cookie_domain: '.example.com'
          #
        twig.config:
          # Twig debugging:
          #
          # When debugging is enabled:
          # - The markup of each Twig template is surrounded by HTML comments that
          #   contain theming information, such as template file name suggestions.
          # - Note that this debugging markup will cause automated tests that directly
          #   check rendered HTML to fail. When running automated tests, 'debug'
          #   should be set to FALSE.
          # - The dump() function can be used in Twig templates to output information
          #   about template variables.
          # - Twig templates are automatically recompiled whenever the source code
          #   changes (see auto_reload below).
          #
          # For more information about debugging Twig templates, see
          # https://www.drupal.org/node/1906392.
          #
          # Not recommended in production environments
          # @default false
          debug: true
          # Twig auto-reload:
          #
          # Automatically recompile Twig templates whenever the source code changes.
          # If you don't provide a value for auto_reload, it will be determined
          # based on the value of debug.
          #
          # Not recommended in production environments
          # @default null
          auto_reload: null
          # Twig cache:
          #
          # By default, Twig templates will be compiled and stored in the filesystem
          # to increase performance. Disabling the Twig cache will recompile the
          # templates from source each time they are used. In most cases the
          # auto_reload setting above should be enabled rather than disabling the
          # Twig cache.
          #
          # Not recommended in production environments
          # @default true
          cache: true
        renderer.config:
          # Renderer required cache contexts:
          #
          # The Renderer will automatically associate these cache contexts with every
          # render array, hence varying every render array by these cache contexts.
          #
          # @default ['languages:language_interface', 'theme', 'user.permissions']
          required_cache_contexts: ['languages:language_interface', 'theme', 'user.permissions']
          # Renderer automatic placeholdering conditions:
          #
          # Drupal allows portions of the page to be automatically deferred when
          # rendering to improve cache performance. That is especially helpful for
          # cache contexts that vary widely, such as the active user. On some sites
          # those may be different, however, such as sites with only a handful of
          # users. If you know what the high-cardinality cache contexts are for your
          # site, specify those here. If you're not sure, the defaults are fairly safe
          # in general.
          #
          # For more information about rendering optimizations see
          # https://www.drupal.org/developing/api/8/render/arrays/cacheability#optimizing
          auto_placeholder_conditions:
            # Max-age at or below which caching is not considered worthwhile.
            #
            # Disable by setting to -1.
            #
            # @default 0
            max-age: 0
            # Cache contexts with a high cardinality.
            #
            # Disable by setting to [].
            #
            # @default ['session', 'user']
            contexts: ['session', 'user']
            # Tags with a high invalidation frequency.
            #
            # Disable by setting to [].
            #
            # @default []
            tags: []
        # Cacheability debugging:
        #
        # Responses with cacheability metadata (CacheableResponseInterface instances)
        # get X-Drupal-Cache-Tags and X-Drupal-Cache-Contexts headers.
        #
        # For more information about debugging cacheable responses, see
        # https://www.drupal.org/developing/api/8/response/cacheable-response-interface
        #
        # Not recommended in production environments
        # @default false
        http.response.debug_cacheability_headers: false
        factory.keyvalue:
          {}
          # Default key/value storage service to use.
          # @default keyvalue.database
          # default: keyvalue.database
          # Collection-specific overrides.
          # state: keyvalue.database
        factory.keyvalue.expirable:
          {}
          # Default key/value expirable storage service to use.
          # @default keyvalue.database.expirable
          # default: keyvalue.database.expirable
        # Allowed protocols for URL generation.
        filter_protocols:
          - http
          - https
          - ftp
          - news
          - nntp
          - tel
          - telnet
          - mailto
          - irc
          - ssh
          - sftp
          - webcal
          - rtsp
      
         # Configure Cross-Site HTTP requests (CORS).
         # Read https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
         # for more information about the topic in general.
         # Note: By default the configuration is disabled.
        cors.config:
          enabled: false
          # Specify allowed headers, like 'x-allowed-header'.
          allowedHeaders: []
          # Specify allowed request methods, specify ['*'] to allow all possible ones.
          allowedMethods: []
          # Configure requests allowed from specific origins.
          allowedOrigins: ['*']
          # Sets the Access-Control-Expose-Headers header.
          exposedHeaders: false
          # Sets the Access-Control-Max-Age header.
          maxAge: false
          # Sets the Access-Control-Allow-Credentials header.
          supportsCredentials: false
      

      【讨论】:

        【解决方案4】:

        只需将default.services.yml 复制到services.yml。因此,core.services.xml 将被覆盖。

        【讨论】:

          【解决方案5】:

          您需要在您的development.services.yml 文件上将cache 选项设置为true。 像这样的:

          services:
            cache.backend.null:
              class: Drupal\Core\Cache\NullBackendFactory
          parameters:
            twig.config:
              debug: true
              auto_reload: true
              cache: true
          

          在您清除缓存并打开一些您想要调试的页面后,您会看到在files/php/twig 中会创建一些.php 文件,如下图所示:

          这些页面将在您调试.php 文件时正常调试。

          Acquia 博客上有一篇文章帮助您使用 Xdebug 和 PhpStorm IDE 进行此配置和调试,您可以在链接 Debugging TWIG templates in Drupal 8 with PhpStorm and XDebug 上进行此配置和调试

          【讨论】:

            【解决方案6】:

            对于大多数人来说,包括settings.php 文件底部的这一行将起作用:

            $settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
            

            更多详情

            如果您已经为 Twig 设置了调试但没有任何效果,那么您正在修改错误的文件。

            首先,确保 Drupal 正在解析您正在修改的服务文件。一种简单的方法是向您正在使用的服务文件添加错误的 YML 格式,然后尝试清除缓存,Drupal 应该会抱怨“无法解析”。

            如果尽管添加了错误的 YML 格式,Drupal 也没有抱怨,这意味着文件没有被解析;因此,如果你改变它并不重要。

            将我提到的行附加到您的settings.php 确保您正在添加开发服务配置,大部分时间具有以下内容:

            # Local development services.
            #
            # To activate this feature, follow the instructions at the top of the
            # 'example.settings.local.php' file, which sits next to this file.
            parameters:
              http.response.debug_cacheability_headers: true
              twig.config:
                # @default false
                debug: true
                # @default null
                auto_reload: true
                # @default true
                cache: false
            services:
              cache.backend.null:
                class: Drupal\Core\Cache\NullBackendFactory
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-07-19
              • 1970-01-01
              • 2016-06-02
              • 2019-11-24
              • 1970-01-01
              • 1970-01-01
              • 2018-08-25
              相关资源
              最近更新 更多