protected function ParagraphsWidget::getChildParagraphs in Paragraphs 8
Returns a list of child paragraphs for a given field to loop over.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
string $field_name: The field name for which to find child paragraphs.
\Drupal\paragraphs\ParagraphInterface $paragraph: The current paragraph.
array $array_parents: The current field parent structure.
Return value
\Drupal\paragraphs\Entity\Paragraph[] Child paragraphs.
1 call to ParagraphsWidget::getChildParagraphs()
- ParagraphsWidget::buildNestedParagraphsFoDragDrop in src/
Plugin/ Field/ FieldWidget/ ParagraphsWidget.php - Builds the nested drag and drop structure.
File
- src/
Plugin/ Field/ FieldWidget/ ParagraphsWidget.php, line 1254
Class
- ParagraphsWidget
- Plugin implementation of the 'entity_reference_revisions paragraphs' widget.
Namespace
Drupal\paragraphs\Plugin\Field\FieldWidgetCode
protected function getChildParagraphs(FormStateInterface $form_state, $field_name, ParagraphInterface $paragraph = NULL, array $array_parents = []) {
// Convert the parents structure which only includes field names and delta
// to the full storage array key which includes a prefix and a subform.
$full_parents_key = [
'field_storage',
'#parents',
];
foreach ($array_parents as $i => $parent) {
$full_parents_key[] = $parent;
if ($i % 2) {
$full_parents_key[] = 'subform';
}
}
$current_parents = array_merge($full_parents_key, [
'#fields',
$field_name,
]);
$child_field_state = NestedArray::getValue($form_state
->getStorage(), $current_parents);
$entities = [];
if ($child_field_state && isset($child_field_state['paragraphs'])) {
// Fetch the paragraphs from the field state. Use the original delta
// to get the right position. Also reorder the paragraphs in the widget
// state accordingly.
$new_widget_paragraphs = [];
foreach ($child_field_state['paragraphs'] as $child_delta => $child_field_item_state) {
$entities[array_search($child_delta, $child_field_state['original_deltas'])] = $child_field_item_state['entity'];
$new_widget_paragraphs[array_search($child_delta, $child_field_state['original_deltas'])] = $child_field_item_state;
}
ksort($entities);
// Set the orderd paragraphs into the widget state and reset original
// deltas.
ksort($new_widget_paragraphs);
$child_field_state['paragraphs'] = $new_widget_paragraphs;
$child_field_state['original_deltas'] = range(0, count($child_field_state['paragraphs']) - 1);
NestedArray::setValue($form_state
->getStorage(), $current_parents, $child_field_state);
}
elseif ($paragraph) {
// If there is no field state, return the paragraphs directly from the
// entity.
foreach ($paragraph
->get($field_name) as $child_delta => $item) {
if ($item->entity) {
$entities[$child_delta] = $item->entity;
}
}
}
return $entities;
}