View source
<?php
namespace Drupal\cms_content_sync\Entity;
use Drupal\cms_content_sync\Controller\ContentSyncSettings;
use Drupal\cms_content_sync\Plugin\Type\EntityHandlerPluginManager;
use Drupal\cms_content_sync\PullIntent;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityInterface;
class Flow extends ConfigEntityBase implements FlowInterface {
public const HANDLER_IGNORE = 'ignore';
public const PREVIEW_DISABLED = 'disabled';
public const PREVIEW_TABLE = 'table';
public const TYPE_PUSH = 'push';
public const TYPE_PULL = 'pull';
public const TYPE_BOTH = 'both';
public $id;
public $name;
public $sync_entities;
public static $all = null;
public function getType() {
if (empty($this->sync_entities)) {
return null;
}
$has_push = false;
$has_pull = false;
foreach ($this
->getEntityTypeConfig() as $config) {
if (PushIntent::PUSH_DISABLED != $config['export']) {
$has_push = true;
if ($has_pull) {
break;
}
}
if (PullIntent::PULL_DISABLED != $config['import']) {
$has_pull = true;
if ($has_push) {
break;
}
}
}
if ($has_push) {
if ($has_pull) {
return self::TYPE_BOTH;
}
return self::TYPE_PUSH;
}
if ($has_pull) {
return self::TYPE_PULL;
}
return null;
}
public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this
->getUsedPools() as $pool) {
$this
->addDependency('config', 'cms_content_sync.pool.' . $pool->id);
}
}
public static function getFlowsForPushing($entity, $action, $include_dependencies = true) {
if (SyncIntent::ACTION_DELETE === $action) {
$last_push = EntityStatus::getLastPushForEntity($entity);
if (empty($last_push)) {
return [];
}
}
$flows = PushIntent::getFlowsForEntity($entity, PushIntent::PUSH_AUTOMATICALLY, $action);
if (!count($flows) && SyncIntent::ACTION_DELETE === $action) {
$flows = PushIntent::getFlowsForEntity($entity, PushIntent::PUSH_MANUALLY, $action);
}
if ($include_dependencies && !count($flows)) {
$flows = PushIntent::getFlowsForEntity($entity, PushIntent::PUSH_AS_DEPENDENCY, $action);
if (count($flows)) {
$infos = EntityStatus::getInfosForEntity($entity
->getEntityTypeId(), $entity
->uuid());
$pushed = [];
foreach ($infos as $info) {
if (!in_array($info
->getFlow(), $flows)) {
continue;
}
if (in_array($info
->getFlow(), $pushed)) {
continue;
}
if (!$info
->getLastPush()) {
continue;
}
$pushed[] = $info
->getFlow();
}
$flows = $pushed;
}
}
return $flows;
}
public static function getEntityTypeVersion($type_name, $bundle_name) {
if (EntityHandlerPluginManager::isEntityTypeFieldable($type_name)) {
$entityFieldManager = \Drupal::service('entity_field.manager');
$field_definitions = $entityFieldManager
->getFieldDefinitions($type_name, $bundle_name);
$field_definitions_array = (array) $field_definitions;
unset($field_definitions_array['field_cms_content_synced']);
$field_names = array_keys($field_definitions_array);
sort($field_names);
$version = json_encode($field_names);
}
else {
$version = '';
}
return md5($version);
}
public static function isLocalDeletionAllowed(EntityInterface $entity) {
if (!$entity
->uuid()) {
return true;
}
$entity_status = EntityStatus::getInfosForEntity($entity
->getEntityTypeId(), $entity
->uuid());
foreach ($entity_status as $info) {
if (!$info
->getLastPull() || $info
->isSourceEntity()) {
continue;
}
$flow = $info
->getFlow();
if (!$flow) {
continue;
}
$config = $flow
->getEntityTypeConfig($entity
->getEntityTypeId(), $entity
->bundle(), true);
if (PullIntent::PULL_DISABLED === $config['import']) {
continue;
}
if (!boolval($config['import_deletion_settings']['allow_local_deletion_of_import'])) {
return false;
}
}
return true;
}
public function getUsedPools() {
$result = [];
$pools = Pool::getAll();
foreach ($pools as $id => $pool) {
if ($this
->usesPool($pool)) {
$result[$id] = $pool;
}
}
return $result;
}
public function getUsedPoolsForPulling($entity_type, $bundle) {
$config = $this
->getEntityTypeConfig($entity_type, $bundle);
if (empty($config['import_pools'])) {
return [];
}
$result = [];
$pools = Pool::getAll();
foreach ($config['import_pools'] as $id => $setting) {
$pool = $pools[$id];
if (Pool::POOL_USAGE_FORBID == $setting) {
continue;
}
$result[] = $pool;
}
return $result;
}
public function getPoolsToPushTo(EntityInterface $entity, $reason, $action, $include_forced = true) {
$config = $this
->getEntityTypeConfig($entity
->getEntityTypeId(), $entity
->bundle());
if (!$this
->canPushEntity($entity, $reason, $action)) {
return [];
}
$result = [];
$pools = Pool::getAll();
foreach ($config['export_pools'] as $id => $setting) {
if (!isset($pools[$id])) {
continue;
}
$pool = $pools[$id];
if (Pool::POOL_USAGE_FORBID == $setting) {
continue;
}
if (Pool::POOL_USAGE_FORCE == $setting) {
if ($include_forced) {
$result[$id] = $pool;
}
continue;
}
$entity_status = EntityStatus::getInfoForEntity($entity
->getEntityTypeId(), $entity
->uuid(), $this, $pool);
if ($entity_status && $entity_status
->isPushEnabled()) {
$result[$id] = $pool;
}
}
return $result;
}
public function canPushEntity(EntityInterface $entity, $reason, $action = SyncIntent::ACTION_CREATE, $pool = null) {
$infos = $entity
->uuid() ? EntityStatus::getInfosForEntity($entity
->getEntityTypeId(), $entity
->uuid()) : [];
if (!count($infos) || null !== $pool) {
return $this
->canPushEntityType($entity
->getEntityTypeId(), $entity
->bundle(), $reason, $action, $pool);
}
foreach ($infos as $info) {
if ($this
->canPushEntityType($entity
->getEntityTypeId(), $entity
->bundle(), $reason, $action, $info
->getPool())) {
return true;
}
}
return $this
->canPushEntityType($entity
->getEntityTypeId(), $entity
->bundle(), $reason, $action, $pool);
}
public function canPushEntityType($entity_type_name, $bundle_name, $reason, $action = SyncIntent::ACTION_CREATE, $pool = null) {
$any_reason = [
PushIntent::PUSH_AUTOMATICALLY,
PushIntent::PUSH_MANUALLY,
PushIntent::PUSH_AS_DEPENDENCY,
];
if (is_string($reason)) {
if (PushIntent::PUSH_ANY === $reason || PushIntent::PUSH_FORCED === $reason) {
$reason = $any_reason;
}
else {
$reason = [
$reason,
];
}
}
if (!$bundle_name) {
foreach ($this
->getEntityTypeConfig($entity_type_name) as $config) {
if ($this
->canPushEntityType($entity_type_name, $config['bundle_name'], $reason, $action, $pool)) {
return true;
}
}
return false;
}
$config = $this
->getEntityTypeConfig($entity_type_name, $bundle_name);
if (empty($config) || self::HANDLER_IGNORE == $config['handler']) {
return false;
}
if (PushIntent::PUSH_DISABLED == $config['export']) {
return false;
}
if (SyncIntent::ACTION_DELETE == $action && !boolval($config['export_deletion_settings']['export_deletion'])) {
return false;
}
if ($pool) {
if (empty($config['export_pools'][$pool->id]) || Pool::POOL_USAGE_FORBID == $config['export_pools'][$pool->id]) {
return false;
}
}
return in_array($config['export'], $reason);
}
public static function getAll($skip_inactive = true) {
if ($skip_inactive && null !== self::$all) {
return self::$all;
}
$configurations = \Drupal::entityTypeManager()
->getStorage('cms_content_sync_flow')
->loadMultiple();
foreach ($configurations as $id => &$configuration) {
global $config;
$config_name = 'cms_content_sync.flow.' . $id;
if (!isset($config[$config_name]) || empty($config[$config_name])) {
continue;
}
foreach ($config[$config_name] as $key => $new_value) {
if ('sync_entities' == $key) {
foreach ($new_value as $sync_key => $options) {
foreach ($options as $options_key => $setting) {
if (is_array($setting)) {
foreach ($setting as $setting_key => $set) {
$configuration->sync_entities[$sync_key][$options_key][$setting_key] = $set;
}
}
else {
$configuration->sync_entities[$sync_key][$options_key] = $setting;
}
}
}
continue;
}
$configuration
->set($key, $new_value);
}
$configuration
->getEntityTypeConfig();
}
if ($skip_inactive) {
$result = [];
foreach ($configurations as $id => $flow) {
if ($flow
->get('status')) {
$result[$id] = $flow;
}
}
$configurations = $result;
self::$all = $configurations;
}
return $configurations;
}
public static function getFlowForApiAndEntityType($pool, $entity_type_name, $bundle_name, $reason, $action = SyncIntent::ACTION_CREATE, $strict = false) {
$flows = self::getAll();
$fallback = null;
foreach ($flows as $flow) {
if ($pool && !in_array($pool, $flow
->getUsedPoolsForPulling($entity_type_name, $bundle_name))) {
continue;
}
if (!$flow
->canPullEntity($entity_type_name, $bundle_name, $reason, $action, true)) {
if (!$strict && $flow
->canPullEntity($entity_type_name, $bundle_name, $reason, $action, false)) {
$fallback = $flow;
}
continue;
}
return $flow;
}
if (!empty($fallback)) {
return $fallback;
}
return null;
}
public function canPullEntity($entity_type_name, $bundle_name, $reason, $action = SyncIntent::ACTION_CREATE, $strict = false) {
$config = $this
->getEntityTypeConfig($entity_type_name, $bundle_name);
if (empty($config) || self::HANDLER_IGNORE == $config['handler']) {
return false;
}
if (PullIntent::PULL_DISABLED == $config['import']) {
return false;
}
if (SyncIntent::ACTION_DELETE == $action && !boolval($config['import_deletion_settings']['import_deletion'])) {
return false;
}
if (PullIntent::PULL_FORCED == $reason) {
return true;
}
if (PullIntent::PULL_AUTOMATICALLY == $config['import']) {
if (PullIntent::PULL_AS_DEPENDENCY == $reason && !$strict) {
return true;
}
}
if (PullIntent::PULL_AUTOMATICALLY == $reason && PullIntent::PULL_MANUALLY == $config['import']) {
if (SyncIntent::ACTION_UPDATE == $action || SyncIntent::ACTION_DELETE == $action) {
return true;
}
}
return $config['import'] == $reason;
}
public function supportsEntity(EntityInterface $entity) {
$config = $this
->getEntityTypeConfig($entity
->getEntityTypeId(), $entity
->bundle());
if (empty($config) || empty($config['handler'])) {
return false;
}
return self::HANDLER_IGNORE != $config['handler'];
}
public function usesPool($pool) {
foreach ($this
->getEntityTypeConfig(null, null, true) as $config) {
if (Flow::HANDLER_IGNORE == $config['handler']) {
continue;
}
if (PushIntent::PUSH_DISABLED != $config['export']) {
if (!empty($config['export_pools'][$pool->id]) && Pool::POOL_USAGE_FORBID != $config['export_pools'][$pool->id]) {
return true;
}
}
if (PullIntent::PULL_DISABLED != $config['import']) {
if (!empty($config['import_pools'][$pool->id]) && Pool::POOL_USAGE_FORBID != $config['import_pools'][$pool->id]) {
return true;
}
}
}
return false;
}
public function resetVersionWarning() {
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler
->moduleExists('cms_content_sync_developer')) {
$developer_config = \Drupal::service('config.factory')
->getEditable('cms_content_sync.developer');
$mismatching_versions = $developer_config
->get('version_mismatch');
if (!empty($mismatching_versions)) {
unset($mismatching_versions[$this
->id()]);
$developer_config
->set('version_mismatch', $mismatching_versions)
->save();
}
}
}
public function updateEntityTypeBundleVersion($entity_type, $bundle) {
$active_version = Flow::getEntityTypeVersion($entity_type, $bundle);
$config_key = $entity_type . '-' . $bundle;
$flow_config = \Drupal::service('config.factory')
->getEditable('cms_content_sync.flow.' . $this
->id());
$config_version = $flow_config
->get('sync_entities.' . $config_key . '.version');
if ($active_version != $config_version) {
$default = self::getDefaultFieldConfigForEntityType($entity_type, $bundle, $this->sync_entities);
foreach ($default as $id => $config) {
$flow_config
->set('sync_entities.' . $id, $config);
}
$flow_config
->set('sync_entities.' . $config_key . '.version', $active_version);
$flow_config
->save();
\Drupal::messenger()
->addMessage('Content Sync - Flow: ' . $this
->label() . ' has been updated. Ensure to export the new configuration to the sync core.');
}
}
public static function getDefaultFieldConfigForEntityType($type, $bundle, $existing = null, $field = null) {
if ($field) {
$field_default_values = [
'export' => null,
'import' => null,
];
$entity_type = \Drupal::entityTypeManager()
->getDefinition($type);
$forbidden_fields = [
'revision_default',
'revision_translation_affected',
'content_translation_outdated',
'host_type',
'uri',
'filemime',
'filesize',
'thumbnail',
'parent',
$entity_type
->getKey('bundle'),
$entity_type
->getKey('id'),
$entity_type
->getKey('uuid'),
$entity_type
->getKey('label'),
$entity_type
->getKey('revision'),
];
$pools = Pool::getAll();
if (count($pools)) {
$reserved = reset($pools)
->getClient()
->getReservedPropertyNames();
$forbidden_fields = array_merge($forbidden_fields, $reserved);
}
if (false !== in_array($field, $forbidden_fields)) {
$field_default_values['handler'] = 'ignore';
$field_default_values['export'] = PushIntent::PUSH_DISABLED;
$field_default_values['import'] = PullIntent::PULL_DISABLED;
return $field_default_values;
}
$field_handler_service = \Drupal::service('plugin.manager.cms_content_sync_field_handler');
$field_definition = \Drupal::service('entity_field.manager')
->getFieldDefinitions($type, $bundle)[$field];
$field_handlers = $field_handler_service
->getHandlerOptions($type, $bundle, $field, $field_definition, true);
if (empty($field_handlers)) {
throw new \Exception('Unsupported field type ' . $field_definition
->getType() . ' for field ' . $type . '.' . $bundle . '.' . $field);
}
reset($field_handlers);
$handler_id = empty($field_default_values['handler']) ? key($field_handlers) : $field_default_values['handler'];
$field_default_values['handler'] = $handler_id;
$field_default_values['export'] = PushIntent::PUSH_AUTOMATICALLY;
$field_default_values['import'] = PullIntent::PULL_AUTOMATICALLY;
$handler = $field_handler_service
->createInstance($handler_id, [
'entity_type_name' => $type,
'bundle_name' => $bundle,
'field_name' => $field,
'field_definition' => $field_definition,
'settings' => $field_default_values,
'sync' => null,
]);
$advanced_settings = $handler
->getHandlerSettings($field_default_values);
if (count($advanced_settings)) {
foreach ($advanced_settings as $name => $element) {
$field_default_values['handler_settings'][$name] = $element['#default_value'];
}
}
return $field_default_values;
}
$field_definition = \Drupal::service('entity_field.manager')
->getFieldDefinitions($type, $bundle);
$result = [];
foreach ($field_definition as $key => $field) {
$field_id = $type . '-' . $bundle . '-' . $key;
if ($existing && isset($existing[$field_id])) {
continue;
}
$result[$field_id] = self::getDefaultFieldConfigForEntityType($type, $bundle, null, $key);
}
return $result;
}
public function getEntityTypeConfig($entity_type = null, $entity_bundle = null, $used_only = false) {
$entity_types = $this->sync_entities;
$result = [];
foreach ($entity_types as $id => &$type) {
if (1 != substr_count($id, '-')) {
continue;
}
if ($used_only && self::HANDLER_IGNORE == $type['handler']) {
continue;
}
preg_match('/^(.+)-(.+)$/', $id, $matches);
$entity_type_name = $matches[1];
$bundle_name = $matches[2];
if ($entity_type && $entity_type_name != $entity_type) {
continue;
}
if ($entity_bundle && $bundle_name != $entity_bundle) {
continue;
}
if (empty($type['version'])) {
$type['version'] = Flow::getEntityTypeVersion($entity_type_name, $bundle_name);
$type['entity_type_name'] = $entity_type_name;
$type['bundle_name'] = $bundle_name;
}
if ($entity_type && $entity_bundle) {
return $type;
}
$result[$id] = $type;
}
return $result;
}
public function getEntityTypeHandler($config) {
$entityPluginManager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
return $entityPluginManager
->createInstance($config['handler'], [
'entity_type_name' => $config['entity_type_name'],
'bundle_name' => $config['bundle_name'],
'settings' => $config,
'sync' => $this,
]);
}
public function getFieldHandler($entity_type_name, $bundle_name, $field_name) {
$fieldPluginManager = \Drupal::service('plugin.manager.cms_content_sync_field_handler');
$key = $entity_type_name . '-' . $bundle_name . '-' . $field_name;
if (empty($this->sync_entities[$key])) {
return null;
}
if (self::HANDLER_IGNORE == $this->sync_entities[$key]['handler']) {
return null;
}
$entityFieldManager = \Drupal::service('entity_field.manager');
$field_definition = $entityFieldManager
->getFieldDefinitions($entity_type_name, $bundle_name)[$field_name];
return $fieldPluginManager
->createInstance($this->sync_entities[$key]['handler'], [
'entity_type_name' => $entity_type_name,
'bundle_name' => $bundle_name,
'field_name' => $field_name,
'field_definition' => $field_definition,
'settings' => $this->sync_entities[$key],
'sync' => $this,
]);
}
public function getFieldHandlerConfig($entity_type_name, $bundle_name, $field_name) {
$key = $entity_type_name . '-' . $bundle_name . '-' . $field_name;
if (!isset($this->sync_entities[$key])) {
return null;
}
return $this->sync_entities[$key];
}
public function getPreviewType($entity_type_name, $bundle_name) {
$previews_enabled = ContentSyncSettings::getInstance()
->isPreviewEnabled();
if (!$previews_enabled) {
return Flow::PREVIEW_DISABLED;
}
$key = $entity_type_name . '-' . $bundle_name;
$settings = $this->sync_entities[$key];
if (empty($settings['preview'])) {
return Flow::PREVIEW_DISABLED;
}
return $settings['preview'];
}
public function getPushedEntityTypes($push_type = null, $entity_type = null) {
$pushed_entity_types = [];
$entity_types = $this
->getEntityTypeConfig();
foreach ($entity_types as $key => $entity_type) {
if ($entity_type && $key !== $entity_type) {
continue;
}
if (is_null($push_type) && PushIntent::PUSH_DISABLED != $entity_type['export']) {
$pushed_entity_types[$key] = $entity_type;
}
elseif ($entity_type['export'] == $push_type) {
$pushed_entity_types[$key] = $entity_type;
}
}
return $pushed_entity_types;
}
public function getEntityTypesToPull($pull_type = null, $entity_type = null) {
$pulled_entity_types = [];
$entity_types = $this
->getEntityTypeConfig();
foreach ($entity_types as $key => $entity_type) {
if (is_null($pull_type) && PullIntent::PULL_DISABLED != $entity_type['import']) {
$pulled_entity_types[$key] = $entity_type;
}
elseif ($entity_type['import'] == $pull_type) {
$pulled_entity_types[$key] = $entity_type;
}
}
return $pulled_entity_types;
}
public static function createFlow($flow_name, $flow_id = '', $status = true, $dependencies = [], $configurations, $force_update = false) {
$flows = Flow::getAll(false);
if (empty($flow_id)) {
$flow_id = strtolower($flow_name);
$flow_id = preg_replace('@[^a-z0-9_]+@', '_', $flow_id);
}
if (!$force_update && array_key_exists($flow_id, $flows)) {
\Drupal::messenger()
->addMessage('A flow with the machine name ' . $flow_id . ' does already exist. Therefor the creation has been skipped.', 'warning');
}
else {
$uuid_service = \Drupal::service('uuid');
$language_manager = \Drupal::service('language_manager');
$default_language = $language_manager
->getDefaultLanguage();
$config = [
'dependencies' => $dependencies,
];
$flow_config = \Drupal::service('config.factory')
->getEditable('cms_content_sync.flow.' . $flow_id);
$flow_config
->set('uuid', $uuid_service
->generate())
->set('langcode', $default_language
->getId())
->set('status', true)
->set('id', $flow_id)
->set('name', $flow_name)
->set('config', $config)
->set('sync_entities', []);
foreach ($configurations as $entity_type_key => $bundles) {
foreach ($bundles as $bundle_key => $bundle) {
$entityPluginManager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
$entity_handler = $entityPluginManager
->getHandlerOptions($entity_type_key, $bundle_key);
$entity_handler = reset($entity_handler);
$flow_config
->set('sync_entities.' . $entity_type_key . '-' . $bundle_key, [
'handler' => $entity_handler['id'],
'entity_type_name' => $entity_type_key,
'bundle_name' => $bundle_key,
'version' => Flow::getEntityTypeVersion($entity_type_key, $bundle_key),
'export' => $bundle['push_configuration']['behavior'] ?? PushIntent::PUSH_DISABLED,
'export_deletion_settings' => [
'export_deletion' => $bundle['push_configuration']['export_deletion_settings'] ?? '',
],
'export_pools' => $bundle['push_configuration']['export_pools'] ?? [],
'import' => $bundle['import_configuration']['behavior'] ?? PullIntent::PULL_DISABLED,
'import_deletion_settings' => [
'import_deletion' => $bundle['import_configuration']['import_deletion'] ?? 0,
'allow_local_deletion_of_import' => $bundle['import_configuration']['allow_local_deletion_of_import'] ?? 0,
],
'import_updates' => $bundle['import_configuration']['import_updates'] ?? PullIntent::PULL_UPDATE_FORCE,
'import_pools' => $bundle['import_configuration']['import_pools'] ?? [],
'pool_export_widget_type' => 'checkboxes',
'preview' => 'table',
]);
$entityFieldManager = \Drupal::service('entity_field.manager');
$fields = $entityFieldManager
->getFieldDefinitions($entity_type_key, $bundle_key);
foreach (Flow::getDefaultFieldConfigForEntityType($entity_type_key, $bundle_key) as $field_id => $field_config) {
if (!empty($bundle['tags'])) {
list(, , $field_name) = explode('-', $field_id);
$field = $fields[$field_name];
if ($field && 'entity_reference' == $field
->getType() && 'taxonomy_term' == $field
->getSetting('target_type')) {
$bundles = $field
->getSetting('target_bundles');
if (!$bundles) {
$field_settings = $field
->getSettings();
$bundles = $field_settings['handler_settings']['target_bundles'];
}
if (is_array($bundles)) {
foreach ($bundle['tags'] as $tag) {
if (in_array($tag
->bundle(), $bundles)) {
$field_config['handler_settings']['subscribe_only_to'][] = [
'type' => 'taxonomy_term',
'bundle' => $tag
->bundle(),
'uuid' => $tag
->uuid(),
];
}
}
}
}
}
$flow_config
->set('sync_entities.' . $field_id, $field_config);
}
}
}
$flow_config
->save();
}
return $flow_id;
}
}