protected function ParagraphsWidget::buildNestedParagraphsFoDragDrop in Paragraphs 8
Builds the nested drag and drop structure.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
\Drupal\paragraphs\ParagraphInterface|null $paragraph: The parent paragraph, NULL for the initial call.
string[] $array_parents: The array parents for nested paragraphs.
Return value
array The built form structure.
1 call to ParagraphsWidget::buildNestedParagraphsFoDragDrop()
- ParagraphsWidget::formMultipleElements in src/
Plugin/ Field/ FieldWidget/ ParagraphsWidget.php - Special handling to create form elements for multiple values.
File
- src/
Plugin/ Field/ FieldWidget/ ParagraphsWidget.php, line 1313
Class
- ParagraphsWidget
- Plugin implementation of the 'entity_reference_revisions paragraphs' widget.
Namespace
Drupal\paragraphs\Plugin\Field\FieldWidgetCode
protected function buildNestedParagraphsFoDragDrop(FormStateInterface $form_state, ParagraphInterface $paragraph = NULL, array $array_parents = []) {
// Look for nested elements.
$elements = [];
$field_definitions = [];
if ($paragraph) {
foreach ($paragraph
->getFieldDefinitions() as $child_field_name => $field_definition) {
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
if ($field_definition
->getType() == 'entity_reference_revisions' && $field_definition
->getSetting('target_type') == 'paragraph') {
$field_definitions[$child_field_name] = $field_definition;
}
}
}
else {
$field_definitions = [
$this->fieldDefinition
->getName() => $this->fieldDefinition,
];
}
/** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
foreach ($field_definitions as $child_field_name => $field_definition) {
$child_path = implode('][', array_merge($array_parents, [
$child_field_name,
]));
$cardinality = $field_definition
->getFieldStorageDefinition()
->getCardinality();
$allowed_types = implode(',', array_keys($this
->getAllowedTypes($field_definition)));
$elements[$child_field_name] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'paragraphs-dragdrop-wrapper',
],
],
];
// Only show a field label if there is more than one paragraph field.
$label = count($field_definitions) > 1 || !$paragraph ? '<label><strong class="paragraphs-dragdrop__label paragraphs-dragdrop__label--field">' . $field_definition
->getLabel() . '</strong></label>' : '';
$elements[$child_field_name]['list'] = [
'#type' => 'markup',
'#prefix' => $label . '<ul class="paragraphs-dragdrop__list" data-paragraphs-dragdrop-cardinality="' . $cardinality . '" data-paragraphs-dragdrop-allowed-types="' . $allowed_types . '" data-paragraphs-dragdrop-path="' . $child_path . '">',
'#suffix' => '</ul>',
];
/** @var \Drupal\paragraphs\Entity\Paragraph $child_paragraph */
foreach ($this
->getChildParagraphs($form_state, $child_field_name, $paragraph, $array_parents) as $child_delta => $child_paragraph) {
$element = [];
$element['top'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'paragraphs-summary-wrapper',
],
],
];
$element['top']['paragraph_summary']['type'] = [
'#markup' => '<strong class="paragraphs-dragdrop__label paragraphs-dragdrop__label--bundle">' . $child_paragraph
->getParagraphType()
->label() . '</strong>',
];
// We name the element '_weight' to avoid clashing with elements
// defined by widget.
$element['_weight'] = array(
'#type' => 'hidden',
'#default_value' => $child_delta,
'#attributes' => [
'class' => [
'paragraphs-dragdrop__weight',
],
],
);
$element['_path'] = [
'#type' => 'hidden',
'#title' => $this
->t('Current path for @number', [
'@number' => $delta = 1,
]),
'#title_display' => 'invisible',
'#default_value' => $child_path,
'#attributes' => [
'class' => [
'paragraphs-dragdrop__path',
],
],
];
$summary_options = [];
$element['#prefix'] = '<li class="paragraphs-dragdrop__item" data-paragraphs-dragdrop-bundle="' . $child_paragraph
->bundle() . '"><a href="#" class="paragraphs-dragdrop__handle"><span class="paragraphs-dragdrop__icon"></span></a>';
$element['#suffix'] = '</li>';
$child_array_parents = array_merge($array_parents, [
$child_field_name,
$child_delta,
]);
if ($child_elements = $this
->buildNestedParagraphsFoDragDrop($form_state, $child_paragraph, $child_array_parents)) {
$element['dragdrop'] = $child_elements;
// Set the depth limit to 0 to avoid displaying a summary for the
// children.
$summary_options['depth_limit'] = 1;
}
$element['top']['summary']['fields_info'] = [
'#theme' => 'paragraphs_summary',
'#summary' => $child_paragraph
->getSummaryItems($summary_options),
'#expanded' => FALSE,
'#access' => $child_paragraph
->access('update') || $child_paragraph
->access('view'),
];
$info = $child_paragraph
->getIcons();
if (isset($info['count'])) {
$element['top']['icons']['count'] = $info['count'];
}
$elements[$child_field_name]['list'][$child_delta] = $element;
}
}
return $elements;
}