I have one paragraph type of Product. and I have to add one field to paragraph product programmatically, how can I achive this.
function my_module_field_widget_paragraphs_form_alter(&$element, FormStateInterface &$form_state, $context){ if ($element['#paragraph_type'] == 'products') { Drupal::logger('my_module')->info('debug'); <!-- add field here --> } }
and other answer I found is
use DrupalparagraphsEntityParagraph; $paragraph = Paragraph::load($paragraph_id); $paragraph->field_image[] = [ 'target_id' => $fid, 'alt' => $alt, 'title' => $title, ]; $paragraph->save();
But in which function or preprocess I need to add above code?