class TwigReference

A class used to pass variables by reference while they are used in twig.

This is done by saving a reference to the original render array within a TwigReference via the setReference() method like this:

$obj = new TwigReference();
$obj
  ->setReference($variable);

When a TwigReference is accessed via the offsetGet method the resulting reference is again wrapped within a TwigReference. Therefore references to render arrays within render arrays are also retained.

To unwrap TwigReference objects the reference can be retrieved out of the object by calling the getReference() method like this:

$variable =& $obj
  ->getReference();

This allows render(), hide() and show() to access the original variable and change it. The process of unwrapping and passing by reference to this functions is done transparently by the TwigReferenceFunctions helper class.

Hierarchy

  • class \Drupal\Core\Template\TwigReference extends \Drupal\Core\Template\ArrayObject

Expanded class hierarchy of TwigReference

See also

TwigReferenceFunction

TwigReferenceFunctions

2 files declare their use of TwigReference

File

drupal/core/lib/Drupal/Core/Template/TwigReference.php, line 36
Definition of Drupal\Core\Template\TwigReference.

Namespace

Drupal\Core\Template
View source

Members

Namesort descending Modifiers Type Description Overrides
TwigReference::$writableRef protected property Holds an internal reference to the original array.
TwigReference::getReference public function Gets a reference to the internal storage.
TwigReference::offsetGet public function Retrieves offset from internal reference.
TwigReference::offsetSet public function Sets offset in internal reference and internal storage to value.
TwigReference::setReference public function Sets a reference in the internal storage.
TwigReference::__construct public function Constructs a \Drupal\Core\Template\TwigReference object.