July 06, 2014

In this article, we are going to discuss about How to create custom blocks with custom fields in Drupal 7. In Drupal 7, we can create custom blocks with custom fields using hook_block_configure() and hook_block_save() hooks.

hook_block_configure() - allows you to set editable custom fields on your custom block.

hook_block_save() - allows you to save the contents on each custom fields edited by the user.

On your custom module file ( e.g. my_custom_block.module), we first setup hook_block_info() and hook_block_view().

/**
 * Implements hook_block_info().
 */
function my_custom_block_block_info() {
  $blocks['custom_block_with_custom_fields'] = array(
    'info' => t('My custom block with custom fields'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function my_custom_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $block['title'] = 'My custom block with custom fields';
      $block['content'] =  my_custom_block_contents($delta),
      break;
  }
  return $block;
}

my_custom_block_contents($delta) is a custom callback that we will be using for displaying the contents.

Now we are going to implement the hook_block_configure() for our custom fields.

/**
 * Implements hook_block_configure().
 */
function my_custom_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $form['custom_textfield'] = array(
        '#type' => 'textfield',
        '#title' => t('Textfield input'),
        '#size' => 60,
      );
      $form['custom_text_format'] = array(
        '#type' => 'text_format',
        '#title' => t('Text format input'),
      );
      break;
  }
  return $form;
}

In creating custom fields, we still follow Drupal's Form API

We now implement hook_block_save() to allow Drupal to save our custom field values when a user edits our custom field using variable_set api.

/**
 * Implements hook_block_save().
 */
function my_custom_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'custom_block_with_custom_fields':   
      variable_set('custom_textfield', $edit['custom_textfield']);
      variable_set('custom_text_format', $edit['custom_text_format']['value']);
      break;
  }
}

Now that we've implemented variable_set on each custom fields to save values, we can now use variable_get api on our custom callback, to display the values that we've saved on each custom field

/**
 * Returns block contents
 */
function my_custom_block_contents($block_id) {
  $block = array();
  switch ($block_id) {
    case 'custom_block_with_custom_fields':
      $block = array(
        'custom_textfield' => variable_get('custom_textfield'),
        'custom_text_format' => variable_get('custom_text_format'),
      );
      break;
  }
  return $block;
}

So, all of the snippets combined, would look like this.

/**
 * Implements hook_block_info().
 */
function my_custom_block_block_info() {
  $blocks['custom_block_with_custom_fields'] = array(
    'info' => t('My custom block with custom fields'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function my_custom_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $block['title'] = 'My custom block with custom fields';
      $block['content'] =  my_custom_block_block_contents($delta),
      break;
  }
  return $block;
}

/**
 * Implements hook_block_configure().
 */
function my_custom_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'custom_block_with_custom_fields':
      $form['custom_textfield'] = array(
        '#type' => 'textfield',
        '#title' => t('Textfield input'),
        '#size' => 60,
      );
      $form['custom_text_format'] = array(
        '#type' => 'text_format',
        '#title' => t('Text format input'),
      );
      break;
  }
  return $form;
}

/**
 * Implements hook_block_save().
 */
functionmy_custom_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'custom_block_with_custom_fields':   
      variable_set('custom_textfield', $edit['custom_textfield']);
      variable_set('custom_text_format', $edit['custom_text_format']['value']);
      break;
  }
}
/**
 * Returns block contents
 */
function my_custom_block_block_contents($block_id) {
  $block = array();
  switch ($block_id) {
    case 'custom_block_with_custom_fields':
      $block = array(
        'custom_textfield' => variable_get('custom_textfield'),
        'custom_text_format' => variable_get('custom_text_format'),
      );
      break;
  }
  return $block;
}

That's it! now go to your custom block and edit it, you will be able to see the custom fields we've created and at the same time be able to save the value we input on each field.

0 comments:

Post a Comment