You are here

class TcaSettingsManager in Token Content Access 2.0.x

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

Class TcaSettingsManager.

@package Drupal\tca

Hierarchy

Expanded class hierarchy of TcaSettingsManager

1 file declares its use of TcaSettingsManager
TcaAccessCheck.php in src/Access/TcaAccessCheck.php
1 string reference to 'TcaSettingsManager'
tca.services.yml in ./tca.services.yml
tca.services.yml
1 service uses TcaSettingsManager
tca.tca_settings_manager in ./tca.services.yml
Drupal\tca\TcaSettingsManager

File

src/TcaSettingsManager.php, line 16

Namespace

Drupal\tca
View source
class TcaSettingsManager implements TcaSettingsManagerInterface {

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\Config\ConfigFactory definition.
   *
   * @var Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructor.
   */
  public function __construct(EntityTypeManagerInterface $etm, ConfigFactory $config_factory) {
    $this->entityTypeManager = $etm;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  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();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function loadSettingsAsConfig($entity_type_id, $entity_id) {

    // Load real entity if it has settings.
    $entity = $this
      ->getEntity($entity_type_id, $entity_id);
    if ($entity) {
      $config = $this->configFactory
        ->get('tca.tca_settings.default');
      $settings = $config
        ->getRawData();
      foreach ($settings as $key => $value) {
        $field = 'tca_' . $key;
        if ($entity
          ->hasField($field)) {
          if (isset($entity
            ->get($field)->value)) {
            $settings[$key] = $entity
              ->get($field)->value;
          }
        }
      }
      return $config
        ->initWithData($settings);
    }
    else {
      $id = $this
        ->generateSettingsFullId($entity_type_id, $entity_id);
      $actual = $this->configFactory
        ->get('tca.tca_settings.' . $id);
      if (!$actual
        ->isNew()) {
        return $actual;
      }
      else {
        return $this->configFactory
          ->get('tca.tca_settings.default');
      }
    }
  }

  /**
   * Get entity.
   *
   * @param string $entity_type_id
   *   The entity type (e.g. node) as a string.
   * @param string $entity_id
   *   The entity ID as a string.
   *
   * @return Drupal\Core\Entity\EntityInterface
   *   The entity.
   */
  private function getEntity($entity_type_id, $entity_id) {
    $entity_storage = $this->entityTypeManager
      ->getStorage($entity_type_id);
    if ($entity_storage && $entity_id) {
      $entity = $entity_storage
        ->load($entity_id);
      if ($entity && is_subclass_of($entity, 'Drupal\\Core\\Entity\\ContentEntityBase')) {
        if ($entity
          ->hasField('tca_active') && $entity
          ->hasField('tca_token')) {
          return $entity;
        }
      }
    }
    return NULL;
  }

  /**
   * Generate a full ID based on entity type label, bundle label and entity id.
   *
   * @param string $entity_type_id
   *   The entity type (e.g. node) as a string.
   * @param string $entity_id
   *   The entity ID as a string.
   *
   * @return string
   *   The full id appropriate for a TcaSettings config entity.
   */
  private function generateSettingsFullId($entity_type_id, $entity_id) {
    return $entity_type_id . (!empty($entity_id) ? '_' . $entity_id : '');
  }

  /**
   * Generate a token value.
   *
   * @return string
   *   64 length string.
   */
  public function generateToken($entity_type_id, $entity_id) {
    $private_key = \Drupal::service('private_key')
      ->get();
    $hash_salt = Settings::getHashSalt();
    return Crypt::hashBase64($entity_type_id . $entity_id . microtime(), $private_key . $hash_salt);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TcaSettingsManager::$configFactory protected property Drupal\Core\Config\ConfigFactory definition.
TcaSettingsManager::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManager definition.
TcaSettingsManager::generateSettingsFullId private function Generate a full ID based on entity type label, bundle label and entity id.
TcaSettingsManager::generateToken public function Generate a token value.
TcaSettingsManager::getEntity private function Get entity.
TcaSettingsManager::loadSettingsAsConfig public function Load settings for an entity or bundle, or load the defaults. Overrides TcaSettingsManagerInterface::loadSettingsAsConfig
TcaSettingsManager::saveSettings public function Save settings for an entity or bundle. Overrides TcaSettingsManagerInterface::saveSettings
TcaSettingsManager::__construct public function Constructor.