idmaterio.module 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = "en", $langcode = "en") {
  53. return $entity_field_item['value'];
  54. }
  55. function computed_field_field_reference_materio_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  56. // dsm('-- computed --');
  57. // dsm($entity_field, '$entity_field');
  58. // dsm($entity_type, '$entity_type');
  59. // dsm($entity, '$entity');
  60. // dsm($field, '$field');
  61. // dsm($instance, '$instance');
  62. // dsm($items, '$items');
  63. if (!empty($entity_field[0]['value'])) { // the node is not new
  64. return $entity_field[0]['value'];
  65. }
  66. else { // the node is new
  67. $entity_field[0]['value'] = $entity->field_famille['und'][0]['value'].'-'.$entity->field_identifiant['und'][0]['value'];
  68. }
  69. }
  70. function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  71. return $entity_field_item['value'];
  72. }
  73. /**
  74. * Implements hook_form_alter().
  75. */
  76. function idmaterio_form_alter(&$form, &$form_state, $form_id) {
  77. if($form_id == "materiau_node_form"){
  78. // dsm($form, '$form');
  79. // dsm($form_state, '$form_state');
  80. $ref = $form['field_reference_materio']['und'][0]['value']['#default_value'];
  81. if($ref != ''){
  82. $title = t('Edit').' '.$form['title']['#default_value'].' ('.$form['type']['#value'].' '.$ref.')';
  83. // dsm($title, '$title');
  84. drupal_set_title($title);
  85. $form['reference'] = array(
  86. '#markup' => t('Materio Reference').' : <span>'.$ref.'</span>',
  87. '#prefix' => '<div class="form-item materio-ref">',
  88. '#suffix' => '</div>',
  89. );
  90. $form['reference']['#placement'] = array(
  91. 'region' => 'right',
  92. 'weight' => 0,
  93. 'has_required' => FALSE,
  94. 'hidden' => FALSE,
  95. );
  96. // dsm(synonyms_get_synonyms(4346), 'synonyms_get_synonyms');
  97. // dsm(synonyms_get_synonym_root('fenouille'), 'synonyms_get_synonym_root');
  98. //dsm(synonyms_node_update_index($form['#node']));
  99. }
  100. }
  101. }