You are here

AnswersNodeController.php in Answers 8

File

src/Controller/AnswersNodeController.php
View source
<?php

namespace Drupal\answers\Controller;

use Drupal\node\Controller\NodeController;

/**
 * Returns my customizations for Node routes.
 */
class AnswersNodeController extends NodeController {

  /**
   * {@inheritdoc}
   */
  public function addPage() {
    $build = [
      '#theme' => 'node_add_list',
      '#cache' => [
        'tags' => $this
          ->entityTypeManager()
          ->getDefinition('node_type')
          ->getListCacheTags(),
      ],
    ];
    $content = [];

    // Only use node types the user has access to.
    foreach ($this
      ->entityTypeManager()
      ->getStorage('node_type')
      ->loadMultiple() as $type) {
      if ($type
        ->id() != 'answers_answer') {
        $access = $this
          ->entityTypeManager()
          ->getAccessControlHandler('node')
          ->createAccess($type
          ->id(), NULL, [], TRUE);
        if ($access
          ->isAllowed()) {
          $content[$type
            ->label()] = $type;
        }
      }
      ksort($content);
      $this->renderer
        ->addCacheableDependency($build, $access);
    }

    // Bypass the node/add listing if only one content type is available.
    if (count($content) == 1) {
      $type = array_shift($content);
      return $this
        ->redirect('node.add', [
        'node_type' => $type
          ->id(),
      ]);
    }
    $build['#content'] = $content;
    return $build;
  }

}

Classes

Namesort descending Description
AnswersNodeController Returns my customizations for Node routes.