TransWikia.com

How have multiple instances of a Custom Block?

Drupal Answers Asked by jagmitg on December 10, 2021

So I have a custom block that has been created within the module. I have used the drupal API to generate the brand new fields – which works perfectly.

However, I am having an issue regarding multiple instances of the custom block. As I want the blocks to be sandboxed as the custom fields are only related to the block used within the one region.

Now using Multi Block, I can create another instance of that block but the field is exactly the same. What would be the best approach for having one block but whenever its used, its custom fields per block.

case 'live_chat_block' :
    variable_set('text_variable', $edit['text_body']);
break;

Now the idea I had was to add in the “panel page” as variable ID but there might be a better way. Any suggestions would be useful.


Just to expand on what i would like to achieve:

We currently have panel pages implemented and we have built a custom block with custom fields inside that custom block. The requirement is that every block that we drag into one of the panel pages, we would like for them to have unique fields.

Panel Page: 1
Block ID: 123
Custom Field Title: Superman

Panel Page 2:
Block ID: 123
Custom Field Title: Batman

Now as you see, the only thing that changes is the panel pages, the block stay the same and the custom fields need to be unique to that panel page and not the block.

Currently, when changing one block, it impacts on all the other blocks as well.

function livechat_block_configure($delta=''){
  $form = array();

  switch($delta) {
     case 'live_chat_block' :
        // checkbox example:
      $form['text_body'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter in Title here'),
        '#default_value' => variable_get('text_variable', ''),
      );


        break;
  }
  return $form;

}

2 Answers

I would look at using Bean and Bean Panels to achieve what you're looking to do. Drupal 7's blocks were pretty minimalistic in terms of handling fields for instances. Bean was designed to address this (and was the basis for the much improved Drupal 8 custom blocks).

Another approach would be to put the custom content in fields on your nodes, and have views load that information using a contextual filter.

Answered by acrosman on December 10, 2021

Add the following function in your template.php

function <themeName>_render_block($module, $delta, array $options = array()){
  $default_options = array(
    'title' => '<none>',
    'region' => -1,
  );

  $options = array_merge($default_options, $options);

  $block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta', array(':module' => $module, ':delta' => $delta))->fetchObject();
  if($block) {
    $block = block_load($module, $delta);
    $block->title = $options['title'];
    $block->region = $options['region'];
    $blocks = array($block);
    $rblocks = _block_render_blocks($blocks);
    $render_array = _block_get_renderable_array($rblocks);
    return drupal_render($render_array);
  }
  return "";
}

then when you want to render the block just write the following line

print render <themeName>_render_block($module_name, <id of block>, array of options like title etc)

You can find module name and block id by going to block configuration page so for the path admin/structure/block/manage/views/locations-block_1/configure
module name is "views" and id is "location-block_1"

So the render function will be

print render <themeName>_render_block("views", "location-block_1", array("title" => t("My block title")));

Answered by Shabir A. on December 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP