materio_id.module 2.6 KB

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