Is there a way to get the number of blocks, of a specific type, used on a page?

Permalink
I am making a block that uses JavaScript to inject content into an element. The element ID will be the JavaScript hook. To allow for multiple blocks per page, each element requires a unique ID.

Is there a method that does something like "blocktype_count_on_page()" that returns the number of uses of a specific block for a page? If it returned 3, then I could increment the ID "custom-feed04" etc.

One approach I was thinking of was to assign the ID a random number. When the block is created, it assigns the random number to the ID (custom-feed435235). Each time the block is edited, instead of reassigning a random number, it checks to see if the number was already set. I think this would work, but there is the real chance of a duplicate ID being created - causing the block to fail (no error, just won't display). In the block documentation I could include how the ID was being generated, and that if adding an additional block failed, removing it and adding it a second time would work.

I am open to any other ideas for a solution.


Thank you

MrKDilkington
 
jordif replied on at Permalink Best Answer Reply
jordif
Hi,

not sure I understand, but can't you use the $bID variable? That variable already contains a unique id for every block, ready to use in you view.php.

Regards,

Jordi
MrKDilkington replied on at Permalink Reply
MrKDilkington
Thank you, Jordi.

I feel silly that I didn't think of something so obvious.
JohntheFish replied on at Permalink Reply
JohntheFish
Take care, because bID is not unique if a block is copied and pasted without further edit, or inherited from page defaults.

See this howto for a 5.6 solution
http://www.concrete5.org/documentation/how-tos/developers/obtain-a-...

I don't know if the solution is identical for 5.7
MrKDilkington replied on at Permalink Reply
MrKDilkington
I bookmarked that page.

Thank you, John