View source
<?php
namespace Drupal\cms_content_sync\Controller;
use Drupal\cms_content_sync\Entity\Flow;
use Drupal\cms_content_sync\Entity\Pool;
use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\cms_content_sync\PullIntent;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Entity\EntityInterface;
class FlowControllerBase {
protected $flow;
public function __construct(Flow $flow) {
$this->flow = $flow;
}
public function getEntityTypesToPull($pull_type = null) {
$pulled_entity_types = [];
$entity_types = $this
->getEntityTypeConfig();
foreach ($entity_types as $entity_type_name => $bundles) {
foreach ($bundles as $bundle_name => $config) {
if (is_null($pull_type) ? PullIntent::PULL_DISABLED != $config['import'] : $config['import'] == $pull_type) {
$pulled_entity_types[$entity_type_name][$bundle_name] = $config;
}
}
}
return $pulled_entity_types;
}
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 $bundles) {
foreach ($bundles as $bundle_name => $config) {
if ($this
->canPushEntityType($entity_type_name, $bundle_name, $reason, $action, $pool)) {
return true;
}
}
}
return false;
}
$config = $this
->getEntityTypeConfig($entity_type_name, $bundle_name);
if (empty($config) || Flow::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 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 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->flow, $pool);
if ($entity_status && $entity_status
->isPushEnabled()) {
$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 getUsedPools() {
$result = [];
$pools = Pool::getAll();
foreach ($pools as $id => $pool) {
if ($this
->usesPool($pool)) {
$result[$id] = $pool;
}
}
return $result;
}
public function usesPool($pool) {
foreach ($this
->getEntityTypeConfig(null, null, true) as $bundles) {
foreach ($bundles 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 canPullEntity($entity_type_name, $bundle_name, $reason, $action = SyncIntent::ACTION_CREATE, $strict = false) {
$config = $this
->getEntityTypeConfig($entity_type_name, $bundle_name);
if (empty($config) || Flow::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 Flow::HANDLER_IGNORE != $config['handler'];
}
public function getEntityTypeHandler(string $entity_type_name, string $bundle_name, $config) {
$entityPluginManager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
return $entityPluginManager
->createInstance($config['handler'], [
'entity_type_name' => $entity_type_name,
'bundle_name' => $bundle_name,
'settings' => $config,
'sync' => $this->flow,
]);
}
public function getFieldHandler($entity_type_name, $bundle_name, $field_name) {
$fieldPluginManager = \Drupal::service('plugin.manager.cms_content_sync_field_handler');
$config = $this
->getPropertyConfig($entity_type_name, $bundle_name, $field_name);
if (empty($config)) {
return null;
}
if (Flow::HANDLER_IGNORE == $config['handler']) {
return null;
}
$entityFieldManager = \Drupal::service('entity_field.manager');
$field_definition = $entityFieldManager
->getFieldDefinitions($entity_type_name, $bundle_name)[$field_name];
return $fieldPluginManager
->createInstance($config['handler'], [
'entity_type_name' => $entity_type_name,
'bundle_name' => $bundle_name,
'field_name' => $field_name,
'field_definition' => $field_definition,
'settings' => $config,
'sync' => $this->flow,
]);
}
public function getPreviewType($entity_type_name, $bundle_name) {
$previews_enabled = ContentSyncSettings::getInstance()
->isPreviewEnabled();
if (!$previews_enabled) {
return Flow::PREVIEW_DISABLED;
}
$config = $this
->getEntityTypeConfig($entity_type_name, $bundle_name);
if (empty($config['preview'])) {
return Flow::PREVIEW_DISABLED;
}
return $config['preview'];
}
}