3

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;

}
  • Unclear what you're asking. Where are the fields added? Into a content type? And you want the block to display the fields from the content type? Please expand/clarify your question so that someone who has no idea what you're trying to achieve can understand your issue. – Beebee Oct 28 '15 at 11:52
  • @Beebee Just added a bit more clarification and some examples as well. – jagmitg Oct 28 '15 at 11:59
  • It sounds like blocks aren't really suitable for what you're trying to achieve. Have you looked at custom ctools content types a.k.a custom panel panes? They can have multiple instances and can be configured separately. The reason you're getting uniform values is because you're using variables with variable_get and variable_set. Everytime someone saves your block config, that variable will get overwritten by new value. – Beebee Oct 28 '15 at 13:22
  • 1
    This is a decent tutorial on custom ctools content type, explaining all the hooks/code that you need to implement. In a sense it's very similar to hook_block_info/view/configure/save – Beebee Oct 28 '15 at 13:24
0

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")));
  • can you please explain the process - also i have created the block through a separate module - does $module_name automatically bring that back? – jagmitg Oct 28 '15 at 13:01
  • yes. lets a your module name is views , place your module name there. The best way is to go admin-> structure -> block and click configure on your block and place the value from there for instance. admin/structure/block/manage/views/locations-block_1/configure for this path module name is views and id is location-block_1 – Shabir A. Oct 28 '15 at 13:07
  • @JSG added more clarification – Shabir A. Oct 28 '15 at 13:11
  • Thanks for the clarification - i meant more about what does this exact function do? because the module file i created generates the block (so i have the module name and the block itself) which is in a separate view function. Now this generates everything that needs to go inside that specific block. However, the problem is that one block cannot be used multiple times with their custom fields. So what would be an answer for that? – jagmitg Oct 28 '15 at 13:19
  • Can you paste your code snippent. I think your requirements are still not clear. You would like to render one block in multiple regions right? or you would like to have different block. if you share your block code it would be helpful – Shabir A. Oct 28 '15 at 13:22
0

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.

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.