0

I'm using Drupal 8.

I need to hide blocks of certain type from blocks UI because I'm managing them from my custom module and I'm not using them as a regular blocks.

Is there any way of altering block list generated on blocks administrative page?

1

You could try using hook_block_access to restrict access to block operations.

Control access to a block instance.

Modules may implement this hook if they want to have a say in whether or not a given user has access to perform a given operation on a block instance.

string $operation: The operation to be performed; for instance, 'view', 'create', 'delete', or 'update'.

  • I tried that but it hides block from actual region. I want to hide it from list of blocks on admin page admin/structure/block/. – otarza Jan 11 '16 at 10:37
  • Did you try preventing access to 'update' instead of 'view'? – David Thomas Jan 11 '16 at 23:02
  • I tried it for all operations, it didn't work as I expected, as I understand it wont work, because the form generated on /admin/structure/block is not showing blocks as regular blocks but queries them as entities and prints every block found in database. – otarza Jan 12 '16 at 12:52
1

Given that you're also asked Check if block is visible, I kind of know what you're trying to do.

This isn't really an answer to your question, but I'm wondering if you're on the wrong track with what you are doing.

I don't fully understand why you are using blocks to do what you want in the first place. But assuming that using block plugins is the right thing, block config entities might not be.*

Block plugins are very flexible and designed to be re-usable, e.g. in page_manager/panels, in a completely different way. Block config entity however are not meant to be re-usable like this at all. They have a very specific purpose. To be displayed in a theme region of a specific theme, based on a set of visibility conditions. Trying to (mis-) use them for a different purpose will lead you into corners like this one.

You might want to consider storing block configuration in your own entity type. You could extend from block to get most of its logic but have your own list page to manage them, and then they would be a completely different thing than normal blocks for Drupal and you don't have to worry about them being used/showing up in places you don't expect. For example, even if you hide them in the UI, the block system will still prepare any accessible block and make them available in the configured theme region.

  • And since your code example seems to hardcode a specific block plugin, maybe you don't want to use block plugins at all?
  • Thank you very much Berdir, your answers gives me correct directions. The only reason I did it this way was that I could use Block's built in visibility settings for our functionality. I thought this could be a simpler approach and saved us a lot of time. I didn't think of reusing the blocks the way you proposed. Our module is almost completed now but we will think to refactor it as you propose. Thanks again. – otarza Jan 12 '16 at 11:06
0

@David Thomas's answer might be correct if you want to hide blocks from actual regions.

But, if you want to hide blocks from Block UI listing /admin/structure/block than you have to alter block_admin_display_form using hook_form_alter()

Here is what I did for my case:

function MY_MODULE_NAME_form_alter(
  &$form,
  \Drupal\Core\Form\FormStateInterface $form_state,
  $form_id
) {
  if ($form_id == "block_admin_display_form") {
    foreach ($form['blocks'] as $key => $block) {
      if (!empty($block['operations']['#links']['edit']['url'])) {
        $block_plugin_id = $block['operations']['#links']['edit']['url']->getOptions()['entity']->getPluginId();
        if ($block_plugin_id == 'MY_CUSTOM_BLOCK_PLUGIN_ID') {
          unset($form['blocks'][$key]);
        }
      }
    }
  }
}

This is the only way I could found by far. If anyone has better solution please share.

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.