materio_id.module 2.8 KB

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