QuickNodeCloneNodeForm.php in Quick Node Clone 8
File
src/Form/QuickNodeCloneNodeForm.php
View source
<?php
namespace Drupal\quick_node_clone\Form;
use Drupal\node\NodeForm;
use Drupal\Core\Form\FormStateInterface;
class QuickNodeCloneNodeForm extends NodeForm {
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$clone_string = $this
->t('New Clone:');
if (!empty($element['publish']['#value'])) {
$element['publish']['#value'] = $clone_string . ' ' . $element['publish']['#value'];
}
if (!empty($element['unpublish']['#value'])) {
$element['unpublish']['#value'] = $clone_string . ' ' . $element['unpublish']['#value'];
}
return $element;
}
public function save(array $form, FormStateInterface $form_state) {
$node = $this->entity;
$insert = $node
->isNew();
$node
->save();
$node_link = $node
->toLink($this
->t('View'))
->toString();
$context = [
'@type' => $node
->getType(),
'%title' => $node
->label(),
'link' => $node_link,
];
$t_args = [
'@type' => node_get_type_label($node),
'%title' => $node
->label(),
];
if ($insert) {
$this
->logger('content')
->notice('@type: added %title (clone).', $context);
$this
->messenger()
->addMessage($this
->t('@type %title (clone) has been created.', $t_args));
}
if ($node
->id()) {
$form_state
->setValue('nid', $node
->id());
$form_state
->set('nid', $node
->id());
$storage = $form_state
->getStorage();
foreach ($storage['quick_node_clone_groups_storage'] as $group) {
$group
->addContent($node, "group_node:" . $node
->bundle());
}
if ($node
->access('view')) {
$form_state
->setRedirect('entity.node.canonical', [
'node' => $node
->id(),
]);
}
else {
$form_state
->setRedirect('<front>');
}
}
else {
$this
->messenger()
->addError($this
->t('The cloned post could not be saved.'));
$form_state
->setRebuild();
}
}
}