Skip to content
Permalink
Browse files

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
  • Loading branch information...
markstory committed Aug 25, 2014
1 parent 8799141 commit 88b342b2dfe3fc9b5df105cbb8b94d0a009e6cac
Showing 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
@@ -87,6 +87,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.
*
@@ -16,6 +16,7 @@
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 @@ public function cell($cell, array $data = [], array $options = []) {
$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);
}
@@ -21,6 +21,7 @@
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 @@ public function testCellOptions() {
$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);
}
}

0 comments on commit 88b342b

Please sign in to comment.
You can’t perform that action at this time.