Welcome to Monthly Delhi/NCR Drupal Meetup – Pre-Requisite – Take Aways



Welcome to Monthly Delhi/NCR Drupal Meetup – Pre-Requisite – Take Aways

0 0


d8moduledevelopment_ppt


On Github joshirohit100 / d8moduledevelopment_ppt

Welcome to Monthly Delhi/NCR Drupal Meetup

Drupal 8 Module Development

About me

Rohit Joshi

(Drupal Developer)

Pre-Requisite

Oops Basics

Class

Object / Instance

Inheritance

Take Aways

Module Structure

Custom Menu

Form

Block

Module Structure

Drupal 7 Module

Required Files

                    
                      *.info
                      *.module
                    
                    

Module Location

                    sites\all\modules\mymodule\mymodule.info
                    sites\all\modules\mymodule\mymodule.module
                    

Drupal 8 Module

Required Files

                    *.info.yml
                    

Module Location

                    modules\mymodule\mymodule.info.yml
                    

Drupal 7 info file

                        name = D7 Dummy
description = Dummy module for exploring drupal 8.
core = 7.x
                    

Drupal 8 info.yml

                        name: D8 Dummy
description: Dummy module for exploring drupal 8.
type: module
core: 8.x
                    

Custom Route

Drupal 7 Way

                hook_menu() { }
                    $items['my-dummy-path'] = array(
  'title' => 'My Dummy Callback',
  'description' => "Dummy page for demo",
  'page callback' => 'my_dummy_page_callback',
  'access arguments' => array('access content'),
);
                
function my_dummy_page_callback() {
       return 'Hello friends';
}

Drupal 8 Way

Hook Menu

dummy.routing.yml
                        dummy.my_controller:
    path: '/my-dummy-path'
    defaults:
      _controller: 'Drupal\dummy\Controller\MyDummyController::my_controller'
      _title: 'My Dummy Callback'
    requirements:
      _permission: 'access content'
                    

Controller file location

                            dummy/src/Controller/MyDummyController.php
                        

MyDummyController.php file

                        /**
 * @file
 * Contains \Drupal\dummy\Controller\MyDummyController
 */

namespace Drupal\dummy\Controller;

use Drupal\Core\Controller\ControllerBase;

class MyDummyController extends ControllerBase {
  public function my_controller() {
    $build = array(
      '#type' => 'markup',
      '#markup' => $this->t('Hello Friends!'),
    );
    return $build;
  }

}
                    

Form

Drupal 7 Way

                hook_menu() { }
                    $items['my-dummy-path'] = array(
  'page callback' => 'drupal_get_form',
  'page arguments' => 'my_dummy_form',
);
                
function my_dummy_form() {
       $form = array();
       $form['my-name'] = array();
       return $form;
}
                Validation Handler :- my_dummy_form_validate();
Submit Handler :- my_dummy_form_submit();
                
            

Drupal 8 Way

Hook Menu

dummy.routing.yml
                        dummy.my_form:
    path: '/my-dummy-form'
    defaults:
      _form: 'Drupal\dummy\Form\MyDummyForm'
      _title: 'My Dummy Form'
    requirements:
      _permission: 'access content'
                    

Form file location

                            dummy/src/Form/MyDummyForm.php
                        

MyDummyForm.php file

                        /**
 * @file
 * Contains \Drupal\dummy\Form\MyDummyForm
 */

namespace Drupal\dummy\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class MyDummyForm extends FormBase {

  public function getFormId() {
    return 'my_dummy_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  public function validateForm(array &$form, FormStateInterface $form_state) {

  }

  public function submitForm(array &$form, FormStateInterface $form_state) {

  }

}
                    

Form Builder

                        
public function buildForm(array $form, FormStateInterface $form_state) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => $this->t('Name'),
    );
    .
    .
    .
  return $form;
);

                    

Form Validation

                    
public function validateForm(array &$form, FormStateInterface $form_state) {
    $name = $form_state->getValue('name');
    if (empty($name)) {
      $form_state->setErrorByName('name', $this->t('Name field is required and can not be blank.'));
    }
  }
                    

Form Submission

                    
public function submitForm(array &$form, FormStateInterface $form_state) {
    // Logic goes here.
    drupal_set_message($this->t('Thankyou for submitting the form'));
}
                    

Blocks

Block in Drupal 7

hook_block_info()

                    
function hook_block_info() {
  $blocks['my_dummy_block'] = array(
    'info' => t('Dummy Block'),
  );
  return $blocks;
}
                    
                

hook_block_view()

                    
function hook_block_view($delta = '') {
  switch ($delta) {
    case 'my_dummy_block':
      $block['subject'] = t('Dummy block subject');
      $block['content'] = t('This is my dummy block content.');
      break;
  }
  return $block;
}
                    
                

Drupal 8 block

Block are plugin in D8

Block Files Location

                            my_module/src/Plugin/Block/*
                        

Dummy Block

my_module/src/Plugin/Block/MyDummyBlock.php

                        
/**
 * Provides a 'Dummy' block.
 *
 * @Block (
 *   id = "my_dummy_block",
 *   admin_label = @Translation("My Dummy Block"),
 * )
 */
class MyDummyBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    return array(
      '#type' => 'markup',
      '#markup' => $this->t("This is my dummy block"),
    );
  }
}
                        
                    
                        
 *  @Block (
 *    id = "my_dummy_block",
 *    admin_label = @Translation("My Dummy Block"),
 * )
                        
                    

hook_block_info() replace by Annotation

Example code used

https://github.com/joshirohit100/dummy

Presentation

https://github.com/joshirohit100/d8moduledevelopment_ppt

Questions ?

Welcome to Monthly Delhi/NCR Drupal Meetup