class PluginTypeExampleController
Same name 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 \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\AutowireTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\plugin_type_example\Controller\PluginTypeExampleController extends \Drupal\Core\Controller\ControllerBase
Expanded class hierarchy of PluginTypeExampleController
File
-
modules/
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
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
ControllerBase::$configFactory | protected | property | The configuration factory. | ||
ControllerBase::$currentUser | protected | property | The current user service. | 2 | |
ControllerBase::$entityFormBuilder | protected | property | The entity form builder. | ||
ControllerBase::$entityTypeManager | protected | property | The entity type manager. | ||
ControllerBase::$formBuilder | protected | property | The form builder. | 1 | |
ControllerBase::$keyValue | protected | property | The key-value storage. | 1 | |
ControllerBase::$languageManager | protected | property | The language manager. | 1 | |
ControllerBase::$moduleHandler | protected | property | The module handler. | 1 | |
ControllerBase::$stateService | protected | property | The state service. | ||
ControllerBase::cache | protected | function | Returns the requested cache bin. | ||
ControllerBase::config | protected | function | Retrieves a configuration object. | ||
ControllerBase::container | private | function | Returns the service container. | ||
ControllerBase::currentUser | protected | function | Returns the current user. | 2 | |
ControllerBase::entityFormBuilder | protected | function | Retrieves the entity form builder. | ||
ControllerBase::entityTypeManager | protected | function | Retrieves the entity type manager. | ||
ControllerBase::formBuilder | protected | function | Returns the form builder service. | 1 | |
ControllerBase::keyValue | protected | function | Returns a key/value storage collection. | 1 | |
ControllerBase::languageManager | protected | function | Returns the language manager service. | 1 | |
ControllerBase::moduleHandler | protected | function | Returns the module handler. | 1 | |
ControllerBase::redirect | protected | function | Returns a redirect response object for the specified route. | ||
ControllerBase::state | protected | function | Returns the state storage service. | ||
LoggerChannelTrait::$loggerFactory | protected | property | The logger channel factory service. | ||
LoggerChannelTrait::getLogger | protected | function | Gets the logger for a specific channel. | ||
LoggerChannelTrait::setLoggerFactory | public | function | Injects the logger channel factory. | ||
MessengerTrait::$messenger | protected | property | The messenger. | 16 | |
MessengerTrait::messenger | public | function | Gets the messenger. | 16 | |
MessengerTrait::setMessenger | public | function | Sets the messenger. | ||
PluginTypeExampleController::$sandwichManager | protected | property | The sandwich plugin manager. | ||
PluginTypeExampleController::create | public static | function | Override the parent method so that we can inject our sandwich plugin manager service into the controller. |
Overrides AutowireTrait::create | |
PluginTypeExampleController::description | public | function | Displays a page with an overview of our plugin type and plugins. | ||
PluginTypeExampleController::__construct | public | function | Constructor. | ||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 2 | |
RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | ||
RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | ||
RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | ||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | 3 | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | ||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | ||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | ||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |