Using hook_form_alter I have added a custom field in my node creation form
function custom_assignments_form_alter(&$form, $form_state, $form_id) {
//print_r($form_id);exit;
if($form_id == 'node_assignment_form'){
$lecture_options = array();
$lecture_values = db_query("SELECT nid, title FROM `node_field_data` WHERE nid IN (SELECT entity_id FROM
`node__field_module` WHERE bundle='lectures' AND field_module_target_id=:tid)",
array(':tid' => $selectedWeek))->fetchAll();
foreach($lecture_values AS $key => $value){
$lecture_options[$value->nid] = $value->title;
}
//print("<pre>");print_r($lecture_options);exit;
$form['field_after_lecture']['#access'] = FALSE;
$form['field_after_lecture_tmp'] = array(
'#title' => t('After Lecture'),
'#required' => true,
'#type' => 'select',
'#options' => $lecture_options,
//'#default_value' => print_r($form_state->getBuildInfo()['args']);exit;,
'#weight' => 100,
'#prefix' => '<div id="custom">',
'#suffix' => '</div>'
);
}}
How to get the value of field_after_lecture_tmp in hook_node_presave
I printed the node fields using $node->toArray()
function. But this field is not there. In Drupal 7 I was able to access it.