2011-10-21 2 views
1

Drupal 사용자 정의 노드 유형을 구성하는 데 약간의 문제가 있습니다. 결국 하이 차트 API를 사용하여 데이터 차트를 전달하는 모듈 sj_highcharts를 만들었습니다. 이 모듈의 요점은 사람들이 차트 api와 인터페이스하고 일부 데이터베이스 테이블에 데이터를 저장 한 다음 마지막으로 차트를 출력하는 양식을 표시 할 노드 유형을 작성하는 것입니다.drupal 노드 유형 hook_insert()

문제는 내 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); 
    } 
} 

필자는 전체 근무 이렇게보고하고 문제를 찾을 수 없습니다. 이것에 대한 안내는 훌륭합니다. 감사합니다 Brandon

+0

당신이 드루팔의 캐시를 삭제 했습니까? 일부 후크 구현은 캐쉬되므로 – Clive

+1

예, 캐시를 지우지 않으면 사용할 수 없습니다. 드루팔 (Drupal)의 요점은 내가 모든 작업을 마친 후에 캐시를 지우는 것입니다. – BDUB

답변

0

examples 모듈의 node_example 하위 모듈을 확인하십시오. 그것은 올바른 방향으로 당신을 가리 키도록 도와야합니다.

사용중인 drupal의 버전이 귀하의 게시물에 의해 분명하지 않습니다. Drupal 6에서는 양식이나 양식 항목에 직접 유효성 검사 및 제출 기능을 추가 할 수 있습니다.

예 : 당신은`hook_insert`와`hook_validate`을 구현하기 때문에

$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' => '', 
     '#validate' => array('_name_of_custom_validation_function'), 
     '#submit' => array('_name_of_custom_submit_function'), 
    );