Skip to content

How to add a block to an App

baisong edited this page Mar 7, 2013 · 4 revisions

Sometimes, you might want your custom App to contain a single instance of a block, pre-created when the user enables your app.

Note: blocks != boxes

A box is a kind of "block factory", that allows a site to create many instances of a single type. In contrast, a each block you add following these instructions will provide a single, pre-created instance in each vsite's available widgets layout page in CP > Build > Layout.

1. Provide a hook_block_info() in your .module file

<?php
/**
 * Implements hook_block_info().
 */
function mymodule_block_info() {
  $blocks = array();

  $blocks['myblock'] = array(
    'info' => t('My block'),
    'weight' => '-10',
  );

  return $blocks;
}

2. Provide a hook_block_view() in your .module file

<?php
/**
 * Implements hook_block_view().
 */
function  mymodule_block_view($delta) {
  $block = array();

  switch ($delta) {
    case 'myblock':
      $block['content'] = mymodule_myblock_block_content();
      break;
  }

  return $block;
}

3. Provide a hook_os_widget() in your .module file

<?php
/**
 * Implements hook_os_widget().
 */
function mymodule_os_widget() {
  $widgets = array();

  $widgets['mymodule-myblock'] = array(
    'module' => 'mymodule',
    'delta' => 'myblock',
    'title' => '',
    'info' => t('My block'),
  );

  return $widgets;
}

That's it! Once your app is enabled on this vsite, the block will become available in the Layout.

See also

Clone this wiki locally