Drupal 8 Module Development, Phần 3 - viết Plugins

Drupal 8 Module Development, Phần 3 - viết Plugins

As with any new version of Drupal, a number of contributed modules will be refactored, and pulled into core. Among them is ctools which has been a Drupal staple since version 6. If you're at all familiar with the current ctools implementation of plugins, the Drupal 8 version of this is very similar and will be used to replace a number of hooks. This is another important step in moving towards less procedural and more object-oriented code.

Plugins are a pretty broad subject which I'm sure will get covered more, both here and on other blogs as time progresses. In this example, we'll focus on how and why they're replacing hooks, as well as how to use them for simple tasks like creating blocks.

What are they?

Plugins are not too dissimilar from hooks. They are used to modularly include elements and functionality into a module via its dependent modules. This is pretty much exactly what hooks are for, except they are packaged differently and the implementation is much cleaner.

Creating Blocks

Step 1: Create your plugin file

Start by creating the file foobar/lib/Drupal/foobar/Plugin/Block/FoobarBlock.php with an empty class. This is where we will define the properties of our block which would have previously been handled with hook_block_info, hook_block_view, hook_block_configure, and hook_block_save.

<?php

/**
 * @file
 * Contains \Drupal\foobar\Plugin\Block\FoobarBlock.
 */

namespace Drupal\foobar\Plugin\Block;

use Drupal\block\BlockBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

class FoobarBlock extends BlockBase {

}

Step 2: Block Methods

There are a number of methods that can be used to override the defaults defined in BlockBase class to provide custom functionality.

access

Used to specify who can view the block.

/**
 * Implements \Drupal\block\BlockBase::access().
 */
public function access() {
  return user_access('access content');
}

blockBuild

Used to define the renderable output of the block.

/**
 * Implements \Drupal\block\BlockBase::blockBuild().
 */
protected function blockBuild() {
  return array(
    '#markup' => t('Foobar'),
  );
}

blockForm, blockValidate, and blockSubmit

Used to add additional fields to the block configuration form and manage the validation and submit behavior of the form.

/**
 * Implements \Drupal\block\BlockBase:: blockForm().
 */
public function blockForm($form, &$form_state) {
  $form['foobar'] = array(
    '#type' => 'textfield',
    '#title' => t('Foobar'),
  );
  return $form;
}

public function blockValidate($form, &$form_state) {}
public function blockSubmit($form, &$form_state) {}

For this example, we'll use the blockBuild method.

<?php

/**
 * @file
 * Contains \Drupal\foobar\Plugin\Block\FoobarBlock.
 */

namespace Drupal\foobar\Plugin\Block;

use Drupal\block\BlockBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

class FoobarBlock extends BlockBase {

  /**
   * Implements \Drupal\block\BlockBase::blockBuild().
   */
  protected function blockBuild() {
    return array(
      '#markup' => t('Foobar'),
    );
  }

}

Step 3: Annotation

Annotation is something that has been introduced by Symfony and is used to register a plugin with Drupal much like the .info file for modules. Annotation is basically comments designed to specify parameters about the code that it is surrounding. Without the annotation, Drupal will not recognize your plugin, and it will not work.

<?php

/**
 * @file
 * Contains \Drupal\foobar\Plugin\Block\FoobarBlock.
 */

namespace Drupal\foobar\Plugin\Block;

use Drupal\block\BlockBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
 * Provides a 'Foobar' block.
 *
 * @Plugin(
 *   id = "foobar_foobar_block",
 *   admin_label = @Translation("Foobar"),
 *   module = "foobar"
 * )
 */
class FoobarBlock extends BlockBase {

  /**
   * Implements \Drupal\block\BlockBase::blockBuild().
   */
  protected function blockBuild() {
    return array(
      '#markup' => t('Foobar'),
    );
  }

}

 

Tags: 
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

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ác thao tác cơ sở làm việc mongodb trong java

Các thao tác cơ sở làm việc mongodb trong java

Để  làm việc được với mongodb trước hết bạn download thư viện mongo tại link sau :

Drupal Developer

Những công cụ cho Drupal Developer

Drupal là nền tảng cực kỳ tốt để bạn có thể dựa vào đó và phát triển các ứng dụng web của mình. Tuy nhiên, để trở thành một drupal developer giỏi, bạn cần nhiều thời gian học hỏi và không sử dụng thành thạo các công cụ hỗ trơ. Xin giới thiệu với bạn những công cụ mà chúng tôi biết được trong thời gian dài sử dụng Drupal.

Tiếp thị liên kết - Affiliate Marketing là gì?

Tiếp thị liên kết - Affiliate Marketing là gì?

Với kinh nghiệm của cá nhân, cũng như tham khảo một số người làm Affiliate khá nổi tiếng, Thủ Thuật Marketing giới thiệu với các bạn cơ bản về trường phái Marketing này.

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

 

Diet con trung