materio_id.module 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ->accessCheck(FALSE)
  22. ->range(0,1);
  23. $nids = $query->execute();
  24. $index = null;
  25. if(is_array($nids)){
  26. $nid = array_pop($nids);
  27. if ($nid) {
  28. // $last = entity_load($entity->getEntityTypeId(), $nid); // array_pop($nids)
  29. $last = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($nid);
  30. // get identifiants fo these entities
  31. $index = $last->get('field_index')->getValue()[0]['value'] + 1;
  32. }
  33. }
  34. // if(!$index){
  35. // $index = '1';
  36. // }
  37. return $index ? $index : '1';
  38. }
  39. // function computed_field_field_identifiant_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  40. // return $entity_field_item['value'];
  41. // }
  42. function computed_field_field_reference_compute($entity_type_manager, $entity, $fields, $delta) {
  43. if (!empty($entity->field_reference->value)) { // the node is not new
  44. return $entity->field_reference->value;
  45. }
  46. // the node is new
  47. // parse the value to a string as 0023 or 4458 or 0001
  48. $index_parts = str_split($entity->field_index->value);
  49. while (count($index_parts) < 4) {
  50. array_unshift($index_parts, "0");
  51. }
  52. return $entity->field_famille->value.implode('', $index_parts);
  53. }
  54. // function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
  55. // return $entity_field_item['value'];
  56. // }
  57. /**
  58. * Implements hook_form_alter().
  59. */
  60. function materio_id_form_node_materiau_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  61. // dsm($form_id);
  62. // ksm($form);
  63. $node = $form_state->getFormObject()->getEntity();
  64. // lock the family filed if already filled
  65. if(isset($form['field_famille']) && $family = $node->field_famille->value){
  66. // dsm($family);
  67. $form['field_famille']['widget']['#disabled'] = true;
  68. }
  69. // adding reference to the form title
  70. $ref = $node->field_reference->value;
  71. // dsm($ref);
  72. if($ref != ''){
  73. $string = $form['#title']->getUntranslatedString() . ' (@ref)';
  74. // dsm($string);
  75. $args = $form['#title']->getArguments() + array('@ref'=>$ref);
  76. // ksm($args);
  77. $form['#title'] = t($string, $args);
  78. }
  79. }