class StyleguideController in Style Guide 8
Same name and namespace in other branches
- 2.x src/Controller/StyleguideController.php \Drupal\styleguide\Controller\StyleguideController
The Styleguide controller.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\styleguide\Controller\StyleguideController
Expanded class hierarchy of StyleguideController
File
- src/
Controller/ StyleguideController.php, line 17
Namespace
Drupal\styleguide\ControllerView source
class StyleguideController extends ControllerBase {
/**
* The theme handler service.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The theme manager service.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The Styleguide plugin manager.
*
* @var \Drupal\styleguide\StyleguidePluginManager
*/
protected $styleguideManager;
/**
* Constructs a new StyleguideController.
*
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler service.
* @param \Drupal\styleguide\StyleguidePluginManager $styleguide_manager
* The Styleguide plugin manager.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager service.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(ThemeHandlerInterface $theme_handler, StyleguidePluginManager $styleguide_manager, ThemeManagerInterface $theme_manager, RequestStack $request_stack) {
$this->themeHandler = $theme_handler;
$this->styleguideManager = $styleguide_manager;
$this->themeManager = $theme_manager;
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('theme_handler'), $container
->get('plugin.manager.styleguide'), $container
->get('theme.manager'), $container
->get('request_stack'));
}
/**
* Build styleguide page.
*
* @return array
* Renderable array of styleguide items.
*/
public function page() {
// Get active theme.
$active_theme = $this->themeManager
->getActiveTheme()
->getName();
$themes = $this->themeHandler
->rebuildThemeData();
// Get theme data.
$theme_info = $themes[$active_theme]->info;
$items = [];
foreach ($this->styleguideManager
->getDefinitions() as $plugin_id => $plugin_definition) {
$plugin = $this->styleguideManager
->createInstance($plugin_id, [
'of' => 'configuration values',
]);
$items = array_merge($items, $plugin
->items());
}
$this
->moduleHandler()
->alter('styleguide', $items);
$this->themeManager
->alter('styleguide', $items);
$groups = [];
foreach ($items as $key => $item) {
if (!isset($item['group'])) {
$item['group'] = $this
->t('Common');
}
else {
$item['group'] = $this
->t('@group', [
'@group' => $item['group'],
]);
}
$item['title'] = $this
->t('@title', [
'@title' => $item['title'],
]);
$groups[$item['group']
->__toString()][$key] = $item;
}
ksort($groups);
// Create a navigation header.
$header = $head = $content = [];
// Process the elements, by group.
foreach ($groups as $group => $elements) {
foreach ($elements as $key => $item) {
$display = [];
// Output a standard HTML tag.
if (isset($item['tag']) && isset($item['content'])) {
$tag = [
'#type' => 'html_tag',
'#tag' => $item['tag'],
'#value' => $item['content'],
];
if (!empty($item['attributes'])) {
$tag['#attributes'] = $item['attributes'];
}
$display[] = $tag;
}
elseif (isset($item['content']) && is_array($item['content'])) {
$display[] = $item['content'];
}
elseif (isset($item['content'])) {
$display[] = [
'#markup' => $item['content'],
];
}
// Add the content.
$content[] = [
'#theme' => 'styleguide_item',
'#key' => $key,
'#item' => $item,
'#content' => $display,
];
// Prepare the header link.
$uri = $this->requestStack
->getCurrentRequest()
->getUri();
$url = Url::fromUri($uri, [
'fragment' => $key,
]);
$link = Link::fromTextAndUrl($item['title'], $url);
$header[$group][] = $link
->toRenderable();
}
$head[] = [
'#theme' => 'item_list',
'#items' => $header[$group],
'#title' => $group,
];
}
return [
'#title' => 'Style guide',
'header' => [
'#theme' => 'styleguide_header',
'#theme_info' => $theme_info,
],
'navigation' => [
'#theme' => 'styleguide_links',
'#items' => $head,
],
'content' => [
'#theme' => 'styleguide_content',
'#content' => $content,
],
'#attached' => [
'library' => [
'styleguide/styleguide_css',
],
],
];
}
}
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. | |
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. | |
StyleguideController:: |
protected | property | The request stack. | |
StyleguideController:: |
protected | property | The Styleguide plugin manager. | |
StyleguideController:: |
protected | property | The theme handler service. | |
StyleguideController:: |
protected | property | The theme manager service. | |
StyleguideController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
StyleguideController:: |
public | function | Build styleguide page. | |
StyleguideController:: |
public | function | Constructs a new StyleguideController. | |
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. |