View source
<?php
namespace Drupal\entity_type_clone\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_type_clone\Controller\EntityTypeCloneController;
use Drupal\node\Entity\NodeType;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\profile\Entity\ProfileType;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\HttpFoundation\RedirectResponse;
class CloneEntityTypeData {
public static function cloneEntityTypeField(array $data, array &$context) {
$sourceFieldName = $data['field']
->getName();
if ($data['field'] instanceof EntityInterface) {
$targetFieldConfig = $data['field']
->createDuplicate();
$targetFieldConfig
->set('entity_type', $data['values']['show']['entity_type']);
$targetFieldConfig
->set('bundle', $data['values']['clone_bundle_machine']);
$targetFieldConfig
->save();
}
$form_mode_displays = \Drupal::service('entity_display.repository')
->getFormModeOptionsByBundle($data['values']['show']['entity_type'], $data['values']['show']['type']);
foreach ($form_mode_displays as $form_mode_display => $value) {
EntityTypeCloneController::copyFieldDisplay('form', $form_mode_display, $data);
}
$config_factory = \Drupal::configFactory();
$modes = $config_factory
->listAll('core.entity_view_display' . '.' . $data['values']['show']['entity_type'] . '.' . $data['values']['show']['type']);
foreach ($modes as $mode) {
$mode_explode = explode('.', $mode);
$view_mode = $mode_explode[4];
EntityTypeCloneController::copyFieldDisplay('view', $view_mode, $data);
}
if (empty($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
$context['sandbox']['progress']++;
$context['sandbox']['current_item'] = $sourceFieldName;
$context['message'] = t('Field @source successfully cloned.', [
'@source' => $sourceFieldName,
]);
$context['results']['fields'][] = $sourceFieldName;
}
public static function cloneEntityTypeData(array $values, array &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
if ($values['show']['entity_type'] === 'node') {
$sourceContentType = NodeType::load($values['show']['type']);
if (isset($sourceContentType)) {
$targetContentType = $sourceContentType
->createDuplicate();
$targetContentType
->set('uuid', \Drupal::service('uuid')
->generate());
$targetContentType
->set('name', $values['clone_bundle']);
$targetContentType
->set('type', $values['clone_bundle_machine']);
$targetContentType
->set('originalId', $values['clone_bundle_machine']);
$targetContentType
->set('description', $values['target_description']);
$targetContentType
->save();
}
}
if ($values['show']['entity_type'] === 'paragraph') {
$sourceContentType = ParagraphsType::load($values['show']['type']);
if (isset($sourceContentType)) {
$targetContentType = $sourceContentType
->createDuplicate();
$targetContentType
->set('uuid', \Drupal::service('uuid')
->generate());
$targetContentType
->set('label', $values['clone_bundle']);
$targetContentType
->set('id', $values['clone_bundle_machine']);
$targetContentType
->set('originalId', $values['clone_bundle_machine']);
$targetContentType
->set('description', $values['target_description']);
$targetContentType
->save();
}
}
if ($values['show']['entity_type'] === 'taxonomy_term') {
$vocabulary = Vocabulary::create(array(
'vid' => $values['clone_bundle_machine'],
'description' => $values['target_description'],
'name' => $values['clone_bundle'],
));
$vocabulary
->save();
}
if ($values['show']['entity_type'] === 'profile') {
$profile_type_load = ProfileType::load($values['show']['type']);
if (isset($profile_type_load)) {
$type = ProfileType::create([
'id' => $values['clone_bundle_machine'],
'label' => $values['clone_bundle'],
'description' => isset($values['target_description']) ? $values['target_description'] : $profile_type_load
->getDescription(),
'registration' => $profile_type_load
->getRegistration(),
'multiple' => $profile_type_load
->getMultiple(),
'roles' => $profile_type_load
->getRoles(),
]);
$type
->save();
}
}
$context['sandbox']['progress']++;
$context['sandbox']['current_item'] = $values['show']['type'];
$context['message'] = t('Entity type @source successfully cloned.', [
'@source' => $values['show']['type'],
]);
$context['results']['source'][] = $values['show']['type'];
$context['results']['target'][] = $values['clone_bundle_machine'];
}
public static function cloneEntityTypeFinishedCallback($success, array $results, array $operations) {
if ($success) {
$message = t('"@source" content type and @fields field(s) cloned successfuly to "@target".', [
'@source' => $results['source'][0],
'@fields' => count($results['fields']),
'@target' => $results['target'][0],
]);
}
else {
$message = t('Finished with an error.');
}
\Drupal::messenger()
->addStatus($message);
$response = new RedirectResponse('admin/entity-type-clone');
$response
->send();
}
}