You are here

public function GeysirCutPasteController::paste in Geysir 8

Paste a paragraph entity before/after another paragraph on the given delta.

1 string reference to 'GeysirCutPasteController::paste'
geysir.routing.yml in ./geysir.routing.yml
geysir.routing.yml

File

src/Controller/GeysirCutPasteController.php, line 40

Class

GeysirCutPasteController
Controller for cut & paste functionality.

Namespace

Drupal\geysir\Controller

Code

public function paste($parent_entity_type, $parent_entity_bundle, $parent_entity_revision, $field, $field_wrapper_id, $delta, $position, $paragraph_to_paste, $paragraph_revision, $js = 'nojs') {
  if ($js == 'ajax') {

    // Get the parent revision if available, otherwise the parent.
    $parent_entity_revision = $this
      ->getParentRevisionOrParent($parent_entity_type, $parent_entity_revision);
    $list_items = $parent_entity_revision
      ->get($field)
      ->getIterator();
    for ($index = $parent_entity_revision
      ->get($field)
      ->count() - 1; $index >= 0; $index--) {
      $parent_entity_revision
        ->get($field)
        ->removeItem($index);
    }
    foreach ($list_items as $item_delta => $item) {
      $item_value = $item
        ->getValue();

      // Prevent the "paragraph to paste" being added again.
      $prevent_adding_twice = $paragraph_to_paste
        ->id() !== $item_value['target_id'] ? true : false;
      if ($item_delta == $delta) {
        $paste_value = [
          'target_id' => $paragraph_to_paste
            ->id(),
          'target_revision_id' => $paragraph_to_paste
            ->getRevisionId(),
        ];
        if ($position == 'before') {
          $parent_entity_revision
            ->get($field)
            ->appendItem($paste_value);
          if ($prevent_adding_twice) {
            $parent_entity_revision
              ->get($field)
              ->appendItem($item_value);
          }
        }
        else {
          if ($prevent_adding_twice) {
            $parent_entity_revision
              ->get($field)
              ->appendItem($item_value);
          }
          $parent_entity_revision
            ->get($field)
            ->appendItem($paste_value);
        }
      }
      else {
        if ($prevent_adding_twice) {
          $parent_entity_revision
            ->get($field)
            ->appendItem($item_value);
        }
      }
    }
    $parent_entity_revision
      ->save();
    $response = new AjaxResponse();
    $response
      ->addCommand(new ReplaceCommand(sprintf('[data-geysir-field-paragraph-field-wrapper="%s"]', $field_wrapper_id), $parent_entity_revision
      ->get($field)
      ->view('default')));
    $response
      ->addCommand(new GeysirReattachBehaviors());
    return $response;
  }
  return $this
    ->t('Javascript is required for this functionality to work properly.');
}