From ca109d941b37fab3c4544212ed66f14f5749f997 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 19 Jul 2015 22:38:25 -0400 Subject: [PATCH] Fix notice error when reading array values. We cannot assume that array keys will exist. Refs #7061 --- src/View/Form/EntityContext.php | 3 ++- tests/TestCase/View/Form/EntityContextTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index c1a54d1e1b9..fdaba75fa32 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -223,7 +223,8 @@ public function val($field) if ($entity instanceof EntityInterface) { return $entity->get(array_pop($parts)); } elseif (is_array($entity)) { - return $entity[array_pop($parts)]; + $key = array_pop($parts); + return isset($entity[$key]) ? $entity[$key] : null; } return null; } diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index 917ce1a843e..253276f2f67 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -477,6 +477,12 @@ public function testValGetArrayValue() $result = $context->val('tag.name'); $this->assertEquals($row->tag['name'], $result); + + $result = $context->val('tag.nope'); + $this->assertNull($result); + + $result = $context->val('author.roles.3'); + $this->assertNull($result); } /**