public function ParagraphCloneForm::save in Paragraphs Edit 8
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
ParagraphCloneForm.php, line 164
Class
Namespace
Drupal\paragraphs_editCode
public function save(array $form, FormStateInterface $form_state) {
$route_match = $this
->getRouteMatch();
/** @var \Drupal\node\NodeInterface $node */
$node = $route_match
->getParameter('node');
$field_name = $route_match
->getParameter('field');
$field = $node
->get($field_name);
$field_definition = $field
->getFieldDefinition();
$field_label = $field_definition
->getLabel();
$delta = $route_match
->getParameter('delta');
$destination_entity_id = $form_state
->getValue([
'paragraphs_edit',
'parent',
]);
/** @var \Drupal\Core\Entity\FieldableEntityInterface $destination_entity */
$destination_entity = $this->entityManager
->getStorage('node')
->load($destination_entity_id);
$destination_field = $form_state
->getValue([
'paragraphs_edit',
'field',
]);
$this->entity
->save();
$destination_entity
->get($destination_field)
->appendItem([
'target_id' => $this->entity
->id(),
'target_revision_id' => $this->entity
->getRevisionId(),
]);
//$destination->get($destination_field)->appendItem($this->entity);
$destination_entity
->save();
drupal_set_message($this
->t('Cloned #@delta of @field of %label to %destination_label', [
'@delta' => $delta,
'@field' => $field_label,
'%label' => $node
->label(),
'%destination_label' => $destination_entity
->label(),
]));
$request = \Drupal::request();
if ($request->query
->has('destination')) {
$request->query
->remove('destination');
}
$form_state
->setRedirectUrl($destination_entity
->toUrl());
}