diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index b873f6c..9911d02 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Template; use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Utility\String; /** * A class that can be used for collecting then rendering HTML attributtes. @@ -76,7 +77,7 @@ public function offsetSet($name, $value) { * @param mixed $value * The attribute value. * - * @throws \Exception + * @throws \UnexpectedValueException * If invalid object was passed as a value. * * @return \Drupal\Core\Template\AttributeValueBase @@ -107,7 +108,11 @@ protected function createAttributeValue($name, $value) { } // Provide a better exception message when invalid object is passed as value. elseif (is_object($value)) { - throw new \Exception('"' . $name . '" attribute value of type "' . get_class($value) . '" can not be converted to string'); + throw new \UnexpectedValueException(String::format('"@name" attribute value of type "@type" can not be converted to string', + array( + '@name' => $name, + '@type' => get_class($value), + ))); } return $value; diff --git a/core/lib/Drupal/Core/Template/TwigNodeExpressionFunction.php b/core/lib/Drupal/Core/Template/TwigNodeExpressionFunction.php index 3902abd..0e3f9b4 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeExpressionFunction.php +++ b/core/lib/Drupal/Core/Template/TwigNodeExpressionFunction.php @@ -13,11 +13,16 @@ * A class that extends Twig_Node_Expression_Function. * * Instances of this class are used in Drupal\Core\Template\TwigNodeVisitor - * to call the render_var function, with extra arguments. + * to call the render_var function, with extra arguments - generic name of var. * * @see core\vendor\twig\twig\lib\Twig\Node\Expression\Function.php */ class TwigNodeExpressionFunction extends \Twig_Node_Expression_Function { + + /** + * @var array + * Contains additional data that could be useful inside twig.engine. + */ protected $extra_arguments; /** @@ -41,6 +46,7 @@ public function compile(\Twig_Compiler $compiler) { $this->setAttribute('needs_environment', $function->needsEnvironment()); $this->setAttribute('needs_context', $function->needsContext()); $this->setAttribute('arguments', array_merge($function->getArguments(), $this->extra_arguments)); + if ($function instanceof \Twig_FunctionCallableInterface || $function instanceof \Twig_SimpleFunction) { $this->setAttribute('callable', $function->getCallable()); } @@ -48,4 +54,3 @@ public function compile(\Twig_Compiler $compiler) { $this->compileCallable($compiler); } } - diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index 5eab437..55dd4de 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -229,7 +229,7 @@ public function testStorage() { } /** - * PHPUnit's data provider callback. + * Provides test data sets for testCreateAttributeValue(). * * @see $this->testCreateAttributeValue() * @return array @@ -297,30 +297,15 @@ public function testCreateAttributeValue($name, $value, $result) { } /** - * PHPUnit's data provider callback to test exceptions. - * - * @see $this->testCreateAttributeValueException() - * @return array - */ - public function createAttributeValueExceptionProvider() { - $testCases = array(); - - // Test passed object without 'class' as name. - $name = $this->getRandomGenerator()->string(); - $value = $this->getRandomGenerator()->object(); - $testCases[] = array($name, $value); - - return $testCases; - } - - /** * @covers ::createAttributeValue() - * @dataProvider createAttributeValueExceptionProvider * @expectedException \Exception */ - public function testCreateAttributeValueException($name, $value) { + public function testCreateAttributeValueException() { $attribute = new Attribute(); + $name = $this->getRandomGenerator()->string(); + $value = $this->getRandomGenerator()->object(); $createAttributeValueMethod = $this->getCreateAttributeValueMethod(); + $createAttributeValueMethod->invokeArgs($attribute, array($name, $value)); } diff --git a/core/themes/engines/twig/twig.engine b/core/themes/engines/twig/twig.engine index c20da07..bf014bf 100644 --- a/core/themes/engines/twig/twig.engine +++ b/core/themes/engines/twig/twig.engine @@ -110,14 +110,17 @@ function twig_render_template($template_file, $variables) { * If an array is passed it is rendered via render() and scalar values are * returned directly. * + * @param string $arg_name + * Name of object that will be rendered. * @param mixed $arg * String, Object or Render Array * - * @return - * The rendered output or an Twig_Markup object. + * @return string The rendered output or an Twig_Markup object. * - * @see render + * @see render() * @see TwigNodeVisitor + * + * @throws \UnexpectedValueException */ function twig_render_var($arg_name, $arg) { // Check for numeric zero. @@ -144,7 +147,11 @@ function twig_render_var($arg_name, $arg) { return (string) $arg; } else { - throw new Exception(t('@arg_name of type "@class" doesn\'t implement __toString.', array('@arg_name' => $arg_name, '@class' => get_class($arg)))); + throw new \UnexpectedValueException(String::format('@arg_name of type "@class" doesn\'t implement __toString.', + array( + '@arg_name' => $arg_name, + '@class' => get_class($arg), + ))); } }