Issue #2032031 by damiankloip: Deprecate use of views_get_view() function in favour of Views::getView() method.

8.0.x
Alex Pott 2013-07-16 21:17:11 -04:00
parent 14708544c8
commit d8e21f879c
3 changed files with 88 additions and 4 deletions

View File

@ -75,4 +75,20 @@ class Views {
return Drupal::service('plugin.manager.views.' . $type);
}
/**
* Loads a view from configuration and returns its executable object.
*
* @param string $id
* The view ID to load.
*
* @return \Drupal\views\ViewExecutable
* A view executable instance, from the loaded entity.
*/
public static function getView($id) {
$view = Drupal::service('plugin.manager.entity')->getStorageController('view')->load($id);
if ($view) {
return static::executableFactory()->get($view);
}
}
}

View File

@ -0,0 +1,68 @@
<?php
/**
* @file
* Contains \Drupal\views\Tests\ViewsTest.
*/
namespace Drupal\views\Tests;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Views;
use Drupal\views\Plugin\Core\Entity\View;
use Drupal\views\ViewExecutableFactory;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
class ViewsTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Views test',
'description' => 'Tests the Drupal\views\Views class.',
'group' => 'Views',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$container = new ContainerBuilder();
$container->set('views.executable', new ViewExecutableFactory());
$this->view = new View(array('id' => 'test_view'), 'view');
$view_storage_controller = $this->getMockBuilder('Drupal\views\ViewStorageController')
->disableOriginalConstructor()
->getMock();
$view_storage_controller->expects($this->once())
->method('load')
->with('test_view')
->will($this->returnValue($this->view));
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->once())
->method('getStorageController')
->with('view')
->will($this->returnValue($view_storage_controller));
$container->set('plugin.manager.entity', $entity_manager);
Drupal::setContainer($container);
}
/**
* Tests the getView() method.
*/
public function testGetView() {
$executable = Views::getView('test_view');
$this->assertInstanceOf('Drupal\views\ViewExecutable', $executable);
$this->assertEquals($this->view->id(), $executable->storage->id());
$this->assertEquals(spl_object_hash($this->view), spl_object_hash($executable->storage));
}
}

View File

@ -1105,12 +1105,12 @@ function views_disable_view(View $view) {
*
* @return Drupal\views\ViewExecutable
* A reference to the $view object.
*
* @deprecated as of Drupal 8.0. Use \Drupal\views\Views::getView().
*
*/
function views_get_view($name) {
$view = entity_load('view', $name);
if ($view) {
return $view->get('executable');
}
return Views::getView($name);
}
/**