例として「Powered by Drupal」ブロックを使用します。
Drupal 7
modules / system / system.module
/**
* Implements hook_block_info().
*/
function system_block_info() {
$blocks['powered-by'] = array(
'info' => t('Powered by Drupal'),
'weight' => '10',
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function system_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'powered-by':
$block['subject'] = NULL;
$block['content'] = theme('system_powered_by');
return $block;
}
}
Drupal 8
core / modules / system / src / Plugin / Block / SystemPoweredByBlock.php
<?php
/**
* @file
* Contains \Drupal\system\Plugin\Block\SystemPoweredByBlock.
*/
namespace Drupal\system\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Powered by Drupal' block.
*
* @Plugin(
* id = "system_powered_by_block",
* admin_label = @Translation("Powered by Drupal"),
* module = "system"
* )
*/
class SystemPoweredByBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#children' => theme('system_powered_by'),
);
}
}
hook_block_info()
置換されたアノテーション内に必要なメタデータのすべてを含む@Plugin
のdocblock部分。メソッドにhook_block_view()
置き換えられBlockPluginInterface::build()
ます。
ここでは、ディレクトリ構造が重要です。すべてのブロックは、モジュールフォルダー内の特定のディレクトリ(src / Plugin / Block)に配置する必要があります。