允许在树枝中的沙箱策略中使用新方法(drupal 8)

Asm*_*pih 2 symfony twig drupal-8 twig-extension

使用“字段收集”模块时出现此错误:

 Twig_Sandbox_SecurityError: Calling "uri" method on a "Drupal\field_collection\Entity\FieldCollectionItem" object is not allowed in "themes/communitylife/templates/content/node.html.twig" at line 83. in Drupal\Core\Template\TwigSandboxPolicy->checkMethodAllowed() (line 99 of core/lib/Drupal/Core/Template/TwigSandboxPolicy.php).
Run Code Online (Sandbox Code Playgroud)

导致问题的代码是以下代码:

<div class=" title-col col-md-7">

     <a  href="{{file_url(node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.uri.value)}}" target="_blank"> <strong> {{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_description.value}}

      <span class="file-type"> ({{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.uri.value | slice(-3) }} </span>, <span class="file-size"> {{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.size }}) </span> 

      </strong></a>

</div>
Run Code Online (Sandbox Code Playgroud)

解决此问题的最佳方法是什么?通过在沙箱策略的允许方法中添加(uri)来实现?如果是,那我该怎么做?

我在树枝文档中阅读到可以执行以下操作:

    $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何或在何处放置此代码。

提前致谢

Sut*_*3kh 5

Drupal的树枝沙箱策略(在core / lib / Drupal / Core / Template / TwigSandboxPolicy.php中定义)从全局$ settings数组读取,因此您可以在settings.php中定义自己的

// Override default twig method whitelist.
$settings['twig_sandbox_whitelisted_methods'] = [
  // Defaults:
  'id',
  'label',
  'bundle',
  'get',
  '__toString',
  'toString',
  // Additions:
  'url',
];
Run Code Online (Sandbox Code Playgroud)