materio_id.module 2.7 KB

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