6

I am getting this error when using the "field collection" module :

 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).

the code that causes the problem is this one :

<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>

what is the best way to fix this ? is it by adding (uri) to the allowed methods in the sandbox policy ? if yes then how I can do that ?

I read in the twig documentation that I can do something like this :

    $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);

but I didn't understand how or where to put this code.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Asma Supih
  • 61
  • 6
  • Possible duplicate of [How to properly enable the twig's sandbox extension in Symfony2?](http://stackoverflow.com/questions/16070602/how-to-properly-enable-the-twigs-sandbox-extension-in-symfony2) – DarkBee Oct 17 '16 at 17:34

1 Answers1

11

Drupal's twig sandbox policy (defined in core/lib/Drupal/Core/Template/TwigSandboxPolicy.php) reads from the global $settings array so you can define your own in your settings.php i.e.

// Override default twig method whitelist.
$settings['twig_sandbox_whitelisted_methods'] = [
  // Defaults:
  'id',
  'label',
  'bundle',
  'get',
  '__toString',
  'toString',
  // Additions:
  'url',
];
Sut3kh
  • 111
  • 1
  • 4