class PluginTypeExampleController in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/plugin_type_example/src/Controller/PluginTypeExampleController.php \Drupal\plugin_type_example\Controller\PluginTypeExampleController
Controller for our example pages.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\plugin_type_example\Controller\PluginTypeExampleController
Expanded class hierarchy of PluginTypeExampleController
File
- plugin_type_example/
src/ Controller/ PluginTypeExampleController.php, line 12
Namespace
Drupal\plugin_type_example\ControllerView source
class PluginTypeExampleController extends ControllerBase {
/**
* The sandwich plugin manager.
*
* We use this to get all of the sandwich plugins.
*
* @var \Drupal\plugin_type_example\SandwichPluginManager
*/
protected $sandwichManager;
/**
* Constructor.
*
* @param \Drupal\plugin_type_example\SandwichPluginManager $sandwich_manager
* The sandwich plugin manager service. We're injecting this service so that
* we can use it to access the sandwich plugins.
*/
public function __construct(SandwichPluginManager $sandwich_manager) {
$this->sandwichManager = $sandwich_manager;
}
/**
* Displays a page with an overview of our plugin type and plugins.
*
* Lists all the Sandwich plugin definitions by using methods on the
* \Drupal\plugin_type_example\SandwichPluginManager class. Lists out the
* description for each plugin found by invoking methods defined on the
* plugins themselves. You can find the plugins we have defined in the
* \Drupal\plugin_type_example\Plugin\Sandwich namespace.
*
* @return array
* Render API array with content for the page at
* /examples/plugin_type_example.
*/
public function description() {
$build = [];
$build['intro'] = [
'#markup' => $this
->t("This page lists the sandwich plugins we've created. The sandwich plugin type is defined in Drupal\\plugin_type_example\\SandwichPluginManager. The various plugins are defined in the Drupal\\plugin_type_example\\Plugin\\Sandwich namespace."),
];
// Get the list of all the sandwich plugins defined on the system from the
// plugin manager. Note that at this point, what we have is *definitions* of
// plugins, not the plugins themselves.
$sandwich_plugin_definitions = $this->sandwichManager
->getDefinitions();
// Let's output a list of the plugin definitions we now have.
$items = [];
foreach ($sandwich_plugin_definitions as $sandwich_plugin_definition) {
// Here we use various properties from the plugin definition. These values
// are defined in the annotation at the top of the plugin class: see
// \Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich.
$items[] = $this
->t("@id (calories: @calories, description: @description )", [
'@id' => $sandwich_plugin_definition['id'],
'@calories' => $sandwich_plugin_definition['calories'],
'@description' => $sandwich_plugin_definition['description'],
]);
}
// Add our list to the render array.
$build['plugin_definitions'] = [
'#theme' => 'item_list',
'#title' => 'Sandwich plugin definitions',
'#items' => $items,
];
// If we want just a single plugin definition, we can use getDefinition().
// This requires us to know the ID of the plugin we want. This is set in the
// annotation on the plugin class: see
// \Drupal\plugin_type_example\Plugin\Sandwich\ExampleHamSandwich.
$ham_sandwich_plugin_definition = $this->sandwichManager
->getDefinition('meatball_sandwich');
// To get an instance of a plugin, we call createInstance() on the plugin
// manager, passing the ID of the plugin we want to load. Let's output a
// list of the plugins by loading an instance of each plugin definition and
// collecting the description from each.
$items = [];
// The array of plugin definitions is keyed by plugin id, so we can just use
// that to load our plugin instances.
foreach ($sandwich_plugin_definitions as $plugin_id => $sandwich_plugin_definition) {
// We now have a plugin instance. From here on it can be treated just as
// any other object; have its properties examined, methods called, etc.
$plugin = $this->sandwichManager
->createInstance($plugin_id, [
'of' => 'configuration values',
]);
$items[] = $plugin
->description();
}
$build['plugins'] = [
'#theme' => 'item_list',
'#title' => 'Sandwich plugins',
'#items' => $items,
];
return $build;
}
/**
* {@inheritdoc}
*
* Override the parent method so that we can inject our sandwich plugin
* manager service into the controller.
*
* For more about how dependency injection works read
* https://www.drupal.org/node/2133171
*
* @see container
*/
public static function create(ContainerInterface $container) {
// Inject the plugin.manager.sandwich service that represents our plugin
// manager as defined in the plugin_type_example.services.yml file.
return new static($container
->get('plugin.manager.sandwich'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginTypeExampleController:: |
protected | property | The sandwich plugin manager. | |
PluginTypeExampleController:: |
public static | function |
Override the parent method so that we can inject our sandwich plugin
manager service into the controller. Overrides ControllerBase:: |
|
PluginTypeExampleController:: |
public | function | Displays a page with an overview of our plugin type and plugins. | |
PluginTypeExampleController:: |
public | function | Constructor. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |