On Github joshirohit100 / d8moduledevelopment_ppt
*.info *.module
sites\all\modules\mymodule\mymodule.info sites\all\modules\mymodule\mymodule.module
*.info.yml
modules\mymodule\mymodule.info.yml
name = D7 Dummy description = Dummy module for exploring drupal 8. core = 7.x
name: D8 Dummy description: Dummy module for exploring drupal 8. type: module core: 8.x
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'; }
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'
dummy/src/Controller/MyDummyController.php
/** * @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; } }
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();
dummy.routing.yml
dummy.my_form: path: '/my-dummy-form' defaults: _form: 'Drupal\dummy\Form\MyDummyForm' _title: 'My Dummy Form' requirements: _permission: 'access content'
dummy/src/Form/MyDummyForm.php
/** * @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) { } }
public function buildForm(array $form, FormStateInterface $form_state) { $form['name'] = array( '#type' => 'textfield', '#title' => $this->t('Name'), ); . . . return $form; );
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.')); } }
public function submitForm(array &$form, FormStateInterface $form_state) { // Logic goes here. drupal_set_message($this->t('Thankyou for submitting the form')); }
function hook_block_info() { $blocks['my_dummy_block'] = array( 'info' => t('Dummy Block'), ); return $blocks; }
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; }
my_module/src/Plugin/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
https://github.com/joshirohit100/dummy
https://github.com/joshirohit100/d8moduledevelopment_ppt