You are here

public function ParagraphsFrontendUIController::duplicate in Paragraphs frontend ui 8

Same name and namespace in other branches
  1. 8.2 src/Controller/ParagraphsFrontendUIController.php \Drupal\paragraphs_frontend_ui\Controller\ParagraphsFrontendUIController::duplicate()

Duplicate a paragraph.

1 string reference to 'ParagraphsFrontendUIController::duplicate'
paragraphs_frontend_ui.routing.yml in ./paragraphs_frontend_ui.routing.yml
paragraphs_frontend_ui.routing.yml

File

src/Controller/ParagraphsFrontendUIController.php, line 70

Class

ParagraphsFrontendUIController
Controller for up and down actions.

Namespace

Drupal\paragraphs_frontend_ui\Controller

Code

public function duplicate($paragraph, $js = 'nojs') {
  $paragraph
    ->getTranslation($this
    ->langcode());
  extract($this
    ->getParentData($paragraph));
  $paragraph_items = $parent->{$parent_field_name}
    ->getValue();
  $paragraphs_new = [];
  foreach ($paragraph_items as $delta => $paragraph_item) {
    $paragraphs_new[] = $paragraph_item;
    if ($paragraph_item['target_id'] == $paragraph
      ->id()) {
      $cloned_paragraph = $paragraph
        ->createDuplicate();
      $cloned_paragraph
        ->save();
      $paragraphs_new[] = [
        'target_id' => $cloned_paragraph
          ->id(),
        'target_revision_id' => $cloned_paragraph
          ->getRevisionId(),
      ];
    }
  }
  $parent->{$parent_field_name}
    ->setValue($paragraphs_new);
  $parent
    ->save();
  return $this
    ->refreshWithAJaxResponse($parent, $parent_field_name);
}