Drupal 7 - 创建自定义时出现问题 mod

Drupal 7 - issue while creating custom mod

我在 Drupal 7 中创建了一个自定义 module。mod 的目的是从后端创建一些记录并在前端将记录显示为一个块。我可以通过这个 mod 规则 add/edit/delete/show 记录。我还通过后端将此 mod 分配给块部分中的 "Content" 区域。但是,记录不会出现在前端。下面给出了自定义构建 module 的完整代码。

<?php

/**
* @file
* Custom functions for this site.
*/

/**
* Implements hook_menu().
*/
function my_custom_banner_menu()
{
$items['admin/custom/my_custom_banner/show'] = array(
'title' => 'My Custom Table',
'description' => 'View My Custom Banner.',
'page callback' => 'my_custom_banner_sort_with_pager_content',
'access arguments' => array('access my_custom_banner'),
'weight' => -14,
);


$items['admin/custom/my_custom_banner/%/add'] = array(
'title' => 'My Custom Table',
'description' => 'View My Custom Table.',
'page callback' => 'my_custom_banner_add_func',
'access arguments' => array('access my_custom_banner'),
'weight' => -14,
);

$items['admin/custom/my_custom_banner/%/edit'] = array(
'title' => 'My Custom Table',
'description' => 'View My Custom Table.',
'page callback' => 'my_custom_banner_edit_block_view',
'page argument' => array(3),
'access arguments' => array('access my_custom_banner'),
'weight' => -14,
);

$items['admin/custom/my_custom_banner/%/delete'] = array(
'title' => 'My Custom Table',
'description' => 'View My Custom Table.',
'page callback' => 'my_custom_delete',
'page argument' => array(3),
'access arguments' => array('access my_custom_banner'),
'weight' => -14,
);


$items['admin/custom/my_custom_banner'] = array(
'title' => 'My Custom Table',
'description' => 'View My Custom Table.',
'page callback' => 'my_custom_banner_sort_with_pager_content',
'access arguments' => array('access my_custom_banner'),
'weight' => -14,
);


return $items;

// return $items;
}

/**
* Implements hook_block_view().
*/
function my_custom_banner_block_view($block_name = '')
{
#echo "show";
// in my example I show the form only in the front page.
// You can show it where you want, obviously

/**
if (drupal_is_front_page())
{
return NULL;
}

 * */
 

$header = array(
array('data' => t('Custom id'), 'field' => 'id', 'sort' => 'asc'),
array('data' => t('Title'), 'field' => 'title'),
array('data' => t('Status'), 'field' => 'status'),
array('data' => t('Action')),
);

$query = db_select('custom_table', 'c');
$query->fields('c', array('id', 'title', 'status'));

$table_sort = $query->extend('TableSort') // Add table sort extender.
->orderByHeader($header); // Add order by headers.
$pager = $table_sort->extend('PagerDefault')
->limit(5);
$result = $pager->execute();

$rows = array();
foreach($result as $res){
$rows[] = array($res->id, $res->title, $res->status);
}

// If rows are not empty theme and display the rows.

if (!empty($rows)) {
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));
$output .= theme('pager');
}
else {
$output .= t("No results found");
}




$block['my_custom_banner'] .= theme('item_list', array(
    'items' => $output
  ));
  
  return $block;

 
 
}

function my_custom_banner_form($form, &$form_state)
{
// now I add a text field to the form
$form['my_custom_banner_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t('The Title of the My Custom Table.'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
);

// Textarea for the body
$form['my_custom_banner_description'] = array(
'#type' => 'textarea',
'#rows' => 10,
'#columns' => 40,
'#title' => t('Description'),
'#required' => TRUE,
'#description'=> t('The text of My Custom Table .'),
);

// Checkbox to indicate.
$form['my_custom_active'] = array(
'#type' => 'checkbox',
'#title' => t('Status'),
'#description' => t("Indicates whether the active or inactive."),
);
// now I add also a button
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Save'),
);
// and now I assign a my function as handler of the submit event
// $form['#validate'][] = 'my_custom_banner_submit_handler';
$form['#submit'][] = 'my_custom_banner_submit_handler';
return $form;
}

function my_custom_banner_submit_handler($form, &$form_state)
{
// this function will be executed after the click
// event of the user on the "submit” button.
// here I only print a message
// you can access a database, redirect, or whatever you want, obviously
$error = 1;
if ( !isset($form_state['values']['my_custom_banner_title']) || !isset($form_state['values']['my_custom_banner_title']) ) {
$error = 0 ;
}

if($error){
$my_custom_banner_title = $form_state['values']['my_custom_banner_title'];
$my_custom_banner_description = $form_state['values']['my_custom_banner_description'];
$nid = db_insert('custom_table') // Table name no longer needs {}
->fields(array(
'title' => $my_custom_banner_title,
'description' => $my_custom_banner_description,
))
->execute();
drupal_set_message(t('Record has been added!'));
}
}


function my_custom_banner_add_func(){
$form = drupal_get_form('my_custom_banner_form');
$block = array
(
// 'subject' => t('Subject'),
'content' => $form,
);
// $block['content'][] .= '<br /><a href=”add”>Back to Listing</a>';
return $block;
 
}


function my_custom_banner_sort_with_pager_content() {

 
/** $form = drupal_get_form('my_custom_banner_form');
$block = array
(
// 'subject' => t('Subject'),
'content' => $form,
);
// $block['content'][] .= '<br /><a href=”add”>Back to Listing</a>';
return $block;
*/

$header = array(
array('data' => t('Custom id'), 'field' => 'id', 'sort' => 'asc'),
array('data' => t('Title'), 'field' => 'title'),
array('data' => t('Status'), 'field' => 'status'),
array('data' => t('Action')),
);

$query = db_select('custom_table', 'c');
$query->fields('c', array('id', 'title', 'status'));

$table_sort = $query->extend('TableSort') // Add table sort extender.
->orderByHeader($header); // Add order by headers.
$pager = $table_sort->extend('PagerDefault')
->limit(5);
$result = $pager->execute();

$rows = array();
foreach($result as $res){
$rows[] = array($res->id, $res->title, $res->status, "<a href='$res->id/edit'>Edit</a> | <a href='$res->id/delete' onclick='return confirm(\"Are you sure\")'>Delete</a>");
}

// If rows are not empty theme and display the rows.

if (!empty($rows)) {
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));
$output .= theme('pager');
}
else {
$output .= t("No results found");
}
$output .= '<br /><a href="my_custom_banner/add">Add new record</a>';
return $output;
}





function my_custom_delete(){
$id = arg(3);
$num_updated = db_delete('custom_table')
->condition('id', $id, '=')
->execute();
drupal_set_message(t('Record has been deleted!'));
drupal_goto("admin/custom/my_custom_banner/show");
}

/**
* Implements hook_block_view().
*/
function my_custom_banner_edit_block_view($block_name = '')
{

// in my example I show the form only in the front page.
// You can show it where you want, obviously


if (drupal_is_front_page())
{
return NULL;
}



$form = drupal_get_form('my_custom_banner_edit_form');


$block = array
(
// 'subject' => t('Subject'),
'content' => $form,
);
$block['content'][] .= '<br /><a href="add">Back to Listing</a>';
return $block;
}





function my_custom_banner_edit_form($form, &$form_state)
{

$id = arg(3);
$result = db_query('SELECT * FROM {custom_table} WHERE id = :tid', array(':tid' => $id));
/* foreach($result as $val){
$record = $val;
}*/
$record = $result->fetchObject();

// now I add a text field to the form
// with a label and fixed dimensions (you never know…)
$form['my_custom_banner_title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#value' => t($record->title),
'#description' => t('The Title of the My Custom Table.'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
);

// Textarea for the body
$form['my_custom_banner_description'] = array(
'#type' => 'textarea',
'#rows' => 10,
'#columns' => 40,
'#title' => t('Description'),
'#value' => t($record->description),
'#required' => TRUE,
'#description'=> t('The text of My Custom Table .'),
);
// hidden for the body
$form['id'] = array(
'#type' => 'hidden',
'#value' => t($id),
);

// Checkbox to indicate.
$form['my_custom_active'] = array(
'#type' => 'checkbox',
'#title' => t('Status'),
'#description' => t("Indicates whether the active or inactive"),
);
// now I add also a button
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Save'),
);
// and now I assign a my function as handler of the submit event
// $form['#validate'][] = 'my_custom_banner_submit_handler';
$form['#submit'][] = 'my_custom_banner_edit_submit_handler';
return $form;
}

function my_custom_banner_edit_submit_handler($form, &$form_state)
{
// this function will be executed after the click
// event of the user on the "submit” button.
// here I only print a message
// you can access a database, redirect, or whatever you want, obviously
$error = 1;
if ( !isset($form_state['values']['my_custom_banner_title']) || !isset($form_state['values']['my_custom_banner_title']) ) {
$error = 0 ;
}

if($error){
$id = $form_state['values']['id'];
//var_dump($form_state);
$my_custom_banner_title = $form_state['input']['my_custom_banner_title'];
$my_custom_banner_description = $form_state['input']['my_custom_banner_description'];
$data = array(
'title' => $my_custom_banner_title,
'description' => $my_custom_banner_description,
);
$num_updated = db_update('custom_table')
->fields($data)
->condition('id', $id, '=')
->execute();
drupal_set_message(t('Record has been Updated!'));
}
}

/**
* Implements hook_permission().
*/
function my_custom_banner_permission() {
return array(
'access my_custom_banner' => array(
'title' => t('View My Custom Table'),
// Note: We translate the 'Administer blocks' permission string here with
// a separate t() call, to make sure it gets the same translation as when
// it's in block_permission().
'description' => t('Customizing the My Custom Table requires the !permission-name permission.', array(
'!permission-name' => l(t('Administer blocks'), 'admin/people/permissions', array('fragment' => 'module-block')),
)),
),
);
}

function my_custom_banner_block_info() {
  $blocks['my_custom_banner'] = array('info' => t('Custom Banner block'));
  return $blocks;
}

等待您的帮助。

'my_custom_banner_block_view' 函数中将 $output 变量分配给 $block['my_custom_banner'] 的代码部分是错误的。您需要改用以下代码 -

$block['content'] = $output;

'content' 是您的块的内容。

您还在 $output 变量中创建最终的 HTML,而您正试图在 $block['my_custom_banner'] 中再次设置主题。我已经为你删除了它。

请检查这是否适合您。 :)