You are here

public function ParagraphsFrontendUIController::up 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::up()

Shift up a single paragraph.

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

File

src/Controller/ParagraphsFrontendUIController.php, line 21

Class

ParagraphsFrontendUIController
Controller for up and down actions.

Namespace

Drupal\paragraphs_frontend_ui\Controller

Code

public function up($paragraph, $js = 'nojs') {
  $paragraph = $paragraph
    ->getTranslation($this
    ->langcode());
  extract($this
    ->getParentData($paragraph));
  $paragraph_items = $parent->{$parent_field_name}
    ->getValue();
  foreach ($paragraph_items as $delta => $paragraph_item) {
    if ($paragraph_item['target_id'] == $paragraph
      ->id()) {
      if ($delta > 0) {
        $prev_paragraph = $paragraph_items[$delta - 1];
        $paragraph_items[$delta - 1] = $paragraph_items[$delta];
        $paragraph_items[$delta] = $prev_paragraph;
      }
      break;
    }
  }
  $parent->{$parent_field_name}
    ->setValue($paragraph_items);
  $parent
    ->save();
  return $this
    ->refreshWithAJaxResponse($parent, $parent_field_name);
}