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

 
6 Drupal SEO Modules Required for Superior Search Ranking Performance

6 Drupal SEO Modules cần thiết cho hiệu suất xếp hạng website

If you are running your site with Drupal, you are reaping the numerous benefits this publishing platform has to offer.

Packt công bố kết quả cuộc bầu chọn CMS nguồn mở tốt nhất

Packt công bố kết quả cuộc bầu chọn CMS nguồn mở tốt nhất

Nhà xuất bản Packt vừa công bố các giải thưởng bầu chọn CMS nguồn mở tốt nhất trong năm 2011.

Cuộc chiến iOS và Android - Ai sẽ giành chiến thắng ?

Cuộc chiến iOS và Android - Ai sẽ giành chiến thắng ?

Ai sẽ giành chiến thắng trong cuộc chiến hệ điều hành cho điện thoại: Apple iOS hay là Google Android ? Đây là câu hỏi ám ảnh đối với những người trong giới công nghệ trong suốt thời gian qua...

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

 

Diet con trung