idmaterio.module 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * @file
  4. * This is the file description for Idmaterio module.
  5. *
  6. * In this more verbose, multi-line description, you can specify what this
  7. * file does exactly. Make sure to wrap your documentation in column 78 so
  8. * that the file can be displayed nicely in default-sized consoles.
  9. */
  10. function computed_field_field_identifiant_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  11. // dsm('-- computed --');
  12. // dsm($entity_field, '$entity_field');
  13. // dsm($entity_type, '$entity_type');
  14. // dsm($entity, '$entity');
  15. // dsm($field, '$field');
  16. // dsm($instance, '$instance');
  17. // dsm($items, '$items');
  18. // dsm($entity_field[0]['value'], 'entity_field value');
  19. if (!empty($entity_field[0]['value'])) { // the node is not new
  20. return $entity_field[0]['value'];
  21. }
  22. else { // the node is new
  23. // get all same entities in same family
  24. $query = new EntityFieldQuery();
  25. $query
  26. ->entityCondition('entity_type', $entity_type)
  27. ->entityCondition('bundle', $entity->type)
  28. ->fieldCondition('field_famille', 'value', $entity->field_famille['und'][0]['value']);
  29. $result = $query->execute();
  30. if(is_array($result[$entity_type])){
  31. $entities = entity_load($entity_type, array_keys($result[$entity_type]));
  32. // get identifiants fo these entities
  33. $ids = array();
  34. foreach ($entities as $id => $e) {
  35. $identifiant = field_view_field($entity_type, $e, 'field_identifiant');
  36. $ids[] = $identifiant[0]['#markup'] ? intval($identifiant[0]['#markup']) : 0;
  37. }
  38. sort($ids);
  39. $value = strval(array_pop($ids) + 1);
  40. }else{
  41. $value = '1';
  42. }
  43. // parse the value to a string as 0023 or 4458 or 0001
  44. $value_parts = str_split($value);
  45. while (count($value_parts) < 4) {
  46. array_unshift($value_parts, "0");
  47. }
  48. // record the result
  49. $entity_field[0]['value'] = implode('', $value_parts);
  50. }
  51. }
  52. function computed_field_field_identifiant_display($field, $entity_field_item, $entity_lang, $langcode) {
  53. return $entity_field_item['value'];
  54. }