Create a popup form in Drupal 8

  1. Add a route record into /modules/my_module/my_module.routing.yml
    • This route indicated the function openForm of openFormController will be executed after you goto the url of http://yourdomain/openForm through browser.
      my_module.openFormController:
        path: 'openForm'
        defaults:
          _controller: '\Drupal\my_module\Controller\openFormController::openForm'
          _title: 'Open Form'
        requirements:
         _permission: 'access content'
      

Programmatically create a custom block in Drupal 8

  1. It seems that we cannot create a custom block in existing modules on drupal 8. So you must create a new custom module first.
    • create a .info.yml file like so: /modules/custom_blocks/custom_blocks.info.yml
      name: All My Custom Blocks
      type: module
      description: Defines a custom block.
      core: 8.x
      package: Custom
      dependencies:
        - block
  2. Create the following file /modules/custom_blocks/src/Plugin/Block/MyFirstBlock.php

How to add custom block region on drupal 8

  1. Open YourThemeName.info.yml file exists in /core/themes/YourThemeName/
    • Add the string "header1 : Header1" under the keyword regions:
      name: YourThemeName
      type: theme
      description: 'YourTheme'
      package: Core
      base theme: classy
      version: '1.0.0'
      core: '8.x'
      project: 'drupal'
      datestamp: 1502903957
      libraries:
      regions:
        content: Content
        header: Header
        headline: Headline
        primary_menu: 'Primary menu'
        secondary_menu: 'Secondary menu'
        footer: Footer
        highlighted: Highlighted

Page redirect after form submit on drupal 8

  1. Find the route name and variable of the page that you want to do redirection on "modules/yourModule/yourModule.routing.yml"
    User.routename://<--route name
    path: 'your_page/{var1}/{var2}'//<--variables
    defaults:
       _controller: '\Drupal\yourModule\Controller\pageController::getPage'
       _title: 'My next page'
  2. Add this script on the function submitForm.
    public function submitForm(array &$form, FormStateInterface $form_state) {
         $url = \Drupal\Core\Url::fromRoute('User.routename',['var1'=>'value1','var2'=>'value2']); 

How to programmatically send email in html format in Drupal 8 (2)

  1. For implement the mail_hook() function, insert the following function into the *.module file inside the module folder. Assume the module name is 'your_module_name', the file path should be "/modules/your_module_name/your_module_name.module"
    /** * Implements hook_mail(). */
    function your_module_name_mail($key, &$message, $params) {
         switch ($key) {
             case 'normal':
                $message['subject'] = $params['title'];
                $message['body'][] = $params['body'];
                break;
        }
    }
    

(Solved) "proc_open(): fork failed - Cannot allocate memory"

Cause by:

Not enough swap space in the running server.

Solution: 

  1. Create new swap
    /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
    chmod 600 /var/swap.1
    /sbin/mkswap /var/swap.1
    /sbin/swapon /var/swap.1
  2. Run your command again.
  3. Remove swap
    /sbin/swapoff /var/swap.1
    rm /var/swap.1

Pages

Subscribe to Development in Drupal 8 and Drupal 7 RSS