Update Value Taxonomy

By admin, 11 August, 2020

 

Update แบบ fix field นั้นๆ ว่าจะเป็นค่าอะไร

   // Get storage handler.
  $storage_handler = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

  // Fetch terms object.
  $data_term = $storage_handler->loadByProperties([
    'vid' => ['taxonomy_1'],
  ]);

  if (!empty($data_term)) {
    foreach ($data_term as $term) {
      $term_obj = $storage_handler->load($term->id());
      // Update by field term.
      $status = $term_obj->field_status->getValue();
      if(empty($status)){
        $term_obj->field_status->setValue(0);
        $term_obj->Save();
      }
    }
  }

 

ส่วนนี้ใช้เพื่อต้องกับ update value field ที่เราเพิ่ม field มาทีหลังให้ ค่าใน field นั้นๆ

 $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
      'vid' => [
        'taxonomy_1',
        'taxonomy_2',
      ],
    ]);
    foreach ($terms as $term) {
      $term->save();
    }