Blocks are plugin in Drupal-8, I need to add a form element in the block add form & all the block edit forms. I am able to add the new form element in the form by form alter. But i need to get the block id on submit(custom submit handler). For block add forms block id has to be new block created & for block edit form, the existing block id.

How can i get the block id?


Have a lookt at Extend Block Class and my answer there about the block_class module.

It does exactly the same to add configurable CSS classes to blocks.

  • 1
    Block class is allowing to add classes to all the existing block, but it is not adding class field to new custom block plugins. I also need to display my form element while adding as well as editing custom blocks.
    – neha
    Nov 29 '16 at 8:32
  • 3
    It supports those too. Creating custom blocks is a two-step process, you first create the block and then put it in a region, on that step you can also configure the same settings as with all other blocks.
    – Berdir
    Nov 29 '16 at 21:04

Place this cistom block(php code) to seven theme under Breadcrumbs for page /admin/structure/block/manage/*

* Cistom admin block for get number block numberID on edit entity.block.edit_form
if( \Drupal::routeMatch()->getRouteName() == 'entity.block.edit_form' ) {
    $blockMachineName = \Drupal::token()->replace('[current-page:url:args:last]');
    $blockLoad = \Drupal\block\Entity\Block::load($blockMachineName); //dump($blockLoad);
    if($blockLoad) { //dump($blockLoad->get('id'));
        $blockIdStr = str_ireplace('block_content:', '', $blockLoad->get('settings')['id']); //print "blockIdStr: {$blockIdStr}";
        $query = \Drupal::database()->select('block_content', 'bc')->fields('bc', ['id'])->condition('bc.uuid', $blockIdStr);
        $blockIdNum = $result = $query->execute()->fetchField(); //print " blockIdNum: {$blockIdNum}"; //drupal_set_message("id: {$blockIdNum}");
        print "<b>BlockIDs</b> Str: {$blockIdStr} Num: {$blockIdNum}";

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.