Skip to main content
deleted 285 characters in body
Source Link
Elin Y.
  • 7.4k
  • 1
  • 41
  • 65

A Document Content TypeDocument content type with Taxonomy Term like this

When user selectselects 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 Categorycategory will have its own number format and reset yearly. This number will be stored in the Doc NumberDoc Number Field.

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
          'select n.nid,
                            n.type,
                            n.title,
                            nomor.field_nomor_dokumen_value,
                            tahun.field_tahun_value,
                            tin.tid,
                term.name
         from node n term.name
                left join field_data_field_nomor_dokumen nomor  
    from node n left join field_data_field_nomor_dokumen nomor on n.nid = nomor.entity_id 
                 left join taxonomy_index tin  
       left join taxonomy_index tin on n.nid = tin.nid
                 left join taxonomy_term_data term  
       left join taxonomy_term_data term on tin.tid = term.tid
                 left join field_data_field_tahun tahun  
       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{ 
         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 'veI've explained well.

A Document Content Type with Taxonomy Term like this

When user select 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 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.

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

A Document content type with Taxonomy Term like this

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.

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.

Explain specific issue and display function I wrote
Source Link
just-d
  • 35
  • 1
  • 9

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

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

Post Closed as "Needs details or clarity" by Bala, Pierre.Vriens, Yuseferi, avpaderno
Source Link
just-d
  • 35
  • 1
  • 9

Filling the field with certain criteria before save

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 select 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 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.