Drupal Development and Consultation - Proudly operating in The United States of America

Drupal 10 Custom Block (Programmatically)

By Xandermar LLC, April 30, 2024

Below is a simple way to create a module that will produce a custom block.

  1. Set up your module directory structure: Create a directory for your custom module in the modules/custom directory of your Drupal installation. Let's name our module "custom_block_example". So, you'll create a directory named custom_block_example.
  2. Create the .info.yml file: Inside the custom_block_example directory, create a file named custom_block_example.info.yml. This file will contain metadata about your module.

    name: 'Custom Block Example'
                    type: module
                    description: 'Creates a custom block example.'
                    package: 'Custom'
                    version: 1.0
                    core_version_requirement: ^10
                    
  3. Create the .module file: Inside the custom_block_example directory, create a file named custom_block_example.module. This file will contain PHP code that implements various aspects of your module.
  4. Define the block class: Inside the .module file, define a class for your custom block.

    <?php
                    namespace Drupal\custom_block_example\Plugin\Block;
                    use Drupal\Core\Block\BlockBase;
                    /**
                     * Provides a 'Custom Block Example' block.
                     *
                     * @Block(
                     *   id = "custom_block_example",
                     *   admin_label = @Translation("Custom Block Example"),
                     * )
                     */
                    class CustomBlockExample extends BlockBase {
                      /**
                       * {@inheritdoc}
                       */
                      public function build() {
                        // You can put any content or logic here to render in the block.
                        return [
                          '#markup' => $this->t('Hello, this is a custom block!'),
                        ];
                      }
                    }
                    
  5. Enable your module: After you've created the module files, you need to enable the module in Drupal. You can do this through the Drupal admin interface by navigating to Extend (/admin/modules) and finding your module in the list. Check the box next to your module's name and click the "Install" button.
  6. Place the block: Once your module is enabled, you can place the custom block wherever you want on your site by navigating to the block layout configuration page (/admin/structure/block). Look for your block in the list of available blocks and place it in a region of your choice.

That's it! You've created a custom module that creates a custom block in Drupal 10. You can further customize the block's appearance and functionality by modifying the block class and the build() method according to your requirements.

Tags

Comments