-3

I had a case like this.

A Document content type with Taxonomy Term like this

Title

Category :

  • CAT A
  • CAT B
  • CAT C

Doc Number

When user selects CAT A, then do some query to find last number of node with CAT A. If there is no data found then it will give a starting number, if there is data found it will increment last number of node CAT A.

Every category will have its own number format and reset yearly. This number will be stored in the Doc Number Field.

Whether there is a good idea for this case? I know PHP a little, but what makes me confused is how to know which category is selected before I save this node.

Update Obstacles that I faced was to make automatic numbering based on the selected category.

Each category has its own number and reset every year. After some reading on Drupal I made my custom module like this.

function custom_node_presave($node) {
//    dsm($node);
//    $test = $node->changed;
//    $term = taxonomy_term_load($tid); // find term
//    $name = $term->name; // get term name
  if ($node->type == 'dokumen') { // if type = dokumen
    if (empty($node->nid)) { // if no nid then this is new
      $tid = $node->field_jenis_peraturan['und'][0]['tid']; // taxonomy id
      // do query to find last number
      $result = db_query_range(
        'select
           n.nid,
           n.type,
           n.title,
           nomor.field_nomor_dokumen_value,
           tahun.field_tahun_value,
           tin.tid,
           term.name
         from node n 
         left join field_data_field_nomor_dokumen nomor 
           on n.nid = nomor.entity_id 
         left join taxonomy_index tin 
           on n.nid = tin.nid
         left join taxonomy_term_data term 
           on tin.tid = term.tid
         left join field_data_field_tahun tahun 
           on n.nid = tahun.entity_id
         where tin.tid = :tid', 
       array(':tid' => $tid));

      if (!$result) { // if empty no data found
        $node->field_nomor_dokumen['und'][0]['value'] = 1; // set starting number
        $node->field_tahun['und'][0]['value'] = date("Y"); // set this year
      }
      else {
      // if data found then set data to var $no & $tahun
        foreach ($result as $item) {
          $no = $item->nomor.field_nomor_dokumen_value;
          $tahun = $item->tahun.field_tahun_value;
        }

        $ynow = date("Y"); // get current year

        if ($tahun < $ynow) { // if year in last data < current year
          $node->field_nomor_dokumen['und'][0]['value'] = '1'; // set starting number
          $node->field_tahun['und'][0]['value'] = date("Y"); // set year = current year
        }
        else {
          ++$no; // last number increment by 1
          $node->field_nomor_dokumen['und'][0]['value'] = $no;
          $node->field_tahun['und'][0]['value'] = date("Y"); // set year = current year
        }
      }
    }
  }

I hope I've explained well.

2
  • 1
    Welcome to Drupal Answers! This question is also too broad. Then, this site is not for giving good ideas, but to answer questions that are not subjective. Since you are asking about code you wrote, we need to see that code.
    – avpaderno
    Commented Dec 24, 2015 at 8:07
  • thanks for reply, I 've update my issue and show my code
    – just-d
    Commented Dec 25, 2015 at 6:23

1 Answer 1

1

To do this thing, you have to implement hook_form_alter, and #ajax for the drop down, after that create callback function for the same. In that AJAX callback you can query the database, and produce desired results.

Format is given below:

'#ajax' => array(
  'callback' => 'ajax_example_autocheckboxes_callback',
  'wrapper' => 'checkboxes-div',
  'method' => 'replace',
  'effect' => 'fade',
),

Not the answer you're looking for? Browse other questions tagged or ask your own question.