Browse Source

Make cells inherit more data from the view.

As Cells are often created in the view, it makes sense that a cell will
inherit the containing view's helpers and classname. By propagating both
helpers and view class names developers can expect to get the same
rendering engine in both their views and cells.

Refs #3926
Refs #4358
Refs cakephp/app#118
mark_story 11 years ago
parent
commit
88b342b2df
3 changed files with 45 additions and 3 deletions
  1. 9 0
      src/View/Cell.php
  2. 11 2
      src/View/CellTrait.php
  3. 25 1
      tests/TestCase/View/CellTest.php

+ 9 - 0
src/View/Cell.php

@@ -88,6 +88,15 @@ abstract class Cell {
 	public $theme;
 
 /**
+ * The helpers this cell uses.
+ *
+ * This property is copied automatically when using the CellTrait
+ *
+ * @var array
+ */
+	public $helpers = [];
+
+/**
  * These properties can be set directly on Cell and passed to the View as options.
  *
  * @var array

+ 11 - 2
src/View/CellTrait.php

@@ -16,6 +16,7 @@ namespace Cake\View;
 
 use Cake\Core\App;
 use Cake\Utility\Inflector;
+use Cake\View\View;
 
 /**
  * Provides cell() method for usage in Controller and View classes.
@@ -75,9 +76,17 @@ trait CellTrait {
 		$cellInstance->template = Inflector::underscore($action);
 		$cellInstance->plugin = !empty($plugin) ? $plugin : null;
 		$cellInstance->theme = !empty($this->theme) ? $this->theme : null;
-		$length = count($data);
+		if (!empty($this->helpers)) {
+			$cellInstance->helpers = $this->helpers;
+		}
+		if (isset($this->viewClass)) {
+			$cellInstance->viewClass = $this->viewClass;
+		}
+		if ($this instanceof View) {
+			$cellInstance->viewClass = get_class($this);
+		}
 
-		if ($length) {
+		if (!empty($data)) {
 			$data = array_values($data);
 		}
 

+ 25 - 1
tests/TestCase/View/CellTest.php

@@ -21,6 +21,7 @@ use Cake\Event\EventManager;
 use Cake\TestSuite\TestCase;
 use Cake\View\Cell;
 use Cake\View\CellTrait;
+use TestApp\View\CustomJsonView;
 
 /**
  * CellTest class.
@@ -37,7 +38,6 @@ class CellTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		Configure::write('App.namespace', 'TestApp');
-		Configure::write('debug', 2);
 		Plugin::load(['TestPlugin', 'TestTheme']);
 		$request = $this->getMock('Cake\Network\Request');
 		$response = $this->getMock('Cake\Network\Response');
@@ -223,4 +223,28 @@ class CellTest extends TestCase {
 		$this->assertFalse(property_exists('nope', $cell), 'Not a valid option');
 	}
 
+/**
+ * Test that cells get the helper configuration from the view that created them.
+ *
+ * @return void
+ */
+	public function testCellInheritsHelperConfig() {
+		$this->View->helpers = ['Url', 'Form', 'Banana'];
+		$cell = $this->View->cell('Articles');
+		$this->assertSame($this->View->helpers, $cell->helpers);
+	}
+
+/**
+ * Test that cells the view class name of a custom view passed on.
+ *
+ * @return void
+ */
+	public function testCellInheritsCustomViewClass() {
+		$request = $this->getMock('Cake\Network\Request');
+		$response = $this->getMock('Cake\Network\Response');
+		$view = new CustomJsonView($request, $response);
+		$cell = $view->cell('Articles');
+		$this->assertSame('TestApp\View\CustomJsonView', $cell->viewClass);
+	}
+
 }