materio_id.module 2.5 KB

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