This question is a continuation of this Block in Drupal 8, The second problem I am facing is that suppose I select block made for comment section and place it under Sidebar second section and afterwards deselect comment from configuration setting then after clearing cache the label for comment from block section is removed but block placed under Sidebar section still remains with default message for broken blocks "This block is broken or missing. You may be missing content or you might need to enable the original module.".What changes should I make so that this message is removed ? Thanks.

1 Answer 1


As mentioned in the other question, what you need to do is query for the blocks that no longer exist and delete them.

BlockContent::delete()/getInstances() is an example for that. You can do a similar query, either by using entity_load_by_properties() for the plugin ID that those blocks would have. Or you can run an entity query, which has more options.

You don't have an entity, so in your submit, you need to build a list of all unchecked options and then query for those. The advantage of doing an entity query is that you can do an IN condition, so you probably want to use that.

A more complicated example, that looks at settings and either updates or deletes the affected blocks can be found in simplenews.module's Newsletter::postDelete():

if (\Drupal::moduleHandler()->moduleExists('block')) {
  // Make sure there are no active blocks for these newsletters.
  $ids = \Drupal::entityQuery('block')
    ->condition('plugin', 'simplenews_subscription_block')
    ->condition('settings.newsletters.*', array_keys($entities), 'IN')
  if ($ids) {
    $blocks = Block::loadMultiple($ids);
    foreach ($blocks as $block) {
      $settings = $block->get('settings');
      foreach ($entities as $newsletter) {
        if (in_array($newsletter->id(), $settings['newsletters'])) {
          unset($settings['newsletters'][array_search($newsletter->id(), $settings['newsletters'])]);
      // If there are no enabled newsletters left, delete the block.
      if (empty($settings['newsletters'])) {
      else {
        // otherwise, update the settings and save.
        $block->set('settings', $settings);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.