Làm việc với Drupal 8 Block Cache năm 2016

Làm việc với Drupal 8 Block Cache năm 2016

In case you've had trouble tracking it down (I did), the approach to caching custom blocks is different in Drupal 8 than in Drupal 7. In Drupal 8 all renderable arrays are cacheable – including, of course, those returned by custom blocks.

The new and much-improved Cache API in Drupal 8 provides a sophisticated approach to caching all things renderable–whether pages, entities or, for our case, blocks. Whereas in Drupal 7 default cache settings were returned along with other block settings in "hook_block_info()", Drupal 8 allows developers to manage cache behavior for blocks directly in the render array returned by the block object's "build()" method.

In Drupal 7, caching a block by role looks this:

function mymodule_block_info() {
  $blocks = array();
  $blocks['mymodule_example_block'] = array(
    'info' => t('Block title'),
    // Block caching options (per role, per user, etc.)
    // DRUPAL_CACHE_PER_ROLE is the default.
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

In Drupal 8, cache settings are manipulated directly in renderable arrays returned by (among other things) a block's build() method:

class MyCustomBlock extends BlockBase {
  public function build() {
    return array(
      '#markup' => $this->t('My custom block content'),
      '#cache' => array(
        'contexts' => array('user.roles'),
      ),
    );
  }

Available parameters for manipulating cache settings include 'keys', 'contexts', 'tags', 'max-age' and 'bin'. Detailed documentation about cache management in Drupal 8 is available on Drupal.org.

Beyond implications for caching blocks (which had me scouring Google when first writing custom block plugins for Drupal 8) there are huge implications for caching everything. In Drupal 8, cache behavior needs to be a consideration when rendering absolutely anything via the render API. More on that on Drupal.org.

 

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ả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

Tìm kiếm bất động sản

 

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

 
Tạo mới một node với Node Option Premium.module

Tạo mới một node với Node Option Premium.module

Node Option Premium lets you show a content teaser to the public but show the full content only to people who are logged in and have a certain role. This is typical of the strategy used by many newspapers and magazines with their paywall.

Address Field Module trong Drupal hiện tại chưa Scale

Address Field is a handy Drupal module--spun out of Commerce--that provides a field able to be attached to any Drupal entity.

Lâm Đào Trúc Anh, học sinh lớp 12 Văn trường Phổ thông Năng khiếu - Đại học Quốc gia TP HCM

Lâm Đào Trúc Anh giành 12 học bổng trị giá gần 1,8 triệu USD

Đỗ hai trường ở Mỹ và Canada, Lâm Đào Trúc Anh còn được 12 đại học khác cấp học bổng với tổng trị giá gần 1,8 triệu USD - khoảng 42 tỷ đồng.

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

 

Diet con trung