Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\hello\Controller;
  4.  
  5. use Drupal\Component\Serialization\Json;
  6. use Drupal\Core\Controller\ControllerBase;
  7. use Drupal\Core\Link;
  8. use Drupal\Core\Url;
  9.  
  10. class NodeListController extends ControllerBase {
  11.  
  12.   /**
  13.    * @param string $nodetype
  14.    * @return array
  15.    */
  16.   public function content($nodetype = NULL) {
  17.     $node_types = $this->entityTypeManager()->getStorage('node_type')->loadMultiple();
  18.     $item_list = [];
  19.     foreach ($node_types as $node_type) {
  20.       $url = new Url('hello.hello.node_list', ['nodetype' => $node_type->id()]);
  21.       $item_list[] = new Link($node_type->label(), $url);
  22.     }
  23.     $query = $this->entityTypeManager()->getStorage('node')->getQuery();
  24.     // Si on a un argument dans l'URL, on ne cible que les noeuds correspondants.
  25.     if ($nodetype) {
  26.       $query->condition('type', $nodetype);
  27.     }
  28.     // On construit une requête paginée.
  29.     $nids = $query->pager('10')->execute();
  30.  
  31.     // Charge les noeuds correspondants au résultat de la requête.
  32.     $nodes = $this->entityTypeManager()->getStorage('node')->loadMultiple($nids);
  33.  
  34.     // Construit un tableau de liens vers les noeuds.
  35.     $items = [];
  36.     foreach ($nodes as $node) {
  37.       //$items[] = $node->toLink();
  38.       $items[] = [
  39.         '#type' => 'link',
  40.         '#title' => $this->t('@node_label', ['@node_label' => $node->label()]),
  41.         '#attributes' => [
  42.           'class' => ['use-ajax'],
  43.           'data-dialog-type' => 'dialog',
  44.           'data-dialog-options' => Json::encode([
  45.             'width' => 1000,
  46.           ]),
  47.         ],
  48.         '#url' => Url::fromRoute('entity.node.canonical', ['node' => $node->id(), 'nojs' => 'ajax']),
  49.         '#attached' => [
  50.           'library' => [
  51.             'core/drupal.ajax',
  52.             'core/drupal.dialog',
  53.             'core/drupal.dialog.ajax',
  54.           ]
  55.         ],
  56.       ];
  57.     }
  58.     $node_type_list = [
  59.       '#theme' => 'item_list',
  60.       '#items' => $item_list,
  61.       '#title' => $this->t('Filter by node types'),
  62.     ];
  63.     $list = [
  64.       '#theme' => 'item_list',
  65.       '#items' => $items,
  66.       '#title' => $this->t('Node list'),
  67.     ];
  68.     // Render array pour la pagination.
  69.     $pager = ['#type' => 'pager'];
  70.  
  71.     return [
  72.       'node_type_list' => $node_type_list,
  73.       'list' => $list,
  74.       'pager' => $pager,
  75.       '#cache' => [
  76.         'keys' => ['hello:node_list'],
  77.         'tags' => ['node_list'],
  78.         'contexts' => ['url'],
  79.       ],
  80.     ];
  81.   }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement