Commit 9c74b547 authored by alexpott's avatar alexpott

Issue #1863816 by dawehner, yched, rootatwc: Allow plugins to have services injected.

parent 3e88fc4e
<?php
/**
* @file
* Contains \Drupal\Core\Plugin\ContainerFactoryPluginBase.
*/
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a base plugin that can pull it's dependencies from the container.
*/
class ContainerFactoryPluginBase extends PluginBase {
/**
* Creates an instance of the plugin.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container to pull out services used in the plugin.
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
*
* @return static
* Returns an instance of this plugin.
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Plugin\Factory\ContainerFactory.
*/
namespace Drupal\Core\Plugin\Factory;
use Drupal\Component\Plugin\Factory\DefaultFactory;
/**
* Plugin factory which passes a container to a create method.
*/
class ContainerFactory extends DefaultFactory {
/**
* {inheritdoc}
*/
public function createInstance($plugin_id, array $configuration) {
$plugin_definition = $this->discovery->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
}
}
......@@ -14,6 +14,7 @@
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Factory\ContainerFactory;
/**
* Plugin type manager for all views plugins.
......@@ -35,7 +36,7 @@ public function __construct($type, array $namespaces = array()) {
$this->discovery = new AlterDecorator($this->discovery, 'views_plugins_' . $type);
$this->discovery = new CacheDecorator($this->discovery, 'views:' . $type, 'views_info');
$this->factory = new DefaultFactory($this->discovery);
$this->factory = new ContainerFactory($this);
$this->defaults += array(
'parent' => 'parent',
......
......@@ -7,11 +7,11 @@
namespace Drupal\views\Plugin\views;
use Drupal\Core\Plugin\ContainerFactoryPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\Component\Plugin\PluginBase as ComponentPluginBase;
use Drupal\views\ViewExecutable;
abstract class PluginBase extends ComponentPluginBase {
abstract class PluginBase extends ContainerFactoryPluginBase {
/**
* Options for this plugin will be held here.
......
......@@ -6,7 +6,7 @@
*/
namespace Drupal\views\Plugin\views\join;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginBase;
/**
* @defgroup views_join_handlers Views join handlers
......@@ -49,7 +49,7 @@
*
* Extensions of this class can be used to create more interesting joins.
*/
class JoinPluginBase extends PluginBase {
class JoinPluginBase extends ContainerFactoryPluginBase {
/**
* The table to join (right table).
......
......@@ -7,9 +7,12 @@
namespace Drupal\views\Plugin\views\style;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\views\Plugin\views\wizard\WizardInterface;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Style plugin to render each item as a row in a table.
......@@ -59,6 +62,29 @@ class Table extends StylePluginBase {
*/
public $order;
/**
* Contains the current request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* {inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('request'));
}
/**
* Constructs a Table object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->request = $request;
}
protected function defineOptions() {
$options = parent::defineOptions();
......@@ -80,7 +106,7 @@ protected function defineOptions() {
* @return bool
*/
function build_sort() {
$order = drupal_container()->get('request')->query->get('order');
$order = $this->request->query->get('order');
if (!isset($order) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
return TRUE;
}
......@@ -98,7 +124,7 @@ function build_sort() {
* Add our actual sort criteria
*/
function build_sort_post() {
$query = drupal_container()->get('request')->query;
$query = $this->request->query;
$order = $query->get('order');
if (!isset($order)) {
// check for a 'default' clicksort. If there isn't one, exit gracefully.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment