You are here

public function SiteSettingsReplicator::replicateSetting in Site Settings and Labels 8

Run the replication process for a single new setting.

Parameters

array $setting: A single new setting.

string $original_setting_name: The setting name to replicate from.

Return value

bool Successful completion.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to SiteSettingsReplicator::replicateSetting()
SiteSettingsReplicator::processBatch in src/SiteSettingsReplicator.php
Process callback for the batch set the export form.

File

src/SiteSettingsReplicator.php, line 162

Class

SiteSettingsReplicator
Class SiteSettingsReplicator.

Namespace

Drupal\site_settings

Code

public function replicateSetting(array $setting, $original_setting_name) {

  // Replicate site_settings_entity_type.
  $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();

  // Replicate site_settings_entity.
  $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();

  // Replicate site_settings_entity fields.
  if ($field_definitions = $site_settings_entity
    ->getFieldDefinitions()) {
    foreach ($field_definitions as $field_definition) {

      // Base fields are copied already, we only want fields from the
      // manage fields tab.
      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());
      }
    }
  }

  // Replicate form display.
  $form_display = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load('site_setting_entity.' . $original_setting_name . '.default');
  $this->fieldToolsDisplayCloner
    ->cloneDisplay($form_display, $setting['machine_name']);

  // Replicate view display.
  $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;
}