You are here

class StyleguideController in Style Guide 2.x

Same name and namespace in other branches
  1. 8 src/Controller/StyleguideController.php \Drupal\styleguide\Controller\StyleguideController

The Styleguide controller.

Hierarchy

Expanded class hierarchy of StyleguideController

File

src/Controller/StyleguideController.php, line 17

Namespace

Drupal\styleguide\Controller
View 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

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
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. 1
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. 2
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. 2
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
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. 4
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.
StyleguideController::$requestStack protected property The request stack.
StyleguideController::$styleguideManager protected property The Styleguide plugin manager.
StyleguideController::$themeHandler protected property The theme handler service.
StyleguideController::$themeManager protected property The theme manager service.
StyleguideController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
StyleguideController::page public function Build styleguide page.
StyleguideController::__construct public function Constructs a new StyleguideController.