diff --git a/core/modules/views/lib/Drupal/views/Views.php b/core/modules/views/lib/Drupal/views/Views.php index 80cf166b041..e7127c71eb8 100644 --- a/core/modules/views/lib/Drupal/views/Views.php +++ b/core/modules/views/lib/Drupal/views/Views.php @@ -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); + } + } + } diff --git a/core/modules/views/tests/Drupal/views/Tests/ViewsTest.php b/core/modules/views/tests/Drupal/views/Tests/ViewsTest.php new file mode 100644 index 00000000000..91c22260c85 --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/ViewsTest.php @@ -0,0 +1,68 @@ + '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)); + } + +} diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 2b0a75a23c6..5c4bb60af72 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -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); } /**