2018-12-11 15:11:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
function computed_field_field_index_compute($entity_type_manager, $entity, $fields, $delta){
|
2019-06-22 19:34:21 +02:00
|
|
|
// ksm($entity_type_manager);
|
|
|
|
// ksm($entity);
|
|
|
|
// ksm($fields);
|
|
|
|
// ksm($delta);
|
|
|
|
$famille = $entity->get('field_famille')->getValue()[0]['value'];
|
2020-11-17 11:23:03 +01:00
|
|
|
|
2019-06-22 19:34:21 +02:00
|
|
|
// $entity->isNew(); ??
|
|
|
|
// TODO: what if familly change ?
|
2018-12-17 17:31:28 +01:00
|
|
|
if (!empty($entity->field_index->value)) { // the node is not new
|
|
|
|
return $entity->field_index->value;
|
|
|
|
}
|
2020-11-17 11:23:03 +01:00
|
|
|
|
2019-06-22 19:34:21 +02:00
|
|
|
// the node is new
|
|
|
|
// get all same entities in same family
|
2021-08-02 18:48:15 +02:00
|
|
|
$bundle = $entity->bundle();
|
|
|
|
$entity_type_id = $entity->getEntityTypeId();
|
2019-06-22 19:34:21 +02:00
|
|
|
$query = \Drupal::entityQuery($entity->getEntityTypeId())
|
|
|
|
->condition('type', $entity->bundle())
|
|
|
|
->condition('field_famille', $famille)
|
|
|
|
->sort('field_index', 'DESC')
|
2024-08-20 19:47:47 +02:00
|
|
|
->accessCheck(FALSE)
|
2019-06-22 19:34:21 +02:00
|
|
|
->range(0,1);
|
2020-11-17 11:23:03 +01:00
|
|
|
|
2019-06-22 19:34:21 +02:00
|
|
|
$nids = $query->execute();
|
2020-11-17 11:23:03 +01:00
|
|
|
|
|
|
|
$index = null;
|
2019-06-22 19:34:21 +02:00
|
|
|
if(is_array($nids)){
|
2020-11-17 11:23:03 +01:00
|
|
|
$nid = array_pop($nids);
|
|
|
|
if ($nid) {
|
2022-02-22 15:25:58 +01:00
|
|
|
// $last = entity_load($entity->getEntityTypeId(), $nid); // array_pop($nids)
|
|
|
|
$last = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->load($nid);
|
2020-11-17 11:23:03 +01:00
|
|
|
// get identifiants fo these entities
|
|
|
|
$index = $last->get('field_index')->getValue()[0]['value'] + 1;
|
|
|
|
}
|
2018-12-11 15:11:11 +01:00
|
|
|
}
|
2020-11-17 11:23:03 +01:00
|
|
|
// if(!$index){
|
|
|
|
// $index = '1';
|
|
|
|
// }
|
|
|
|
return $index ? $index : '1';
|
2018-12-11 15:11:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// function computed_field_field_identifiant_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
|
|
|
|
// return $entity_field_item['value'];
|
|
|
|
// }
|
|
|
|
|
|
|
|
function computed_field_field_reference_compute($entity_type_manager, $entity, $fields, $delta) {
|
2018-12-17 17:31:28 +01:00
|
|
|
if (!empty($entity->field_reference->value)) { // the node is not new
|
|
|
|
return $entity->field_reference->value;
|
|
|
|
}
|
2019-06-22 19:34:21 +02:00
|
|
|
// the node is new
|
|
|
|
// parse the value to a string as 0023 or 4458 or 0001
|
|
|
|
$index_parts = str_split($entity->field_index->value);
|
|
|
|
while (count($index_parts) < 4) {
|
|
|
|
array_unshift($index_parts, "0");
|
2018-12-11 15:11:11 +01:00
|
|
|
}
|
2021-02-15 15:30:24 +01:00
|
|
|
return $entity->field_famille->value.implode('', $index_parts);
|
2018-12-11 15:11:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
|
|
|
|
// return $entity_field_item['value'];
|
|
|
|
// }
|
2019-06-22 19:34:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_form_alter().
|
|
|
|
*/
|
|
|
|
function materio_id_form_node_materiau_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
|
|
|
|
// dsm($form_id);
|
2019-07-05 12:52:16 +02:00
|
|
|
// ksm($form);
|
2019-06-22 19:34:21 +02:00
|
|
|
$node = $form_state->getFormObject()->getEntity();
|
2019-07-05 12:52:16 +02:00
|
|
|
|
|
|
|
// lock the family filed if already filled
|
|
|
|
if(isset($form['field_famille']) && $family = $node->field_famille->value){
|
|
|
|
// dsm($family);
|
|
|
|
$form['field_famille']['widget']['#disabled'] = true;
|
|
|
|
}
|
2020-11-17 11:23:03 +01:00
|
|
|
|
2019-07-05 12:52:16 +02:00
|
|
|
// adding reference to the form title
|
2019-06-22 19:34:21 +02:00
|
|
|
$ref = $node->field_reference->value;
|
|
|
|
// dsm($ref);
|
|
|
|
if($ref != ''){
|
|
|
|
$string = $form['#title']->getUntranslatedString() . ' (@ref)';
|
|
|
|
// dsm($string);
|
|
|
|
$args = $form['#title']->getArguments() + array('@ref'=>$ref);
|
|
|
|
// ksm($args);
|
|
|
|
$form['#title'] = t($string, $args);
|
|
|
|
}
|
|
|
|
}
|