0

I have a custom module where its only purpose is going to be serving up a block. I basically used these instructions: https://www.drupal.org/docs/creating-custom-modules/creating-custom-blocks/create-a-custom-block

These are my files' content.

modules/cat_custom/cat_custom.info.yml

name: Cat Custom
type: module
description: Custom functionality for Cat Custom
core_version_requirement: ^9
dependencies:
  - drupal:block

modules/cat_custom/src/Plugin/Block/CatCounterBlock.php

namespace Drupal\cat_custom\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'Cat Counter' Block.
 *
 * @Block(
 *   id = "cat_counter_block",
 *   admin_label = @Translation("Cat Counter Block"),
 * )
 */
class CatCounterBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#markup' => $this->t('Hello, World!'),
    ];
  }
}

When I first enable the module, I can see the block in the Add block list. When I run drush cr, it disappears from that list. If I visit /admin/config/development/performance and click on Clear all caches, it re-appears. As soon as I run drush cr, it disappears again.

If I place it when it's showing up, it works as expected initially. When I run drush cr, the block's text gets replaced with the following message.

This block is broken or missing. You may be missing content or you might need to enable the original module.

I enabled and tried the block example from https://www.drupal.org/project/examples; it works as expected, even after a drush cr.

What am I missing or doing wrong?

3
  • 2
    Compare the status page in admin UI with drush status. Are both using the same PHP version? Are you using the latest Drush version installed via composer in the vendor directory?
    – 4uk4
    Sep 20, 2022 at 7:02
  • Adding to 4uk4, also check the file ownership and access rights. Does your command line user have access to those files? Sep 20, 2022 at 7:52
  • Drush info: PHP: php7.4 Script: /drupal/vendor/drush/drush/drush Version: 10.3.6 Status page: PHP: 7.4.30 For permissions, it looked like everything's read/writable by my user and the webserver except the custom modules I added. I made those writable by webserver too just in case. No change: clearing cache in UI lets it work properly, while drush cr breaks it. I'm not sure why none of the other modules with blocks get similarly broken. Drupal recent log messages says: The "cat_counter_block" was not found.
    – Prizem
    Sep 21, 2022 at 21:54

1 Answer 1

0

I went into the database and did a TRUNCATE on all tables with prefix cache_. That's seemed to fix the problem for me. I can clear all caches in the Performance page and use drush cr without issue.

One thing I noted while trying things was that after removing and uninstalling my custom module, if I moved it in the directory structure and re-installed it, it would give errors when trying to place the block again. It seems like something gets cached and it continues looking for the original reference.

For example, moving it from /modules/cat_custom to /modules/custom/cat_custom or /sites/mysite.com/modules/cat_custom would lead to errors when placing the block again. These errors are viewable in watchdog and just show that it's looking in the wrong directory.

I'm not exactly sure what the problem was, but hope truncating cache tables helps someone else!

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.