2015-10-01 3 views
0

Я создал пользовательский модуль в Drupal 7. Цель мода - создать некоторые записи с заднего конца и показать записи на переднем конце в виде блока. Я могу добавлять/редактировать/удалять/показывать записи через этот модуль. Я также присвоил этот мод области «Контент» в разделе «Блок» через задний конец. Однако записи не поступают на передний план. Полный код модуля пользовательской сборки приведен ниже.Drupal 7 - проблема при создании пользовательского мода

<?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; 
 
\t 
 
} 
 

 

 
function my_custom_banner_sort_with_pager_content() { 
 

 
\t 
 
/** $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; 
 
}

В ожидании вашей помощи.

+0

Вы получаете ошибку? –

+0

Необходимо создать любое представление для отображения данных на лицевой стороне. –

+0

Могу ли я использовать другой пользовательский мод для отображения данных на лицевой стороне? Если это так, то будет два пользовательских мода: один для ввода данных, другой - для отображения данных на лицевой стороне. Я прав? –

ответ

1

Раздел кода в вашей функции 'my_custom_banner_block_view', где вы присваиваете переменную $ output переменной $ block ['my_custom_banner'], неверен. Вместо этого вам нужно ввести следующий код:

$block['content'] = $output; 

«Содержимое» - это содержимое вашего блока.

Также вы создаете окончательный HTML в своей переменной $ output, тогда как вы пытаетесь повторить его снова в $ block ['my_custom_banner']. Я удалил это для вас.

Проверьте, подходит ли оно для вас. :)

+0

Да, я использовал неправильный код. Я исправил это. Мое намерение - показать результат на лицевой стороне. Я успешно ввел данные через конец, но можете ли вы рассказать мне, как я могу показать данные/записи на передней/домашней странице? –

+0

Поскольку вы внесли корректировки кода, теперь вы можете назначить блок для любого региона, который вы хотите показать на переднем конце. Я использовал ваш код и могу успешно отобразить блок. –