View source
<?php
namespace Drupal\quick_node_clone\Entity;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFormBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupContent;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder {
use StringTranslationTrait;
protected $formBuilder;
protected $entityTypeBundleInfo;
protected $configFactory;
protected $moduleHandler;
protected $entityTypeManager;
protected $currentUser;
protected $privateTempStoreFactory;
protected $stringTranslation;
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.bundle.info'), $container
->get('config.factory'), $container
->get('module_handler'), $container
->get('entity_type.manager'), $container
->get('current_user'), $container
->get('tempstore.private'));
}
public function __construct(FormBuilderInterface $formBuilder, EntityTypeBundleInfoInterface $entityTypeBundleInfo, ConfigFactoryInterface $configFactory, ModuleHandlerInterface $moduleHandler, EntityTypeManagerInterface $entityTypeManager, AccountInterface $currentUser, PrivateTempStoreFactory $privateTempStoreFactory, TranslationInterface $stringTranslation) {
$this->formBuilder = $formBuilder;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->configFactory = $configFactory;
$this->moduleHandler = $moduleHandler;
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->privateTempStoreFactory = $privateTempStoreFactory;
$this->stringTranslation = $stringTranslation;
}
public function getForm(EntityInterface $original_entity, $operation = 'default', array $form_state_additions = []) {
$new_node = $original_entity
->createDuplicate();
$new_node
->set('uid', $this->currentUser
->id());
$new_node
->set('created', time());
$new_node
->set('changed', time());
$new_node
->set('revision_timestamp', time());
$groups = [];
if (\Drupal::moduleHandler()
->moduleExists('gnode')) {
$group_contents = GroupContent::loadByEntity($original_entity);
foreach ($group_contents as $group_content) {
$groups[] = $group_content
->getGroup();
}
}
$form_state_additions['quick_node_clone_groups_storage'] = $groups;
$default_bundle_status = $this->entityTypeManager
->getStorage('node')
->create([
'type' => $new_node
->bundle(),
])->status->value;
foreach ($new_node
->getTranslationLanguages() as $langcode => $language) {
$translated_node = $new_node
->getTranslation($langcode);
$translated_node = $this
->cloneParagraphs($translated_node);
$this->moduleHandler
->alter('cloned_node', $translated_node, $original_entity);
$config_name = 'exclude.node.' . $translated_node
->getType();
if ($exclude_fields = $this
->getConfigSettings($config_name)) {
foreach ($exclude_fields as $field) {
unset($translated_node->{$field});
}
}
$prepend_text = "";
$title_prepend_config = $this
->getConfigSettings('text_to_prepend_to_title');
if (!empty($title_prepend_config)) {
$prepend_text = $title_prepend_config . " ";
}
$clone_status_config = $this
->getConfigSettings('clone_status');
if (!$clone_status_config) {
$key = $translated_node
->getEntityType()
->getKey('published');
$translated_node
->set($key, $default_bundle_status);
}
$translated_node
->setTitle($this
->t('@prepend_text@title', [
'@prepend_text' => $prepend_text,
'@title' => $translated_node
->getTitle(),
], [
'langcode' => $langcode,
]));
}
$form_object = $this->entityTypeManager
->getFormObject($translated_node
->getEntityTypeId(), $operation);
$form_object
->setEntity($translated_node);
$form_state = (new FormState())
->setFormState($form_state_additions);
$new_form = $this->formBuilder
->buildForm($form_object, $form_state);
$tempstore = $this->privateTempStoreFactory
->get('quick_node_clone');
if ($tempstore
->get('address_initial_value_delta') !== NULL) {
$tempstore
->set('address_initial_value_delta', NULL);
}
return $new_form;
}
public function cloneParagraphs(Node $node) {
foreach ($node
->getFieldDefinitions() as $field_definition) {
$field_storage_definition = $field_definition
->getFieldStorageDefinition();
$field_settings = $field_storage_definition
->getSettings();
$field_name = $field_storage_definition
->getName();
if (isset($field_settings['target_type']) && $field_settings['target_type'] == "paragraph") {
if (!$node
->get($field_name)
->isEmpty()) {
foreach ($node
->get($field_name) as $value) {
if ($value->entity) {
$value->entity = $value->entity
->createDuplicate();
foreach ($value->entity
->getFieldDefinitions() as $field_definition) {
$field_storage_definition = $field_definition
->getFieldStorageDefinition();
$pfield_settings = $field_storage_definition
->getSettings();
$pfield_name = $field_storage_definition
->getName();
if ($this
->excludeParagraphField($pfield_name, $value->entity
->bundle())) {
unset($value->entity->{$pfield_name});
}
$this->moduleHandler
->alter('cloned_node_paragraph_field', $value->entity, $pfield_name, $pfield_settings);
}
}
}
}
}
}
return $node;
}
public function excludeParagraphField($field_name, $bundle_name) {
$config_name = 'exclude.paragraph.' . $bundle_name;
if ($exclude_fields = $this
->getConfigSettings($config_name)) {
return in_array($field_name, $exclude_fields);
}
}
public function getConfigSettings($value) {
$settings = $this->configFactory
->get('quick_node_clone.settings')
->get($value);
return $settings;
}
}