You are here

public function StyleguideController::page in Style Guide 8

Same name and namespace in other branches
  1. 2.x src/Controller/StyleguideController.php \Drupal\styleguide\Controller\StyleguideController::page()

Build styleguide page.

Return value

array Renderable array of styleguide items.

1 string reference to 'StyleguideController::page'
styleguide.routing.yml in ./styleguide.routing.yml
styleguide.routing.yml

File

src/Controller/StyleguideController.php, line 84

Class

StyleguideController
The Styleguide controller.

Namespace

Drupal\styleguide\Controller

Code

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',
      ],
    ],
  ];
}