drupal节点类型hook_insert()
我在构建 drupal 自定义节点类型时遇到了一些麻烦。我创建了一个模块 sj_highcharts,它最终将使用 highcharts api 来提供数据图表。该模块的重点是创建一个节点类型,该节点类型将显示一个表单,供人们与图表 API 交互,将数据存储在一些数据库表中,然后最终输出图表。
问题是,提交表单后,我的 hook_insert 函数甚至 hook_validate 函数都没有被调用,并且没有任何内容存储到数据库中。我已经发表了声明,以确保他们不会开火。
代码:
/*
* Implementation of hook_node_info().
*/
function sj_highcharts_node_info() {
//defining one node type: 'sj highchart'.
return array(
'sj_highcharts_element' => array(
'name' => t('Highchart Element'),
'module' => 'sj_highcharts',
'description' => t("An element that creates a dynamic chart from specified data."),
'has_title' => FALSE,
'has_body' => FALSE,
'locked' => TRUE,
)
);
//In order to make this an element type, we will have to check the "is element" field in the content type administration page.
}
/**
* Implementation of hook_form().
*/
function sj_highcharts_form($node) {
$type = node_get_types('type', $node);
$form['delimiter'] = array(
'#type' => 'radios',
'#title' => t('Data Delimiter'),
'#description' => t('The character in which the data is delimited.'),
'#options' => array(t('Tab'), t('Space'), t('Comma')),
'#default_value' => '',
);
$form['x_label'] = array(
'#type' => 'textfield',
'#title' => t('x-axis label'),
'#description' => t('The label to be set and displayed for the x axis.'),
'#required' => TRUE,
'#default_value' => '',
);
$form['x_data'] = array(
'#type' => 'textarea',
'#title' => t('x-axis data'),
'#description' => t('The x-axis data to be populated on the chart'),
'#default_value' => '',
'#rows' => 10,
'#required' => TRUE,
);
$form['y_label'] = array(
'#type' => 'textfield',
'#title' => t('x-axis label'),
'#description' => t('The label to be set and displayed for the y axis.'),
'#required' => TRUE,
'#default_value' => '',
);
$form['y_data'] = array(
'#type' => 'textarea',
'#title' => t('y-axis data'),
'#description' => t('The y-axis data to be populated on the chart'),
'#default_value' => '',
'#rows' => 10,
'#required' => TRUE,
);
$form['type'] = array(
'#type' => 'select',
'#title' => t('Select a chart type'),
'#default_value' => 'Bar',
'#description' => t('Select a chart type to display data.'),
'#options' => array(
'1' => t('Pie'),
'2' => t('Line'),
'3' => t('Area'),
'4' => t('Scatter'),
'5' => t('Bar'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create Chart')
);
return $form;
}
/**
* Implentation of hook_validate().
*/
function sj_highcharts_validate($node) {
//watchdog('sj_highcharts', 'in validate function');
die();
if(isset($node->delimiter)){
//if we are dealing with tab delimited input
if($node->delimiter == 'tab'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(' ', $node->x_data) && substr(' ', $node->y_data)){
$xdata_parts = explode(' ', $node->x_data);
$ydata_parts = explode(' ', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
//if we are dealing with space delimited input
if($node->delimiter == 'space'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(' ', $node->x_data) && substr(' ', $node->y_data)){
$xdata_parts = explode(' ', $node->x_data);
$ydata_parts = explode(' ', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
//if we are dealing with comma delimited input
if($node->delimiter == 'comma'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(',', $node->x_data) && substr(',', $node->y_data)){
$xdata_parts = explode(',', $node->x_data);
$ydata_parts = explode(',', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
}
}
/**
* Implementation of hook_form_submit().
*/
function sj_highcharts_insert($node) {
//watchdog('sj_highcharts', 'in submit function');
die();
drupal_write_record('highcharts_chart', $node);
$x_data_parts = explode(',', $node->x_data);
$axis = 'x';
for($i = 0;$i < sizeof($x_data_parts);$i++){
$data_point = $x_data_parts[$i];
//not sure if i should use write_record or not, just know that i need to separate individual data fields
db_query("INSERT into {higcharts_data} (chart_id, data, axis) VALUES (%d, %d, '%s')", $node->nid, $data_point, $axis);
}
$y_data_parts = explode(',', $node->y_data);
$axis = 'y';
for($i = 0;$i < sizeof($y_data_parts);$i++){
$data_point = $y_data_parts[$i];
//not sure if i should use write_record or not, just know that i need to make sure the chart_id matches other table
db_query("INSERT into {higcharts_data} (chart_id, data, axis) VALUES (%d, %d, '%s')", $node->nid, $data_point, $axis);
}
}
我已经盯着这个花了整整一个工作日,但找不到问题。任何关于这方面的指导都会很棒。谢谢布兰登
Im having some trouble with constructing a drupal custom node type. I created a module, sj_highcharts that will eventually use the highcharts api to deliver a chart of data. The point of this module is to create a node type that will display a form for people to interface with the chart api, store the data in some database tables and then finally output the chart.
The problem is that my hook_insert function or even the hook_validate function isnt being called after the form has been submitted and nothing is getting stored into the database. I have put die statement, to make sure and they don't fire.
The code:
/*
* Implementation of hook_node_info().
*/
function sj_highcharts_node_info() {
//defining one node type: 'sj highchart'.
return array(
'sj_highcharts_element' => array(
'name' => t('Highchart Element'),
'module' => 'sj_highcharts',
'description' => t("An element that creates a dynamic chart from specified data."),
'has_title' => FALSE,
'has_body' => FALSE,
'locked' => TRUE,
)
);
//In order to make this an element type, we will have to check the "is element" field in the content type administration page.
}
/**
* Implementation of hook_form().
*/
function sj_highcharts_form($node) {
$type = node_get_types('type', $node);
$form['delimiter'] = array(
'#type' => 'radios',
'#title' => t('Data Delimiter'),
'#description' => t('The character in which the data is delimited.'),
'#options' => array(t('Tab'), t('Space'), t('Comma')),
'#default_value' => '',
);
$form['x_label'] = array(
'#type' => 'textfield',
'#title' => t('x-axis label'),
'#description' => t('The label to be set and displayed for the x axis.'),
'#required' => TRUE,
'#default_value' => '',
);
$form['x_data'] = array(
'#type' => 'textarea',
'#title' => t('x-axis data'),
'#description' => t('The x-axis data to be populated on the chart'),
'#default_value' => '',
'#rows' => 10,
'#required' => TRUE,
);
$form['y_label'] = array(
'#type' => 'textfield',
'#title' => t('x-axis label'),
'#description' => t('The label to be set and displayed for the y axis.'),
'#required' => TRUE,
'#default_value' => '',
);
$form['y_data'] = array(
'#type' => 'textarea',
'#title' => t('y-axis data'),
'#description' => t('The y-axis data to be populated on the chart'),
'#default_value' => '',
'#rows' => 10,
'#required' => TRUE,
);
$form['type'] = array(
'#type' => 'select',
'#title' => t('Select a chart type'),
'#default_value' => 'Bar',
'#description' => t('Select a chart type to display data.'),
'#options' => array(
'1' => t('Pie'),
'2' => t('Line'),
'3' => t('Area'),
'4' => t('Scatter'),
'5' => t('Bar'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create Chart')
);
return $form;
}
/**
* Implentation of hook_validate().
*/
function sj_highcharts_validate($node) {
//watchdog('sj_highcharts', 'in validate function');
die();
if(isset($node->delimiter)){
//if we are dealing with tab delimited input
if($node->delimiter == 'tab'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(' ', $node->x_data) && substr(' ', $node->y_data)){
$xdata_parts = explode(' ', $node->x_data);
$ydata_parts = explode(' ', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
//if we are dealing with space delimited input
if($node->delimiter == 'space'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(' ', $node->x_data) && substr(' ', $node->y_data)){
$xdata_parts = explode(' ', $node->x_data);
$ydata_parts = explode(' ', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
//if we are dealing with comma delimited input
if($node->delimiter == 'comma'){
//check to see if data sets are the same size
if(isset($node->x_data) && isset($node->y_data)) {
if(substr(',', $node->x_data) && substr(',', $node->y_data)){
$xdata_parts = explode(',', $node->x_data);
$ydata_parts = explode(',', $node->y_data);
}
else{
form_set_error('delimiter', t('The data delimiter must match the data string entered.'));
}
if(sizeof($xdata_parts) != sizeof($ydata_parts)){
form_set_error('x_data', t('The number of data points in each, x and y axes, must match.'));
}
}
//change all delimited input to the format we want
$node->x_data = str_replace(' ', ',', $node->x_data);
$node->y_data = str_replace(' ', ',', $node->y_data);
}
}
}
/**
* Implementation of hook_form_submit().
*/
function sj_highcharts_insert($node) {
//watchdog('sj_highcharts', 'in submit function');
die();
drupal_write_record('highcharts_chart', $node);
$x_data_parts = explode(',', $node->x_data);
$axis = 'x';
for($i = 0;$i < sizeof($x_data_parts);$i++){
$data_point = $x_data_parts[$i];
//not sure if i should use write_record or not, just know that i need to separate individual data fields
db_query("INSERT into {higcharts_data} (chart_id, data, axis) VALUES (%d, %d, '%s')", $node->nid, $data_point, $axis);
}
$y_data_parts = explode(',', $node->y_data);
$axis = 'y';
for($i = 0;$i < sizeof($y_data_parts);$i++){
$data_point = $y_data_parts[$i];
//not sure if i should use write_record or not, just know that i need to make sure the chart_id matches other table
db_query("INSERT into {higcharts_data} (chart_id, data, axis) VALUES (%d, %d, '%s')", $node->nid, $data_point, $axis);
}
}
Ive been staring at this for a full workday and cannot find the issue. Any guidance on this would be great. Thanks Brandon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 examples 模块中的 node_example 子模块。它应该可以帮助您指明正确的方向。
您的帖子不清楚您正在使用哪个版本的 drupal。在 Drupal 6 中,您可以直接向表单或表单项添加额外的验证和提交功能。
前任:
Check out the node_example sub module in the examples module. It should help point you in the right direction.
It's unclear by your post as to which version of drupal you're using. In Drupal 6 you can add additional validate and submit functions to a form or form item directly.
ex: