Score:0

Open a paragraph programmatically

nl flag

I have a paragraph contact nested inside another paragraph occurrence.

In the widget settings the paragraph contact is defined to be collapsed by default (edit_mode = closed).

I want the contact paragraph only to be open when creating the content and only on the first occurrence.

How can this be achieved?

Thanks in advance.

id flag
Could you please modify the question to explain the precise meaning of "open" and "closed" in this context?
sonfd avatar
in flag
Do you just need to define a default paragraph bundle on the form display settings for the field?
abelass avatar
nl flag
@cilefen with close I mean collapsed an open, the contrary uncollapsed. I edited my question
Score:0
nl flag

I achieved what I wanted to to by letting the contact field open by default (edit_mode = open) and then in an hook_form_node_form_alter remove the contact form with this code

// Collapse contact field except for the first occurence.
foreach ($form['field_occurence']['widget'] as $delta => $field) {
  if (is_int($delta) && $delta != 0 && isset($field['subform']['field_contact']['widget']['0']['subform']['#type'])) {
    unset($form['field_occurence']['widget'][$delta]['subform']['field_contact']['widget']['0']);
  }
}

I guess there is a cleaner way but form the moment that does the trick.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.