plus-8.x-4.x-dev/src/Utility/Variables.php
src/Utility/Variables.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | <?php namespace Drupal\plus\Utility; use Drupal\plus\Plugin\Theme\ThemeInterface; use Drupal\plus\Plus; /** * Class to help modify template variables. * * @ingroup utility */ class Variables extends DrupalArray { /** * An element object. * * @var \Drupal\plus\Utility\Element|FALSE */ public $element = FALSE; /** * The active theme. * * @var \Drupal\plus\Plugin\Theme\ThemeInterface */ protected $theme ; /** * Element constructor. * * @param array $variables * A theme hook variables array. * @param \Drupal\plus\Plugin\Theme\ThemeInterface $theme * The current theme context for these Variables. If not passed, the active * theme will be used. */ public function __construct( array & $variables , ThemeInterface $theme = NULL) { if (!isset( $theme )) { $theme = Plus::getActiveTheme(); } $this ->theme = $theme ; $this -> array = & $variables ; if (isset( $variables [ 'element' ]) && Element::isRenderArray( $variables [ 'element' ])) { $this ->element = Element::reference( $variables [ 'element' ], NULL, $theme ); } elseif (isset( $variables [ 'elements' ]) && Element::isRenderArray( $variables [ 'elements' ])) { $this ->element = Element::reference( $variables [ 'elements' ], NULL, $theme ); } } /** * Creates a new \Drupal\plus\Utility\Variables instance. * * @param array $variables * A theme hook variables array. * @param \Drupal\plus\Plugin\Theme\ThemeInterface $theme * The theme that these variables belong to. * * @return \Drupal\plus\Utility\Variables * The newly created Variables instance. */ public static function create( array & $variables , ThemeInterface $theme = NULL) { return $variables instanceof self ? $variables : new self( $variables , $theme ); } /** * Retrieves a context value from the variables array or its element, if any. * * @param string $name * The name of the context key to retrieve. * @param mixed $default * Optional. The default value to use if the context $name isn't set. * * @return mixed|NULL * The context value or the $default value if not set. */ public function &getContext( $name , $default = NULL) { $context = & $this ->offsetGet( $this ->propertyPrefix . 'context' , []); if (!isset( $context [ $name ])) { // If there is no context on the variables array but there is an element // present, proxy the method to the element. if ( $this ->element) { return $this ->element->getContext( $name , $default ); } $context [ $name ] = $default ; } return $context [ $name ]; } /** * Maps an element's properties to the variables attributes array. * * @param array $map * An associative array whose keys are element property names and whose * values are the variable names to set in the variables array; e.g., * array('#property_name' => 'variable_name'). If both names are identical * except for the leading '#', then an attribute name value is sufficient * and no property name needs to be specified. * @param bool $overwrite * If the variable exists, it will be overwritten. This does not apply to * attribute arrays, they will always be merged recursively. * * @return $this */ public function map( array $map , $overwrite = TRUE) { // Immediately return if there is no element in the variable array. if (! $this ->element) { return $this ; } // Iterate over each map item. foreach ( $map as $property => $variable ) { // If the key is numeric, the attribute name needs to be taken over. if ( is_int ( $property )) { $property = $variable ; } // Merge attributes from the element. if ( strpos ( $property , 'attributes' ) !== FALSE) { $this ->setAttributes( $this ->element->getAttributes( $property )->getArrayCopy(), $variable ); } // Set normal variable. elseif ( $overwrite || ! $this ->offsetExists( $variable )) { $this ->offsetSet( $variable , $this ->element->getProperty( $property )); } } return $this ; } } |