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

 
Hướng dẫn trang trí văn bản giống trang báo 2015

Hướng dẫn trang trí văn bản giống trang báo 2015

REqual column width: Mặc định độ rộng các cọt bằng nhau. Nếu để £ thì có thể điều chỉnh độ rộng các cột khác nhau tùy thích.

Phần 2 xây dựng Responsive Theme trong Drupal 7

Phần 2 xây dựng Responsive Theme trong Drupal 7

The objective of this section is to make the primary menu a toggle menu for the mobile interface. i.e the menu renders on click in mobile devices and is horizontal on screens wider than 600px.

Samsung tung ra phiên bản Galaxy S III dành riêng cho Olympic

Samsung tung ra phiên bản Galaxy S III dành riêng cho Olympic

Có vẻ như để chào đón thế vận hội đang diễn ra tại London nước Anh, Samsung đã tung ra phiên bản smartphone mang đầy màu sắc và hơi thở của Olympic.

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

 

Diet con trung