Thực hiện query, tạo pagination và element Type là table trong Drupal 8

Thực hiện query, tạo pagination và element Type là table trong Drupal 8

Thực hiện query lấy hết bảng dữ liệu không cần phân quyền trong Drupal 8

// $node = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple();

Nếu cần phân quyền thì thực hiện câu Query sau

 $query = \Drupal::entityQuery('node')
            ->accessCheck(FALSE)           

Sau đây là 1 ví dụ cụ thể

public function my_requests() {       
        //load the current user details
        $user= User::load(\Drupal::currentUser()->id());
        $user_id = \Drupal::currentUser()->id();

        //Query the database
        $query = \Drupal::entityQuery('node')
           ->tableSort($header)
           ->condition('status', 1)
           ->condition('type', "forward_proxy_request")
           ->condition('uid', $user_id)
           ->sort('nid' , 'ASC');
        $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
        $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
        $nids = $pager->execute();

        //creates the table header
        $header = [
                'Node id',
                'title',               
                'Name',
                'Ip',
                'SLU',
            ];  
        foreach ($nids as $nid) {
            $node = Node::load($nid);
            $row  = [
                'data' => [
                    $node->nid->value,
                    $node->title->value, 
                    $node->field_p_name[0]->value,
                    $node->field_ip[0]->value,
                    $node->field_p_slu[0]->value,
                ],
            ];
            $rows[] = $row;
        }
        //Build the table
        $build = [
          'table'           => [
          '#prefix'        => '<h1>My Requests</h1>',
          '#theme'         => 'table',
          '#attributes'    => [
              'data-striping' => 0
          ],
          '#header' => $header,
          '#rows'   => $rows,
      ],
    ];
    //for pagination
    $build['pager'] = array(
        '#type' => 'pager'
    );

     return $build;          

It shows a plain table without any style.

it shows a plain table without any style.

Here is an example of sortable table with pagination.

$header = [
  'id' => [
    'data' => $this->t('ID'),
    'specifier' => 'nid',
  ],
  'title' => [
    'data' => $this->t('Title'),
    'specifier' => 'title',
  ],
  'created' => [
    'data' => $this->t('Created'),
    'specifier' => 'created',
    // Set default sort criteria.
    'sort' => 'desc',
  ],
  'uid' => [
    'data' => $this->t('Author'),
    'specifier' => 'uid',
  ],
];

$storage = \Drupal::entityTypeManager()->getStorage('node');

$query = $storage->getQuery();
$query->condition('status', \Drupal\node\NodeInterface::PUBLISHED);
$query->condition('type', 'article');
$query->tableSort($header);
// Default value is 10.
$query->pager(15);
$nids = $query->execute();

$date_formatter = \Drupal::service('date.formatter');
$rows = [];
foreach ($storage->loadMultiple($nids) as $node) {
  $row = [];
  $row[] = $node->id();
  $row[] = $node->toLink();
  $created = $node->get('created')->value;
  $row[] = [
    'data' => [
      '#theme' => 'time',
      '#text' => $date_formatter->format($created),
      '#attributes' => [
        'datetime' => $date_formatter->format($created, 'custom', \DateTime::RFC3339),
      ],
    ],
  ];
  $row[] = [
    'data' => $node->get('uid')->view(),
  ];
  $rows[] = $row;
}

$build['table'] = [
  '#type' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => $this->t('No content has been found.'),
];

$build['pager'] = [
  '#type' => 'pager',
];

Thực hiện query, tạo pagination và element Type là table trong Drupal 8

Bạn thấy bài viết này như thế nào?: 
Average: 10 (2 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Nữ diễn viên Hàn bị phạt 26.000 USD vì ngoại tình với anh rể

Phạt khoản tiền 30 triệu won (26.000 USD) vì ngoại tình với anh rể

Tin của Sports Kyunghyang ngày 3.8.2021 cho biết một nữ diễn viên đã bị tòa phạt 30 triệu won (26.000 USD) vì ngoại tình với anh rể. Phiên tòa xét xử diễn ra từ ngày 9/7 nhưng tới nay thông tin mới được tiết lộ.

Cuộc chiến iOS và Android - Ai sẽ giành chiến thắng ?

Cuộc chiến iOS và Android - Ai sẽ giành chiến thắng ?

Ai sẽ giành chiến thắng trong cuộc chiến hệ điều hành cho điện thoại: Apple iOS hay là Google Android ? Đây là câu hỏi ám ảnh đối với những người trong giới công nghệ trong suốt thời gian qua...

GS Phan Thành Nam

GS Phan Thành Nam lĩnh vực nghiên cứu là giải tích vật lý toán học

GS Phan Thành Nam, 35 tuổi, quê Phú Yên, là một trong 10 nhà toán học nhận được giải thưởng EMS từ Hội Toán học châu Âu năm 2020.

Công ty diệt chuột T&C

 

Diet con trung