materio_id.module 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. function computed_field_field_index_compute($entity_type_manager, $entity, $fields, $delta){
  3. // drush_print($entity->field_index->value);
  4. if (!empty($entity->field_index->value)) { // the node is not new
  5. return $entity->field_index->value;
  6. }
  7. /* FROM D7
  8. // dsm('-- computed --');
  9. // dsm($entity_field, '$entity_field');
  10. // dsm($entity_type, '$entity_type');
  11. // dsm($entity, '$entity');
  12. // dsm($field, '$field');
  13. // dsm($instance, '$instance');
  14. // dsm($items, '$items');
  15. // dsm($entity_field[0]['value'], 'entity_field value');
  16. if (!empty($entity_field[0]['value'])) { // the node is not new
  17. return $entity_field[0]['value'];
  18. }
  19. else { // the node is new
  20. // get all same entities in same family
  21. $query = new EntityFieldQuery();
  22. $query
  23. ->entityCondition('entity_type', $entity_type)
  24. ->entityCondition('bundle', $entity->type)
  25. ->fieldCondition('field_famille', 'value', $entity->field_famille['und'][0]['value']);
  26. $result = $query->execute();
  27. if(is_array($result[$entity_type])){
  28. $entities = entity_load($entity_type, array_keys($result[$entity_type]));
  29. // get identifiants fo these entities
  30. $ids = array();
  31. foreach ($entities as $id => $e) {
  32. $identifiant = field_view_field($entity_type, $e, 'field_identifiant');
  33. $ids[] = $identifiant[0]['#markup'] ? intval($identifiant[0]['#markup']) : 0;
  34. }
  35. sort($ids);
  36. $value = strval(array_pop($ids) + 1);
  37. }else{
  38. $value = '1';
  39. }
  40. // parse the value to a string as 0023 or 4458 or 0001
  41. $value_parts = str_split($value);
  42. while (count($value_parts) < 4) {
  43. array_unshift($value_parts, "0");
  44. }
  45. // record the result
  46. $entity_field[0]['value'] = implode('', $value_parts);
  47. }
  48. */
  49. }
  50. // function computed_field_field_identifiant_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  51. // return $entity_field_item['value'];
  52. // }
  53. function computed_field_field_reference_compute($entity_type_manager, $entity, $fields, $delta) {
  54. if (!empty($entity->field_reference->value)) { // the node is not new
  55. return $entity->field_reference->value;
  56. }
  57. /* FROM D7
  58. // dsm('-- computed --');
  59. // dsm($entity_field, '$entity_field');
  60. // dsm($entity_type, '$entity_type');
  61. // dsm($entity, '$entity');
  62. // dsm($field, '$field');
  63. // dsm($instance, '$instance');
  64. // dsm($items, '$items');
  65. if (!empty($entity_field[0]['value'])) { // the node is not new
  66. return $entity_field[0]['value'];
  67. }
  68. else { // the node is new
  69. $entity_field[0]['value'] = $entity->field_famille['und'][0]['value'].'-'.$entity->field_identifiant['und'][0]['value'];
  70. }
  71. */
  72. }
  73. // function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  74. // return $entity_field_item['value'];
  75. // }