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

 
www.processing.org: Ngôn ngữ lập trình Processing

www.processing.org: Ngôn ngữ lập trình Processing

Processing là một ngôn ngữ lập trình hiện đại( ra đời năm 2001) cho phép lập trình các ứng dụng đồ họa trên môi trường Window, Linus, Mac Android và cả Web. Nếu bạn đã học C thì bạn có thể tự học Processing rất dễ dàng. Định dang một tập tin Processing là *pde

Facebook bắt đầu triển khai dự án Internet miễn phí

Internet.org là đại diện cho tham vọng của Facebook đem Internet miễn phí đến người dùng trên toàn thế giới

Stanford Law School

Thiết kế Drupal site: Stanford Law School

Stanford Law School (SLS) wanted to migrate their unwieldy proprietary legacy CMS called Pentagram to Drupal. The primary reasons for the migration include:

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

 

Diet con trung