materio_id.module 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. function computed_field_field_index_compute($entity_type_manager, $entity, $fields, $delta){
  3. // ksm($entity_type_manager);
  4. // ksm($entity);
  5. // ksm($fields);
  6. // ksm($delta);
  7. $famille = $entity->get('field_famille')->getValue()[0]['value'];
  8. // $entity->isNew(); ??
  9. // TODO: what if familly change ?
  10. if (!empty($entity->field_index->value)) { // the node is not new
  11. return $entity->field_index->value;
  12. }
  13. // the node is new
  14. // get all same entities in same family
  15. $bundle = $entity->bundle();
  16. $entity_type_id = $entity->getEntityTypeId();
  17. $query = \Drupal::entityQuery($entity->getEntityTypeId())
  18. ->condition('type', $entity->bundle())
  19. ->condition('field_famille', $famille)
  20. ->sort('field_index', 'DESC')
  21. ->range(0,1);
  22. $nids = $query->execute();
  23. $index = null;
  24. if(is_array($nids)){
  25. $nid = array_pop($nids);
  26. if ($nid) {
  27. $last = entity_load($entity->getEntityTypeId(), $nid); // array_pop($nids)
  28. // get identifiants fo these entities
  29. $index = $last->get('field_index')->getValue()[0]['value'] + 1;
  30. }
  31. }
  32. // if(!$index){
  33. // $index = '1';
  34. // }
  35. return $index ? $index : '1';
  36. }
  37. // function computed_field_field_identifiant_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  38. // return $entity_field_item['value'];
  39. // }
  40. function computed_field_field_reference_compute($entity_type_manager, $entity, $fields, $delta) {
  41. if (!empty($entity->field_reference->value)) { // the node is not new
  42. return $entity->field_reference->value;
  43. }
  44. // the node is new
  45. // parse the value to a string as 0023 or 4458 or 0001
  46. $index_parts = str_split($entity->field_index->value);
  47. while (count($index_parts) < 4) {
  48. array_unshift($index_parts, "0");
  49. }
  50. return $entity->field_famille->value.implode('', $index_parts);
  51. }
  52. // function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  53. // return $entity_field_item['value'];
  54. // }
  55. /**
  56. * Implements hook_form_alter().
  57. */
  58. function materio_id_form_node_materiau_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  59. // dsm($form_id);
  60. // ksm($form);
  61. $node = $form_state->getFormObject()->getEntity();
  62. // lock the family filed if already filled
  63. if(isset($form['field_famille']) && $family = $node->field_famille->value){
  64. // dsm($family);
  65. $form['field_famille']['widget']['#disabled'] = true;
  66. }
  67. // adding reference to the form title
  68. $ref = $node->field_reference->value;
  69. // dsm($ref);
  70. if($ref != ''){
  71. $string = $form['#title']->getUntranslatedString() . ' (@ref)';
  72. // dsm($string);
  73. $args = $form['#title']->getArguments() + array('@ref'=>$ref);
  74. // ksm($args);
  75. $form['#title'] = t($string, $args);
  76. }
  77. }