SiteSettingsReplicator.php in Site Settings and Labels 8
File
src/SiteSettingsReplicator.php
View source
<?php
namespace Drupal\site_settings;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
class SiteSettingsReplicator {
use StringTranslationTrait;
protected $replicateReplicator;
protected $entityTypeManager;
protected $fieldToolsFieldCloner;
protected $fieldToolsDisplayCloner;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function processBatch($settings, &$context) {
if (empty($context['sandbox'])) {
$settings = $this
->cleanSettings($settings);
$context['results']['settings'] = $settings;
$context['sandbox']['settings'] = $settings;
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = count($settings['values']['new_settings']);
}
else {
$settings = $context['sandbox']['settings'];
}
if ($context['sandbox']['max'] == 0) {
$context['finished'] = 1;
}
else {
$this->replicateReplicator = \Drupal::service('replicate.replicator');
$this->fieldToolsFieldCloner = \Drupal::service('field_tools.field_cloner');
$this->fieldToolsDisplayCloner = \Drupal::service('field_tools.display_cloner');
$key = $context['sandbox']['progress'];
$setting = $settings['values']['new_settings'][$key];
$this
->replicateSetting($setting, $settings['values']['setting']);
$context['results']['current_id'] = $key;
$context['sandbox']['progress']++;
$context['sandbox']['current_id'] = $key;
$context['message'] = $this
->t('Processed @num of @total new settings.', [
'@num' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
}
protected function cleanSettings(array $settings) {
$new_settings = [];
foreach ($settings['values']['new_settings'] as $key => $setting) {
if (!empty($setting['machine_name']) && !empty($setting['label']) && !empty($setting['fieldset'])) {
$new_settings[] = $setting;
}
}
$settings['values']['new_settings'] = $new_settings;
return $settings;
}
public function replicateSetting(array $setting, $original_setting_name) {
$site_setting_entity_type = $this->entityTypeManager
->getStorage('site_setting_entity_type')
->load($original_setting_name);
$site_settings_entity_type_duplicate = $site_setting_entity_type
->createDuplicate();
$site_settings_entity_type_duplicate
->set('id', $setting['machine_name']);
$site_settings_entity_type_duplicate
->save();
$site_settings_entities = $this->entityTypeManager
->getStorage('site_setting_entity')
->loadByProperties([
'type' => $original_setting_name,
]);
$site_settings_entity = reset($site_settings_entities);
$site_settings_entity_duplicate = $this->replicateReplicator
->replicateByEntityId('site_setting_entity', $site_settings_entity
->id());
$site_settings_entity_duplicate
->setType($site_settings_entity_type_duplicate
->id());
$site_settings_entity_duplicate
->setName($setting['label']);
$site_settings_entity_duplicate
->setFieldset($setting['fieldset']);
$site_settings_entity_duplicate
->save();
if ($field_definitions = $site_settings_entity
->getFieldDefinitions()) {
foreach ($field_definitions as $field_definition) {
if (array_key_exists('Drupal\\field\\FieldConfigInterface', class_implements($field_definition))) {
$bundle = $site_settings_entity_duplicate
->id();
$this->fieldToolsFieldCloner
->cloneField($field_definition, 'site_setting_entity', $site_settings_entity_type_duplicate
->id());
}
}
}
$form_display = $this->entityTypeManager
->getStorage('entity_form_display')
->load('site_setting_entity.' . $original_setting_name . '.default');
$this->fieldToolsDisplayCloner
->cloneDisplay($form_display, $setting['machine_name']);
$view_display = $this->entityTypeManager
->getStorage('entity_view_display')
->load('site_setting_entity.' . $original_setting_name . '.default');
$this->fieldToolsDisplayCloner
->cloneDisplay($view_display, $setting['machine_name']);
return TRUE;
}
public function finishBatch($success, array $results, array $operations) {
if (!$success) {
$message = $this
->t('The settings creation was unsuccessful for an unknown reason. Please check your error logs.');
$messenger = \Drupal::messenger();
$messenger
->addWarning($message);
}
$url = Url::fromRoute('entity.site_setting_entity_type.collection');
$url_string = $url
->toString();
$response = new RedirectResponse($url_string);
$response
->send();
}
}