Showing posts with label controller file. Show all posts
Showing posts with label controller file. Show all posts

Tuesday 2 July 2019

How to display records in custom module with pagination?

It is simple to display record/data from database in drupal 8 with pagination, here we are going to explain by example with some simple steps:
Step 1:
Create a folder with name of "gpcustom", this is our custom module name. Now create a file "gpcustom.info.yml" for create identity of module.
and the code for this file is..

name: Student Records
description: 'Display student records'
type: module
core: 8.x


Step 2:
Create a file with name of "gpcustom.routing.yml", here we are going to declare routing means path for display record page.
and the code for this file is..

google_feeds_list:
  path: 'student/record'
  defaults:
    _controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record'
    _title: 'Student Records'
  requirements:
    _permission: 'access content'   

   
// _controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record'  means '\Drupal\gpcustom\Controller\viewController' is file path for function and 'student_record' is the function name.   
// _permission: 'access content' means anybody can access this page without login.
// path: 'student/record' means display record page url.
// _title: 'Student Records' means title of page.


Step 3:
Create a folder with this hierarchy '\src\Controller\' and file with name of "gpcustomController.php".
and the code for this file is..
<?php
namespace Drupal\gpcustom\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;

/**
 * Class DisplayTableController.
 *
 * @package Drupal\gpcustom\Controller
 */

class gpcustomController extends ControllerBase {

  /**
   * Returns a render-able array for a test page.
   */

  public function student_record() {
    //create table header
         $header = array(
         'id'=> t('SrNo'),
         'name' => t('Candidate Name'),
         'mobile' => t('Mobile Number'),
         'email' => t('Email ID'),
         'age' => t('Age'),
         'gender' => t('Gender'),
         'opt' => t('operations'),
         );

    //select records from table
    $query = \Drupal::database()->select('mydata', 'm');
    $query->fields('m', ['id','name','mobilenumber','email','age','gender']);
    $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(3);
      // The actual action of sorting the rows is here.
    $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')
            ->orderByHeader($header);
    $result = $pager->execute();
    $rows=array();
    foreach($result as $data){
        if ($data->id != 0 && $data->id != 1) {
   
        $operate = '<a href="/drupaladvance/mydata/form/mydata?num='.$data->id.'">Edit</a>|<a href="/drupaladvance/mydata/form/delete/'.$data->id.'">delete</a>';
        //print the data from table
         $rows[$data->id] = array(
             'id' =>$data->id,
             'name' => $data->name,
             'mobile' => $data->mobilenumber,
             'email' => $data->email,
             'age' => $data->age,
             'gender' => $data->gender,
             'operation' => t($operate),
           
         );
        }
    }
        //display data in site
         $form['table'] = [
         '#type' => 'table',
         '#header' => $header,
     '#rows' => $rows,
     '#empty' => t('No users found'),
     ];
      // Finally add the pager.
        $form['pager'] = array(
          '#type' => 'pager'
      );
     return $form;

    }
}

?>
So that's all for display data by csutom module in drupal 8, now clear the cache and goto the modules page '/admin/modules' and find by name your module and enable it.
and go to the URL which we decide in '.routing.yml' file.


Note: If have any suggestions or issue regarding 'How to display records in custom module with pagination?' then you can ask by comments. 

Thursday 24 January 2019

How to render template for custom module in drupal 8?

It is easy way to render/call template(.html.twig) file for custom module in drupal 8, But here concept is slightly different from drupal 7.
First we will go to the Controller file:
Like our controller name is ContactController.php so we will write there code like.
//Go to the ContactController.php file..
/**
 * @file
 * Contains \Drupal\contact\Controller\ContactController.
 */

namespace Drupal\contact\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Controller\ControllerBase;


class Contactv4Controller extends ControllerBase {  
    public function indexv2() {
     
      $data = "<div>Testing</div>";
     
      return [
       '#theme'  => 'specific_contact',
       '#contact'   => $data,
    ];
  }
}

// Then go to the contact.module file.
/**
* Implements hook_theme() to add the template definition..
**/
function contact_theme($existing, $type, $theme, $path) {
  return array(
       'specific_contact' => [
        'variables' => [
            'contact' => NULL,
       ]
    ]
  );
}
 
// Now in the create template folder and the file with name of 'specific_contact.html.twig'.
And render the variable there like: <p> {{ contact }} </p>
And clear the cache after this.
Now Front end result will be 'Testing' on new template page.


Note: If have any suggestions or issue regarding 'How to render template for custom module in drupal 8?' then you can ask by comments.   

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...