欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Page Callbacks, Theme Hooks and Template Files drupal page callback function data to tpl.php 传递数据变量到模板 有大用

Page Callbacks, Theme Hooks and Template Files, O My!

derrekc edited this page on 16 May 2014 · 4 revisions

In your .module file, implement "hook_theme", as seen here...

<?php

function mymodule_theme() {
  return array(
    'bio-content' => array(
      'template' => 'bio-content',    // name of the template file, minus the '.tpl.php'

      // these are the template variables that will available to your template
      'variables' => array(
        'myvar1' => NULL,    // you will be able to use $myvar1 as a PHP variable in your 'bio-content.tpl.php' file
        'myvar2' => NULL,
        'myobj' => NULL,
      ),
    ),
  );
}

Later in your .module file, implement hook_menu (maps URL paths to page callbacks, forms, etc)

function mymodule_menu() {
  return array(
    # http://yourdrupsite/mypage is handled here
    'mypage' => array(
      'page callback' => 'mymodule_page_callback',
      'title' => 'A Title of Your Choosing',        // you can also use 'drupal_set_title()' inside your page callback to achieve the effect
      'type' => MENU_NORMAL_ITEM,        // this is the default value for a menu entry
      'access callback' => TRUE,        // this could also be a function that returns TRUE or FALSE
    ),
  );
}

In your .module file, create the 'page callback' function defined in your hook_menu implementation

function mymodule_page_callback() {
  // $page is an array of 'page elements' (see the Form API for a complete list of pre-defined elements)
  $page = array();

  // gather the data you need to be sent to your template file
  // starting with $myvar1 (defined in the hook_theme implementation above ^)
  $myvar1 = t('Hello World');        // t() is Drupal's "Translation" function
  $myvar2 = array(
    'option1' => t('Option 1'),
    'option2' => t('This is how we say Hello World in Drupal land!'),
  );
  $myobj = new stdClass();
  $myobj->thisData = 'This Data';
  $myobj->thatData = 'That Data';
  
  // Next, we're going to add a page element that Drupal will render..
  $page['bio-content'] = array(
    '#theme' => 'bio-content',        // this is the theme 'key' defined in 'mymodule_theme',
    '#myvar1' => $myvar1,        // remember the 'myvar1' variable key...?
    '#myvar2' => $myvar2,
    '#myobj' => $myobj,
  );
  # NOTE: Drupal will correctly parse all array keys that begin with '#' and process them accordingly.

  # NOTE 2: The above can also be done like this -- it's a matter of personal taste:
  // $page['bio-content'] = theme('bio-content', array('myvar1' => $myvar1, 'myvar2' => $myvar2, 'myobj' => $myobj));

  return $page;
}

Now, we need to create template file. So we'll create a file named "bio-content.tpl.php" and define it as such...

<?php
/**
 * @file template file for 'bio-content' theme
 * 
 * Available variables are:
 *   $myvar1
 *   $myvar2 (array with the following keys -- 'option1', and 'option2')
 *   $myobj (object)
 */
<div>
  <div><?php print $myvar1; ?></div>
  <div><?php print $myvar2['option1']; ?></div>
  <div><?php print $myobj->thisData; ?></div>
</div>

Now that the template file has been created, remember to clear the theme and menu caches.

Using Data from MySQL

We worked with simple, developer-defined data above, but what about when data needs to be fetched from a MySQL database? Let's re-visit the page callback function and fetch some data from an arbitrary table.

function mymodule_page_callback() {
  $page = array();

  // we'll keep myvar1 and myvar2 as-is,
  // however, let's use the database to get data for the 'myobj' variable.
  // --
  // the author prefers 'db_select', but 'db_query' is also effective (and sometimes faster)...
  // SELECT a record from the table "arbitrary" where the id = <some value>
  $query = db_select('arbitrary','a')        // 'a' represents the table alias
    ->fields('a')                // select all the fields (columns) from the table
    ->condition('arbitrary_id', $arbitrary_id, '=');        // the '=' argument is optional as it's the default for the '[SelectQuery::condition](https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery::condition/7)' function

  $result = $query->execute();

  // now fetch the object into the $myobj variable...
  $myobj = $result->fetchObject();

  // .. rest of the function remains unchanged.

  return $page;
}

But wait -- we just referred to a variable, $arbitrary_id, that has to come from somewhere, right..? So, we'll make it an argument to the page callback function, like so...

function mymodule_page_callback($arbitrary_id) {
//...
}

But now, where does that value come from...? Let's revisit the hook_menu implementation...

function mymodule_menu() {
  return array(
    # http://yourdrupsite/mypage is handled here
    'mypage' => array(
      'page callback' => 'mymodule_page_callback',
      'title' => 'A Title of Your Choosing',        // you can also use 'drupal_set_title()' inside your page callback to achieve the effect
      'type' => MENU_NORMAL_ITEM,        // this is the default value for a menu entry
      'access callback' => TRUE,        // this could also be a function that returns TRUE or FALSE
    ),
  );
}

In order to provide a value for $arbitrary_id, we can address this one of two ways...

Do nothing

We can leave the function as-is, and when Drupal receives a browser request like this --http://yourdrupalsite/mypage/111 -- every additional slug after 'mypage' will be passed to your page callback as a function argument, meaning that 111 will be passed to mymodule_page_callbackas the value in $arbitrary_id.

Page Arguments

We can specify a 'page argument' inside of our entry in the hook_menu implementation, so let's revisit the function one more time...

function mymodule_menu() {
  return array(
    # http://yourdrupsite/mypage is handled here
    'mypage' => array(
      'page callback' => 'mymodule_page_callback',
      'page arguments' => array('111'),        // any arguments you want to pass to your 'page callback' function
      'title' => 'A Title of Your Choosing',        // you can also use 'drupal_set_title()' inside your page callback to achieve the effect
      'type' => MENU_NORMAL_ITEM,        // this is the default value for a menu entry
      'access callback' => TRUE,        // this could also be a function that returns TRUE or FALSE
    ),
  );
}

Notice the additional 'page callback' => array('111') -- that will result in Drupal passing '111' to your page callback.


来自  https://github.com/duke-libraries/drupal-dul-timeline/wiki/Page-Callbacks,-Theme-Hooks-and-Template-Files,-O-My!

普通分类: