materio_id.module 2.8 KB

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