I have a paragraphs field that takes a term reference.
I need to count how many terms there are for a specific vocabulary and then create that many instance of the paragraphs multiple value field for when creating a node of a certain type. Can this be done with Hook_form_alter ?
It is important that these fields are loaded when the node creation form is loaded rather than the user having to click add more
Any help would be greatly appreciated
I have tried this but when saving the node it doesn't save the new fields
function hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'my_form') {
$item = $form['field_features']['widget']['0'];
// get taxonomy terms
$query = \Drupal::entityQuery('taxonomy_term');
$query->condition('vid', "my_voc");
$tids = $query->execute();
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);
$count = count($terms);
$i = '1';
foreach($terms as $term){
$form['field_features_para']['widget'][$i] = $item;
$form['field_features_para']['widget'][$i]['#delta'] = $i;
$form['field_features_para']['widget'][$i]['#weight'] = $i;
$i ++;
}
}
}