You are here

public function TcaSettingsManager::saveSettings in Token Content Access 2.0.x

Same name and namespace in other branches
  1. 8 src/TcaSettingsManager.php \Drupal\tca\TcaSettingsManager::saveSettings()

Save settings for an entity or bundle.

Parameters

array $settings: The settings for the TcaSettings entity.

string $entity_type_id: The entity type (e.g. node) as a string.

string $entity_id: The entity ID as a string.

Overrides TcaSettingsManagerInterface::saveSettings

File

src/TcaSettingsManager.php, line 46

Class

TcaSettingsManager
Class TcaSettingsManager.

Namespace

Drupal\tca

Code

public function saveSettings(array $settings, $entity_type_id, $entity_id) {

  // Load real entity if it has settings.
  $entity = $this
    ->getEntity($entity_type_id, $entity_id);
  if ($entity) {
    foreach ($settings as $key => $value) {
      $field = 'tca_' . $key;
      if ($entity
        ->hasField($field)) {
        $entity
          ->set($field, $value);
      }
    }
    $entity
      ->save();
  }
  else {
    $id = $this
      ->generateSettingsFullId($entity_type_id, $entity_id);
    $entity = TcaSettings::load($id);
    if ($entity === NULL) {
      $entity_array = [
        'id' => $id,
      ];
      $entity_array += $settings;
      $entity = TcaSettings::create($entity_array);
    }
    else {
      foreach ($settings as $key => $setting) {
        $entity
          ->set($key, $setting);
      }
    }
    $entity
      ->set('entity_type_id', $entity_type_id);
    $entity
      ->set('entity_id', $entity_id);
    $entity
      ->save();
  }
}