I need to act on a block insert/update/save/delete of other modules in a module (for e.g. filter out all URLs in a block and save the URLs in an extra table). This should work like hook_nodeapi(). Is there anything available that allows this or a workaround that gives me the content and title of a block on save so I can run a regex on it!?
Comments
Comment #1
hass commentedSolved this in the following way. Maybe someone else search for hook_blockapi and may find this helpful...
Comment #2
hass commentedComment #4
hass commentedMayor problem with above code is that
$bid = db_last_insert_id('boxes', 'bid');returns wrong values. See #369520: db_last_insert_id returns watchdog (wid) if comment (cid) is selectedComment #5
hass commentedComment #6
moonray commentedThis is actually a critical issue, and can even be considered a bug. With db_last_insert_id gone, there's no way to act on a newly inserted block without trying to load the block based on module and title (which is not unique); we don't have a delta or a bid assigned yet.
Comment #7
andypostI think it's enough to implement MYMODULE_preprocess_block()
Comment #8
jacineMYMODULE_preprocess_block() to act on a newly inserted block? I don't know if that is a reasonable approach or not, but it sounds very wrong.
Is this really going to be the recommended way modules deal with these events in D7?
Comment #9
moonray commentedAn alternate fix would be to inject the bid and delta (which will have been assigned to the $block object on insert) into the $form_state array? But that, again, seems more like a patch than a good API fix.
Comment #10
andypost@Jacine I think you should take a look on approach used in #684774: Block visibility settings cannot be properly extended
Also this is not critical issue
Comment #11
andypostAlso much better to use http://api.drupal.org/api/function/hook_form_FORM_ID_alter/7 but not all blocks have content at the add/edit stage
I think core have enought hook, alter, preprocess abilities to stop making api bigger
Comment #12
moonray commentedNow that delta is injected into $form_state (core update with #684774: Block visibility settings cannot be properly extended) modules can react on block inserts.
I'm still not sure if this is the ideal way of doing it, but it works.
Comment #13
hass commentedThis feature is to sync the not existing api with node and comment hooks. Form alter with submit hook cannot solve the problems for sure as you cannot be sure what bid you get. It could we a wrong bid and only the hook can provide the correct auto increment value to other modules for sure.
See linkchecker module...example in #1 is not 100% up to date... And the workaround i used IS NOT reliable.
Comment #14
andypost@hass all blocks are different, also they could be altered by other modules but using $module and $delta from $form_state you could make things easy.
If you are talking about custom blocks so it's much easy to use {block_custom}.bid which is equal to $form_state['values']['delta']
IMO, API already good:
http://api.drupal.org/api/function/block_add_block_form_submit/7
http://api.drupal.org/api/function/block_schema/7
Comment #15
jacine@andypost Thank you for the resources and information. It's much appreciated :)
Comment #16
hass commentedAndypost: no. You have no chance to reliable get the correct bid. You may need to dig deeper or try to implement a blocks module that fire on block save as one example. If you compare to node orcomment api you may get the reasons why the current solutions are nothing else like unreliable buggy hacks.
Comment #17
andypost@hass tell me why this could bring a wrong result?
Comment #18
hass commentedAha... Maybe something have changed in D7 I have not seen yet. But how does my module hook in here? I do not see that other modules are called before the block is saved (hook_prepare), after the block is saved (hook_insert) and what will happen on update or delete and how my module is hooked to delete other data from myblock module tables... Obove line looks like aform helper, but not like an api implementation that is working like the node api.
Comment #19
andypost@hass You should use hook_form_FORMID_alter() for block_add_block_form(), block_admin_configure() and block_custom_block_delete() to add your validation and submit handlers.
validation is like _presave and submit with $form->name is _insert _update _delete
Take into account that insert works only for custom (users' provided blocks) to hook into module provided blocks use hook_block_info_alter
Comment #20
hass commentedThis is the bad way used in D6 + I really hope we can get rid of workarounds like below. They are really bad. Additional it's more worse that there is a need to have two different logics to hook into block forms. Linkchecker need to hook into custom and normal blocks.
I'm for cleaning up this workarounds and make the DX and API much cleaner and better - nevertheless it may work - it's much more complex and unreliable than the node and comment hooks that are really easy to understand.
Comment #21
andypostAdd submit handler should be the same
Comment #22
andypostSuppose we could close this because commited [560746]
Hook into what? content (render) or config (form alter)
Comment #23
andypostAlso a bit related #735900: Deleting module's blocks when module is uninstalled
Comment #24
hass commentedAs said i need hook_block_*: insert, delete, update. There is nothing fixed here. Still open.
Comment #25
andypost@hass what do you mean with hook_block insert()? insert into {block} {block_custom} {block_roles} ?
Drupal way is _alter
Comment #26
hass commentedWhy is this so difficult to understand? Please try to read the public API documentation at these links. While you are reading, replace the words "hook_node_*" with "hook_block_*" and "node" with "block":
http://api.drupal.org/api/function/hook_node_insert/7
http://api.drupal.org/api/function/hook_node_delete/7
http://api.drupal.org/api/function/hook_node_update/7
Example results:
http://api.drupal.org/api/function/hook_block_insert/8
http://api.drupal.org/api/function/hook_block_delete/8
http://api.drupal.org/api/function/hook_block_update/8
You may get the idea. Moving to D8 as API changes need to wait for the next major version.
Comment #27
andypost@hass I talk about implementation not about concept. Some concept and planing goes http://groups.drupal.org/node/63708
But for now we need to fix #735900: Deleting module's blocks when module is uninstalled
Comment #28
grendzy commentedfixing tag
Comment #29
alan d. commentedStrangely, validation was not included here and this would be a more common requirement than insert/update hooks. I had a developer wtf moment after realizing that the refactored block system lacked this. Even the core system blocks use validation (unique block names), so why enforce another form alter to do? It would be as easy as this:
Comment #30
indytechcook commentedCross posting #430886: Make all blocks fieldable entities
With Custom blocks becoming entities and using the new entity class pattern, we will have access to hook_block_$op for custom blocks.
Does that cover it for d8?
Comment #31
traviscarden commentedRe-tagging.
Comment #34
tim.plunkettBlocks are now entities, see the hook_entity_* hooks.