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?
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'.
admin/structure/block/
.
/admin/structure/block
is not showing blocks as regular blocks but queries them as entities and prints every block found in database.
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.
@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.