Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.60 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\hello\Controller;
  4.  
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Drupal\Core\Database\Connection;
  7. use Drupal\Core\Datetime\DateFormatter;
  8. use Drupal\node\NodeInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10.  
  11. class HelloNodeHistoryController extends ControllerBase {
  12.  
  13.   protected $database;
  14.   protected $dateFormatter;
  15.  
  16.   public function __construct(Connection $database, DateFormatter $dateFormatter) {
  17.     $this->database = $database;
  18.     $this->dateFormatter = $dateFormatter;
  19.   }
  20.  
  21.   public static function create(ContainerInterface $container) {
  22.     return new static(
  23.       $container->get('database'),
  24.       $container->get('date.formatter')
  25.     );
  26.   }
  27.  
  28.   public function content(NodeInterface $node) {
  29.     $query = $this->database->select('hello_node_history', 'hnh')
  30.       ->fields('hnh', array('uid', 'update_time'))
  31.       ->condition('nid', $node->id());
  32.     // Tableau des updates.
  33.     $result = $query->execute();
  34.  
  35.     $rows = array();
  36.     $userStorage = $this->entityTypeManager()->getStorage('user');
  37.     foreach ($result as $record) {
  38.       $rows[] = array(
  39.         $userStorage->load($record->uid)->toLink(),
  40.         $this->dateFormatter->format($record->update_time),
  41.       );
  42.     }
  43.     $table = array(
  44.       '#theme'  => 'table',
  45.       '#header' => array($this->t('Author'), $this->t('Update time')),
  46.       '#rows'   => $rows,
  47.     );
  48.  
  49.     // Pagination.
  50.     $pager = array('#type' => 'pager');
  51.  
  52.     // On renvoie les 2 render arrays.
  53.     return array(
  54.       'table' => $table,
  55.       'pager' => $pager
  56.     );
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement