Current time: 08-20-2017, 03:52 PM Hello There, Guest! (LoginRegister)

Post Reply 
Using existing PHP code in a Drupal custom module
08-04-2017, 10:42 AM (This post was last modified: 08-04-2017 11:59 AM by MajorGeek.)
Post: #1
Using existing PHP code in a Drupal custom module
My agency is heading towards dropping our current web development and having expensive consultants create a new site in Drupal. I keep trying to understand Drupal and see its appeal, but it gets complicated quickly.

I've looked at Drupal charting modules, and they all seem to take input from CVS files. The big blind spot in Drupal developers seems to be getting data from SQL queries into their modules. There is something I found, the /data/content/entities Maybe that's it. But I'm starting to test weather I can get my existing PHP routines in Drupal custom modules.

I used Aquia Dev Desktop to whip up a few Drupal 8 test sites on my local Windows 10 PC.

I was able to follow the instructions at https://www.drupal.org/docs/8/creating-custom-modules/a-hello-world-cust... to get the Hello World custom module working on my install of Drupal 8.3.4 on a local Windows PC. Next I tried to stick a snippet of my php code into a custom module controller to see if I could print out 10 lines of a MySQL query.

Following the Hello World example, I set up a parallel folder structure to query_Prosser in modules\custom.
https://www.dropbox.com/s/z2z8fm24czgqa5...l.png?dl=0

Then I created an yml file, query_Prosser.info.yml:

Code:
name: Query Prosser Module
description:  Creates a page testing a query of MySQL table tblProssercuryr, printing results from first 10 rows.
package: Custom

type: module
core: 8.x

At this point, the Query Prosser Module appeared in the Custom section of the Modules page.

Then I created a query_Prosser.routing.yml file:

Code:
query_Prosser.content:
  path: '/query'
  defaults:
   _controller: '\Drupal\query_Prosser\Controller
\queryController::content'  
   _title: 'Query Prosser'
requirements:
   _permission: 'access content'

Then I tested a piece of php script on my remote web host that prints out the first 10 lines of a query of a MySQL table, tblProssercuryr. I copied the script lines into a public function of a php script, queryController, on my test Drupal site on local host. not knowing exactly why, I tried to imitate the Hello World example by adding the namespace, use, and class lines, and wraping my php code in a public function.

PHP Code:
<?php

namespace Drupal\query_Prosser\Contoller;

use 
Drupal\Core\Controller\ControllerBase;

class 
queryController extends ControllerBase {
   
    public function 
content() {

//require_once("../ChartDirector/lib/phpchartdir.php");

//testQuery.php
// P. Huffman  8-1-2017
// Retrieve and print a few MySQL records in preparation to trying this as a Drupal module. 

//$year = $_GET["year"];
//$yearprior = $year - 1;
//first read in adult chinook data from a mysql table at mysql.ykfp.org, database db28989a
    
$SQLstatement "Select (Date), Chinook From tblProssercuryr
    WHERE date > '$2017-04-01' and date < '2017-11-15'
    and Chinook IS NOT NULL
    order by Date
    limit 10"
;
    
#
    #Read in the data into arrays
    #
    
$db mysql_connect"mysql.ykfp.org""username""password"'db28989a');
    if (
mysqli_connect_error())
    {
        echo 
'Error: Could not connect to database.';
        exit;
    }   
    
$result mysql_db_query("db28989a"$SQLstatement);
   
    
$num_results $result->num_rows;

    
#Initialize counters and arrays
    
$count 0;
    
$chinooktotal 0;
    
$date = array();
    
$chinook = array();
    while (
$row mysql_fetch_row($result)) {
        print (
sprintf ("date = %s, chinook = %d\n <br />",$row[0],$row[1]));
       
        
$date[$count] = $row[0];
        
$chinook[$count] = $row[1];
        
$chinooktotal $chinooktotal $chinook[$count];
       
    ++
$count;
    }
    
mysql_free_result($result);
    
$dbh->disconnect;
  }
}

?>

Then I thought I activated the custom module by clearing the cache, checking its box at http://drupal-8-3-4.dd:8083/admin/modules, and hitting the install button. But unlike the hello_world custom module that appeared at http://drupal-8-3-4.dd:8083/hello, http://drupal-8-3-4.dd:8083/query gave me a Drupal site template page with "Page not found" . What am I not understanding? Am I on the right track? I also tried not wrapping my php is a function.

This signature line intentionally blank.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-04-2017, 11:05 AM
Post: #2
RE: Using existing PHP code in a Drupal custom module
Did you see anything interesting in the web server's logs? These Drupal-native debugging tools may come in handy to debug your module: https://www.drupal.org/project/devel
Find all posts by this user
Quote this message in a reply
08-07-2017, 02:58 PM (This post was last modified: 08-07-2017 03:07 PM by MajorGeek.)
Post: #3
RE: Using existing PHP code in a Drupal custom module
Nothing useful in the logs, just Page not found. I'm already running Dev module because of the youtube tutorial I ran through.

I see that the Hello World example continues with more examples and exercises. I see what I can learn from that.

Oh, wait, when I went back to Drupal Extend to recheck the box on my custom module, and cleared the cache again, now I get Access denied in my drupal-8-3-4.dd:8083/query. I thought that was taken care of by the _permission: 'access content' in my query_prosser.routing.yml file.

This signature line intentionally blank.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: