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',


        '@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 Tran 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
  • Phone/Zalo: (+84) 944 225 212
  • WhatsApp: (+84) 944 225 212
  • Line Messenger: (+84) 944 225 212
  • Email: asaleotestf@gmail.com
  • Telegram Messenger: https:/t.me/tommytran0401

Quảng cáo việc làm


Thích hợp các bạn nữ mảng thợ may làm việc tại nước NGA

Đơn hàng Tuyển dụng 100 Thợ may đi Nga(đợt 1 tháng 3.2021, đợt 2 tháng 5.2021). Lương thực lãnh 800 USD, bao ăn ở, vé máy bay và visa, phí xuất cảnh(1800 USD)trả khi đi làm có lương. Bạn có thể liên hệ CÔNG TY qua Phone/Zalo: (+84) 944 225 212. Công ty sẽ tư vấn cho bạn.

Xem chi tiết: >>> https://bit.ly/3o9NOfR

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





Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

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

Achieve Rocketship-Fast Jobs in CircleCI by Preinstalling the Database

Sử dụng CircleCI làm environment for testing trong Drupal 8

CircleCI is great at enabling developers to spin up an environment for testing from a set of defined images. When dealing with a website with a database, the usual build process involves downloading a database dump

Lộ diện Lenovo ThinkPad X1 Carbon với màn hình cảm ứng

Lộ diện Lenovo ThinkPad X1 Carbon với màn hình cảm ứng

Phiên bản được trang bị màn hình cảm ứng của ultrabook ThinkPad X1 Carbon mang tên ThinkPad X1 Carbon Touch đã được Lenovo tiết lộ trên trang web của mình.

Creating a Workflow for Drupal Users

Tạo phiên làm việc của 1 node type cho Drupal Users

This week's tutorial is the second of a two-parter. We've had several students in our classes looking to build websites with multiple content authors ... blogs, newspapers, university sites and more.