class CloneEntityTypeData in Entity Type Clone 8
Class CloneEntityTypeData.
@package Drupal\entity_type_clone\Form
Hierarchy
- class \Drupal\entity_type_clone\Form\CloneEntityTypeData
Expanded class hierarchy of CloneEntityTypeData
File
- src/
Form/ CloneEntityTypeData.php, line 18
Namespace
Drupal\entity_type_clone\FormView source
class CloneEntityTypeData {
/**
* Clones a entity type field.
*
* @param array $data
* Contains the field to clone and $form_state data.
* @param array $context
* A reference to the batch operation context.
*/
public static function cloneEntityTypeField(array $data, array &$context) {
// Get the source field name.
$sourceFieldName = $data['field']
->getName();
// Clone the field.
// Only create a duplicate of an entity if the field implements,
// EntityInterface (as this is not guaranteed e.g. for Content moderation).
if ($data['field'] instanceof EntityInterface) {
// Only create a duplicate of an entity if the field implements,
// EntityInterface (as this is not guaranteed e.g. for Content moderation).
// Clone the field.
$targetFieldConfig = $data['field']
->createDuplicate();
$targetFieldConfig
->set('entity_type', $data['values']['show']['entity_type']);
$targetFieldConfig
->set('bundle', $data['values']['clone_bundle_machine']);
$targetFieldConfig
->save();
}
// Copy the form display.
$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];
// Copy the view display.
EntityTypeCloneController::copyFieldDisplay('view', $view_mode, $data);
}
// Update the progress information.target_machine_name.
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;
}
/**
* Clones a entity type.
*
* @param array $values
* Contains the values of the form submitted via $form_state.
* @param array $context
* A reference to the batch operation context.
*/
public static function cloneEntityTypeData(array $values, array &$context) {
// Prepare the progress array.
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
}
// Load the source entity type.
if ($values['show']['entity_type'] === 'node') {
$sourceContentType = NodeType::load($values['show']['type']);
if (isset($sourceContentType)) {
// Create the target entity type.
$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)) {
// Create the target entity type.
$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();
}
}
// Update the progress information.
$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'];
}
/**
* Handles results after the batch operations.
*
* @param bool $success
* The status of the batch process.
* @param array $results
* Contains the results of the batch operation.
* @param array $operations
* The array of operations processed by the batch.
*/
public static function cloneEntityTypeFinishedCallback($success, array $results, array $operations) {
// Check batch operations success.
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.');
}
// Send the result message.
\Drupal::messenger()
->addStatus($message);
// Redirect to the entity type clone page.
$response = new RedirectResponse('admin/entity-type-clone');
$response
->send();
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CloneEntityTypeData:: |
public static | function | Clones a entity type. | |
CloneEntityTypeData:: |
public static | function | Clones a entity type field. | |
CloneEntityTypeData:: |
public static | function | Handles results after the batch operations. |