diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 972c40d..0b32fd1 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -391,11 +391,30 @@ class InlineParagraphsWidget extends WidgetBase { '#paragraphs_mode' => 'edit', ); + $links['duplicate_button'] = array( + '#type' => 'submit', + '#value' => $this->t('Duplicate'), + '#name' => strtr($id_prefix, '-', '_') . '_duplicate', + '#weight' => 502, + '#submit' => array(array(get_class($this), 'duplicateSubmit')), + '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), + '#delta' => $delta, + '#ajax' => array( + 'callback' => array(get_class($this), 'itemAjax'), + 'wrapper' => $widget_state['ajax_wrapper_id'], + 'effect' => 'fade', + ), + '#access' => $paragraphs_entity->access('update'), + '#prefix' => '
  • ', + '#suffix' => '
  • ', + '#paragraphs_mode' => 'duplicate', + ); + $links['remove_button'] = array( '#type' => 'submit', '#value' => $this->t('Remove'), '#name' => strtr($id_prefix, '-', '_') . '_remove', - '#weight' => 502, + '#weight' => 503, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, @@ -978,6 +997,58 @@ class InlineParagraphsWidget extends WidgetBase { $form_state->setRebuild(); } + public function createDuplicateParagraph() { + //Check if the paragraph is nested and call duplicate on its entities. + if ($this->getType() == 'nested_paragraph') { + foreach ($this->getFields() as $type=>$field) { + if ($field->getFieldDefinition()->getType() == 'entity_reference_revisions') { + if ($field->getFieldDefinition()->getTargetEntityTypeId() == "paragraph") { + foreach ($field as $item=>$entity) { + $item->$entity = $entity->createDuplicateParagraph(); + } + } + } + } + } + $duplicate = clone $this; + $entity_type = $this->getEntityType(); + // Reset the entity ID and indicate that this is a new entity. + $duplicate->{$entity_type->getKey('id')} = NULL; + $duplicate->enforceIsNew(); + + // Check if the entity type supports UUIDs and generate a new one if so. + if ($entity_type->hasKey('uuid')) { + $duplicate->{$entity_type->getKey('uuid')} = $this->uuidGenerator()->generate(); + } + + return $duplicate; + } + + public static function duplicateSubmit(array $form, FormStateInterface $form_state) { + $button = $form_state->getTriggeringElement(); + // Go one level up in the form, to the widgets container. + $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -4)); + $field_name = $element['#field_name']; + $parents = $element['#field_parents']; + + // Inserting new element in the array. + $field_state = static::getWidgetState($parents, $field_name, $form_state); + $delta = $button['#delta']; + $field_state['items_count']++; + $field_state['real_item_count']++; + $field_state['original_deltas'] = array_merge($field_state['original_deltas'], ['1' => 1]) ; + + $paragraph[] = [ + 'entity' => $field_state['paragraphs'][$delta]['entity']->createDuplicateParagraph(), + 'display' => $field_state['paragraphs'][$delta]['display'], + 'mode' => 'closed' + ]; + array_splice($field_state['paragraphs'], $delta + 1, 0, $paragraph); + + static::setWidgetState($parents, $field_name, $form_state, $field_state); + $form_state->setRebuild(); + } + public static function paragraphsItemSubmit(array $form, FormStateInterface $form_state) { $button = $form_state->getTriggeringElement();