completed computed field_reference dev
This commit is contained in:
@@ -2,62 +2,36 @@
|
||||
|
||||
|
||||
function computed_field_field_index_compute($entity_type_manager, $entity, $fields, $delta){
|
||||
// drush_print($entity->field_index->value);
|
||||
// ksm($entity_type_manager);
|
||||
// ksm($entity);
|
||||
// ksm($fields);
|
||||
// ksm($delta);
|
||||
$famille = $entity->get('field_famille')->getValue()[0]['value'];
|
||||
|
||||
// $entity->isNew(); ??
|
||||
// TODO: what if familly change ?
|
||||
if (!empty($entity->field_index->value)) { // the node is not new
|
||||
return $entity->field_index->value;
|
||||
}
|
||||
|
||||
// TODO: finish the job
|
||||
/* FROM D7
|
||||
|
||||
// dsm('-- computed --');
|
||||
// dsm($entity_field, '$entity_field');
|
||||
// dsm($entity_type, '$entity_type');
|
||||
// dsm($entity, '$entity');
|
||||
// dsm($field, '$field');
|
||||
// dsm($instance, '$instance');
|
||||
// dsm($items, '$items');
|
||||
|
||||
// dsm($entity_field[0]['value'], 'entity_field value');
|
||||
|
||||
if (!empty($entity_field[0]['value'])) { // the node is not new
|
||||
|
||||
return $entity_field[0]['value'];
|
||||
|
||||
// the node is new
|
||||
// get all same entities in same family
|
||||
$query = \Drupal::entityQuery($entity->getEntityTypeId())
|
||||
->condition('type', $entity->bundle())
|
||||
->condition('field_famille', $famille)
|
||||
->sort('field_index', 'DESC')
|
||||
->range(0,1);
|
||||
|
||||
$nids = $query->execute();
|
||||
|
||||
if(is_array($nids)){
|
||||
$last = entity_load($entity->getEntityTypeId(), array_pop($nids));
|
||||
// get identifiants fo these entities
|
||||
$index = $last->get('field_index')->getValue()[0]['value'] + 1;
|
||||
}else{
|
||||
$index = '1';
|
||||
}
|
||||
else { // the node is new
|
||||
// get all same entities in same family
|
||||
$query = new EntityFieldQuery();
|
||||
$query
|
||||
->entityCondition('entity_type', $entity_type)
|
||||
->entityCondition('bundle', $entity->type)
|
||||
->fieldCondition('field_famille', 'value', $entity->field_famille['und'][0]['value']);
|
||||
$result = $query->execute();
|
||||
if(is_array($result[$entity_type])){
|
||||
$entities = entity_load($entity_type, array_keys($result[$entity_type]));
|
||||
|
||||
// get identifiants fo these entities
|
||||
$ids = array();
|
||||
foreach ($entities as $id => $e) {
|
||||
$identifiant = field_view_field($entity_type, $e, 'field_identifiant');
|
||||
$ids[] = $identifiant[0]['#markup'] ? intval($identifiant[0]['#markup']) : 0;
|
||||
}
|
||||
sort($ids);
|
||||
|
||||
$value = strval(array_pop($ids) + 1);
|
||||
}else{
|
||||
$value = '1';
|
||||
}
|
||||
|
||||
// parse the value to a string as 0023 or 4458 or 0001
|
||||
$value_parts = str_split($value);
|
||||
while (count($value_parts) < 4) {
|
||||
array_unshift($value_parts, "0");
|
||||
}
|
||||
|
||||
// record the result
|
||||
$entity_field[0]['value'] = implode('', $value_parts);
|
||||
}
|
||||
*/
|
||||
return $index;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,26 +43,32 @@ function computed_field_field_reference_compute($entity_type_manager, $entity, $
|
||||
if (!empty($entity->field_reference->value)) { // the node is not new
|
||||
return $entity->field_reference->value;
|
||||
}
|
||||
|
||||
// TODO: finish the job
|
||||
/* FROM D7
|
||||
|
||||
// dsm('-- computed --');
|
||||
// dsm($entity_field, '$entity_field');
|
||||
// dsm($entity_type, '$entity_type');
|
||||
// dsm($entity, '$entity');
|
||||
// dsm($field, '$field');
|
||||
// dsm($instance, '$instance');
|
||||
// dsm($items, '$items');
|
||||
if (!empty($entity_field[0]['value'])) { // the node is not new
|
||||
return $entity_field[0]['value'];
|
||||
// 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");
|
||||
}
|
||||
else { // the node is new
|
||||
$entity_field[0]['value'] = $entity->field_famille['und'][0]['value'].'-'.$entity->field_identifiant['und'][0]['value'];
|
||||
}
|
||||
*/
|
||||
return $entity->field_famille->value.'-'.implode('', $index_parts);
|
||||
}
|
||||
|
||||
// function computed_field_field_reference_materio_display($field, $entity_field_item, $entity_lang = "en", $langcode = "en") {
|
||||
// return $entity_field_item['value'];
|
||||
// }
|
||||
|
||||
/**
|
||||
* 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);
|
||||
$node = $form_state->getFormObject()->getEntity();
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user