Hướng dẫn tạo 1 Drupal 8 block năm 2015

Hướng dẫn tạo 1 Drupal 8 block năm 2015

We continue to explore the possibilities offered by the Console module and will discover how to build a Drupal 8 block with a few command and in a very short time.

If you have not yet installed console, you can read this post Create a Drupal module 8 in 30 seconds.

We will generate a block that will allow us to display a welcome text and the name of the user logged in. Execute the following command

cd /path/to/drupal8folder

bin/console generate:plugin:block

As you may have noticed, blocks in Drupal 8 are now plugins. And because of this we generate a block type plugin.

Génération d'un bloc Durpal 8

As always, we must indicate in which module we will generate the code (in our example, our module entitled example, original eh?).

Then we define respectively the block name, label and ID. We also have the ability to associate a form with the block that will allow us to associate specific fields. In our example, we create a text field that will contain the welcome message.

Our command will generate a file in the directory src/Plugin/Block entitled CurrentUser.php. Consult the code generated.

/**
 * @file
 * Contains Drupal\example\Plugin\Block\CurrentUser.
 */
namespace Drupal\example\Plugin\Block;

use Drupal\Core\Block\BlockBase;

use Drupal\Core\Form\FormStateInterface;
/**

 * Provides a 'CurrentUser' block.

 *

 * @Block(

 *  id = "current_user",

 *  admin_label = @Translation("current_user")

 * )

 */

class CurrentUser extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    return [

      '#markup' => 'current_user',

    ];

  }

  /**

   * Overrides \Drupal\block\BlockBase::blockForm().

   */

  public function blockForm($form, FormStateInterface $form_state) {

    $form['welcome_text'] = [

      '#type' => 'textfield',

      '#title' => $this->t('Welcome text'),

      '#description' => $this->t(''),

      '#default_value' => isset($this->configuration['welcome_text']) ? $this->configuration['welcome_text'] : '',  ];

    return $form;
  }
  /**
   * Overrides \Drupal\block\BlockBase::blockSubmit().
   */
  public function blockSubmit($form, FormStateInterface $form_state) {

    $this->configuration['welcome_text'] = $form_state->getValue('welcome_text');
  }
}

We can modify the build() function to retrieve the account of the logged in user and display our welcome message that we can configure in the block's parameters.

public function build() {

  // Fetch current user.

  $account = \Drupal::currentUser();

  return [

    '#markup' => t('@welcome @username',

      array(

        '@welcome' => $this->configuration['welcome_text'],

        '@username' => user_format_name($account)

      )

    ),

  ];

}

And it remains for us to instantiate our block from the Administration page and position it for example in the first sidebar.

Page d'administration des blocs Drupal 8

Configuration d'un bloc Drupal 8

And our result.

Hướng dẫn tạo 1 Drupal 8 block năm 2015

Devilishly effective, do not you think ? This is almost self-explanatory, but feel free.

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

 
 Cấu hình Web Server trên Windows server 2003

Cấu hình Web Server trên Windows server 2003

Web Server là máy chủ có dung lượng lớn, tốc độ cao, được dùng để lưu trữ thông tin như một ngân hàng dữ liệu, chứa những website đã được thiết kế cùng với những thông tin liên quan khác. (các mã Script, các chương trình, và các file Multimedia)

Samsung lên kế hoạch phát triển mạng xã hội giống Facebook

Samsung lên kế hoạch phát triển mạng xã hội giống Facebook

Tờ Korea Times mới đây cho biết Samsung đang có kế hoạch phát triển một mạng xã hội giống ý tưởng của Facebook hiện nay và sẽ sẵn sàng đi vào hoạt động kể từ đầu năm 2013 tới.

PHP – Ruby – Python

Sự khác nhau giữa các ngôn ngữ PHP – Ruby – Python

Cũng giống như Tiếng Việt, Tiếng Anh, Tiếng Nhật… trong lập trình cũng có rất nhiều ngôn ngữ. Mỗi ngôn ngữ đều có những ưu điểm, khuyết điểm, và các thế mạnh cũng như môi trường, mục đích sử dụng khác nhau

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

 

Diet con trung