Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By pratip.ghosh on
Hello,
Just wanted to know if it's possible to create a custom block in Drupal 7 without using the block hooks hook_block_info() or hook_block_configure() etc.
My requirement is adding dynamic blocks to the Drupal system.
I am a newbie in Drupal, so please excuse me if this is sort of a very basic question and discussed previously.
Thanks in advance...
Comments
There is one options in the drupal admin section..
There is one options in the drupal admin section...
1. Go to the path sitename/?q=admin/settings/block
2. The is one menu called 'add new block' , click on that.
3. It is very user frinendly , check it .
Have a great day...
I meant programmatically
Thanks, but i wanted to create a custom block programmatically, i.e. by using code.
I hope i make myself clear here...
-- Pratip Ghosh
_
i'm not sure what you're asking-- 'block' is a drupal concept and they are created with the hook_block_* functions. If you don't want to use those functions then, by definition, you're not creating a block. Just put a snippet of code wherever you want.
I hope you are not familiar with drupal ....
I hope you are not familiar with drupal ...
In the above mentioned method , you can insert your own code like php/html etc.... Other wise you might use hook_block...
Try this modules too , i hope what you are searching it is here ...
http://drupal.org/project/nodeblock
http://drupal.org/project/nodeasblock
You can create a block as node ...
Hope you can understand....
Explanation
I think either i am not understanding or i am not able to explain my problem. Let me try again:
This is how a block is created programmatically if i am not wrong. But this functions will be called internally from the system whenever i load this module, i.e. without any user interaction.
What i want is to write function(s) that create blocks within Drupal system only when i call them. I will pass the block properties (title, content etc.) as parameter while calling these function(s).
Was that helpful?
-- Pratip Ghosh
Are you looking to make a
Are you looking to make a block based on a condition? or are you looking to do something like loop through a bunch of records and output a new block for a new record.
Both of which are possible using any hook that is called outside of a .install file, as its run every time the hook is called, rather than being a one off. (as far as I know).
There shouldn't be any reason you cant dynamically build new blocks and configurations for those blocks inside the hooks, so long as it returns an array in the right format.
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
Correct
Right, i am trying to create a block when i wish, calling a function like that of the block_hook ones based on parameters like block title, description etc. The block should only be created when i call those function(s) and not like the block_hook.
Does this clear things up?
-- Pratip Ghosh
i think so yes,unfortunately
i think so yes,
unfortunately so far as I know, its not reasonably possible to do what you want, at least not without tearing Drupal apart, but I feel you might be misunderstanding a little of how blocks work.
I'm assuming you want to create a new block, every time, say, a form is filled in? you could then in theory have a foreach() in hook block that will create a block every time there is a new record in the DB, or you could use a while() or any other number of standard PHP functions.
When a block is created its not automatically put somewhere on the site (unless you have specified that in the code), it goes into your block list where you can then do any number of things with it (configure, edit, assign to regions etc).
Under what conditions are you trying to create the block?
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
You got it
Yes, you understood my requirement. If more explanations are needed you can view that at http://drupal.org/node/1709954#comment-6306220
If it's not a big trouble for you, can you kindly provide the code within the foreach/while loop that you were talking about from where a block is created dynamically.
And for the last part of your reply, yes i know that the block thus created would not be assigned to any region. But that's ok for now with me. I just want to create the block with it's title and description.
Thanks a lot to all of you for your co-operation...
-- Pratip Ghosh
Ok, so;this can be done with
Ok, so;
this can be done with what you have above, with a few changes.
The below code is a very basic take on a way you -could- output the result of a form submission as dynamically created blocks. This code is Drupal 7 Specific
you could have multiple foreach() statements, so long as the returned array is built as drupal expects.
Side note: i wrote the above in the reply box, so if there are any syntax errors with it i apologise. its designed as an example rather than functioning code.
N1ght
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
this code would create a
this code would create a block for each form submission saved into a DB table. you could add extra processing to search for a certain piece of data within the row, for example a location, and you could do something like this inside the foreach:
In that way you could create dynamic blocks based on form submissions and certain perimeters. (for example a hidden field in a form or a check list)
N1ght
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
Thanks
Thank you very much for the idea. I figure, that if I HAVE to do it by calling the hook, then it's better to store the block infos in a temporary table, and when the hook gets fired, it reads through this table, fetches records waiting to be inserted in the Drupal block table, creates the block and thereafter deletes the record from the table.
Thanks to all of you for your support...
Cheers!!!
-- Pratip Ghosh
Just a note on the above
Just a note on the above :)
The block only exists while the hook is calling it. if the record is removed form the db, then the block will go too :)
But if thats the aim here, then your right thats by far the best solution for you :)
N1ght
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
Actually i will be deleting
Actually i will be deleting the record from the self-created temporary table and not from the Drupal system block table.
Thanks...
-- Pratip Ghosh
ah, yea ok :) early morning
ah, yea ok :)
early morning and all >.<, that makes more sense :D
sorry for the confusion there :) and glad i could help with a workable solution
RTFM!
For further support or projects contact me.
Source Control - Web Design, Development and Hosting Oxfordshire, UK
I hope you are not familiar with drupal ...
I hope you are not familiar with drupal ...
In the above mentioned method , you can insert your own code like php/html etc.... Other wise you might use hook_block...
Try this modules too , i hope what you are searching it is here ...
http://drupal.org/project/nodeblock
http://drupal.org/project/nodeasblock
You can create a block as node ...
Otherwise you can create the block by using your own function with db_query and variables by using the table 'block_custom', But you might take care on creating this by your own code , it may effect the drupal normal functionality ...
Check this code
Hope you can understand....
Without direct DB interaction?
Thanks a lot for your help.
I considered the option of creating a new block directly in the DB level. Thanks anyways. This i would keep as my last option. But was just wondering that is'nt there something in drupal (like tweaking/replicating the block_hook a little, so as to call when needed) that does it without this writing DB level code?
Again i may be severely wrong approached, but something tells me it's possible, because i think this is a very obvious requirement that a developer may often face.
Thanks a lot again to all of you for your prompt replies...
-- Pratip Ghosh
Always welcome...
Always welcome... This code work only in drupal 7
Just for clarification
This chunk of code has been grabbed from here:
https://api.drupal.org/api/drupal/modules!block!block.admin.inc/function...
It's part of Block Api.
Before posting "copy-past" code I think it should be useful to provide a CORRECT and FULLY CLEAR documentation in order to avoid loss of time for newbies to search (maybe not so) obvious information!
I really can't see the reason to truncate the function name from the Api function code above.
Your question is totally understandable.
Hey Pratip,
I know this question is 2 years old but the answer is quite simple.
You just programmatically submit the block add form, passing to it the data you want.
Can't set format
@leex: thank for your great answer!
For some reason I can't set format to anything but "plain_text". If I try to set it to either "full_html" or "filtered_html" I get the following error:
WD form: Illegal choice filtered_html in Text format element. An illegal choice has been detected. Please contact the site administrator.
I do have both formats enabled, and if I test the input using dpm($form_state) in hook_form_alter the format is set to "full_html". Weird?
Remove format value under body while block create.
Programmatically updates a DB level block
Also if anyone wants to update the created block programatically:
Thank You,
Shashwat Purav