2

I have multiple blocks I'd like to include in a module/install profile; (containing markup) but need to be editable through the standard block UI? What's the best way to implement this?

2

Drupal adds custom blocks in a form submit function using database queries directly, it doesn't expose an API for that particular function unfortunately, so you'll have to write/copy the code manually.

You can find all the code you need in block_add_block_form_submit().

You insert the custom block into the database like so:

$delta = db_insert('block_custom')
  ->fields(array(
    'body' => '<p>Some markup</p>',
    'info' => 'Block description',
    'format' => 'full_html',
  ))
  ->execute();

Then you need to make this block available to all installed themes:

$query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
foreach (list_themes() as $key => $theme) {
  if ($theme->status) {
    $query->values(array(
      'visibility' => 1, // visible
      'pages' => '', // list of pages 
      'custom' => 1, // This is a custom block
      'title' => 'Block title',
      'module' => 'block', // The block module handles custom modules
      'theme' => $theme->name,
      'status' => 0,
      'weight' => 0,
      'delta' => $delta,
      'cache' => DRUPAL_NO_CACHE,
    ));
  }
}
$query->execute();

You'll want to put that code either in hook_install()/hook_enable() if you plan to do this in a custom module, or hook_install() if you're doing it in an install profile.

0

Have a look at block_example.module, there it goes like so:

/**
 * Implements hook_block_info().
 *   Hello Drupal, here is a new block.
 */
function MYMODULE_block_info() {
  $blocks = array();
  $blocks['configurable_block'] = array(
    'info' => t('My Block\'s name'),
    // Setting the following two values will enable this block on ALL themes.
    // Better place this block manually after module activation. Alternatively
    // you could db_update the theme value programmatically, of course.
    //'status' => TRUE,
    //'region' => 'content',
    'visibility' => BLOCK_VISIBILITY_LISTED,
    'pages' => '<front>',
  );
  return $blocks;
}

/**
 * Implements hook_block_configure().
 *   To create a configurable block we need to add a form to our block.
 */
function MYMODULE_block_configure($delta = '') {
  $form = array();
  if ($delta == 'configurable_block') {
    $form['MYMODULE_block_content'] = array(
      '#type' => 'textfield',
      '#title' => t('Block contents'),
      '#size' => 60,
      '#description' => t('This text will appear in the example block.'),
      '#default_value' => variable_get('MYMODULE_block_content', t('Some example content.')),
    );
  }
  return $form;
}

/**
 * Implements hook_block_save().
 *   Whenever the block content is edited, it gets stored into the DB.
 */
function MYMODULE_block_save($delta = '', $edit = array()) {
  if ($delta == 'configurable_block') {
    variable_set('MYMODULE_block_content', $edit['MYMODULE_block_content']);
  }
}

/**
 * Implements hook_block_view().
 *   Tells Drupal what it should put into the block's content.
 */
function MYMODULE_block_view($delta = '') {
  switch ($delta) {
    case 'configurable_block':
      $block['subject'] = '';
      $block['content'] = MYMODULE_block_content($delta);
      break;
  }
  return $block;
}

/**
 * A module-defined block content function.
 */
function MYMODULE_block_content($which_block) {
  switch ($which_block) {
    case 'configurable_block':
      $result = array(
        '#markup' => variable_get('MYMODULE_block_content',
          t('A default value. This block was created at %time',
            array('%time' => date('c'))
          )
        ),
      );
      return $result;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.