Skip to main content

Design patterns in Drupal

Types of design pattern

design patterns that can be broadly categorized into three types.

1. Object oriented program pattern
2. Dependency Injection pattern
3. Factory design pattern
4. Singleton design pattern
5. Plugin Pattern

Types of design pattern

design patterns that can be broadly categorized into three types.

1. Object oriented program pattern
2. Dependency Injection pattern
3. Factory design pattern
4. Singleton design pattern
5. Plugin Pattern
Plugins Pattern in Drupal can be used for various purposes such as blocks, fields, form elements, and more.
For example, let's say you have a module called "custom_module" in Drupal that needs to add a custom block with specific functionality. You can implement the Plugin pattern in Drupal by defining a block plugin in your module
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a custom block for custom_module.
 *
 * @Block(
 *   id = "mymodule_custom_block",
 *   admin_label = @Translation("Custom Block"),
 * )
 */
class CustomBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    // Build the block content.
    $output = '';
    // ...
    return [
      '#markup' => $output,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    // Implement block configuration form.
    // ...
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    // Process block configuration form submission.
    // ...
  }
}

Add new comment

Restricted HTML

  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].
  • You can use shortcode for block builder module. You can visit admin/structure/gavias_blockbuilder and get shortcode, sample [gbb name="page_home_1"].