Last active
March 24, 2017 09:41
-
-
Save colorfield/1a80e0fc1ca384e4ba3f533aa462ec69 to your computer and use it in GitHub Desktop.
Drupal 8 EntityController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\my_module\Controller; | |
use Drupal\Core\Controller\ControllerBase; | |
use Drupal\node\Entity\Node; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Entity\EntityTypeManager; | |
use Drupal\Core\Entity\Query\QueryFactory; | |
use Drupal\Core\Render\Renderer; | |
/** | |
* Class NodeEntityController. | |
* | |
* @package Drupal\my_module\Controller | |
*/ | |
class NodeEntityController extends ControllerBase { | |
/** | |
* Drupal\Core\Entity\EntityTypeManager definition. | |
* | |
* @var \Drupal\Core\Entity\EntityTypeManager | |
*/ | |
protected $entityTypeManager; | |
/** | |
* Drupal\Core\Entity\Query\QueryFactory definition. | |
* | |
* @var Drupal\Core\Entity\Query\QueryFactory | |
*/ | |
protected $entityQuery; | |
/** | |
* Drupal\Core\Render\Renderer definition. | |
* | |
* @var \Drupal\Core\Render\Renderer | |
*/ | |
protected $renderer; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(EntityTypeManager $entity_type_manager, | |
QueryFactory $entity_query, | |
Renderer $renderer) { | |
$this->entityTypeManager = $entity_type_manager; | |
$this->entityQuery = $entity_query; | |
$this->renderer = $renderer; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity_type.manager'), | |
$container->get('entity.query'), | |
$container->get('renderer') | |
); | |
} | |
/** | |
* Returns node ids for a content type. | |
* | |
* @param $type | |
* @return array|int | |
*/ | |
private function getNodeIds($content_type) { | |
$query = $this->entityQuery->get('node') | |
->condition('type', $content_type); | |
$ids = $query->execute(); | |
return $ids; | |
} | |
/** | |
* Returns the markup for a single node | |
*/ | |
private function getNodeMarkup($node_id, $view_mode = 'full') { | |
$node = $this->entityTypeManager->getStorage('node')->load($node_id); | |
$currentLanguageId = $this->languageManager->getCurrentLanguage()->getId(); | |
if($node instanceof Node && $node->hasTranslation($currentLanguageId)) { | |
$nodeTranslation = $node->getTranslation($currentLanguageId); | |
$node = $nodeTranslation; | |
} | |
$viewBuilder = $this->entityTypeManager->getViewBuilder($node->getEntityTypeId()); | |
$nodeView = $viewBuilder->view($node, $view_mode, $this->languageManager->getCurrentLanguage()); | |
$build = [ | |
'#markup' => $this->renderer->render($nodeView), | |
]; | |
return $build; | |
} | |
private function getNodeList($content_type) { | |
$ids = $this->getNodeIds($content_type); | |
$nodes = []; | |
// for example only, we should make use of loadMultiple instead | |
foreach($ids as $nodeId) { | |
$nodes[] = $this->getNodeMarkup($nodeId); | |
} | |
return $nodes; | |
} | |
/** | |
* Default page. | |
* | |
* @return string | |
* Return Node list. | |
*/ | |
public function main() { | |
$build = [ | |
'#theme' => 'item_list', | |
'#items' => $this->getNodeList('my_content_type'), | |
'#type' => 'ul', | |
]; | |
return $build; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment