You are here

public function FlowControllerSimple::getEntityTypeConfig in CMS Content Sync 2.1.x

@inheritDoc

Overrides IFlowController::getEntityTypeConfig

3 calls to FlowControllerSimple::getEntityTypeConfig()
FlowControllerSimple::getPropertyConfig in src/Controller/FlowControllerSimple.php
@inheritDoc
FlowControllerSimple::needsEntityTypeUpdate in src/Controller/FlowControllerSimple.php
@inheritDoc
FlowControllerSimple::updateEntityTypeVersions in src/Controller/FlowControllerSimple.php
Cache the current version per entity type.

File

src/Controller/FlowControllerSimple.php, line 110

Class

FlowControllerSimple

Namespace

Drupal\cms_content_sync\Controller

Code

public function getEntityTypeConfig($find_entity_type = null, $find_entity_bundle = null, $used_only = false, $include_new_versions = false) {
  $result = [];

  // TODO: Cache result per Flow if no $find_* parameter is given and return the cached result.
  $settings = $this->flow->simple_settings;
  $is_push = $this->flow->type === Flow::TYPE_PUSH;
  $merge_local_changes = $settings['mergeLocalChanges'];
  $selected_pools = [];
  $selected_pools_manual_assignment = [];
  $auto_assign_pools = $settings['poolAssignment'] === self::ASSIGN_ALL_POOLS;
  $used_pools = $this
    ->getUsedPools();
  $pools = Pool::getAll();
  foreach ($pools as $id => $pool) {
    if (isset($used_pools[$id])) {
      $selected_pools_manual_assignment[$id] = Pool::POOL_USAGE_ALLOW;
      if ($auto_assign_pools) {
        $selected_pools[$id] = Pool::POOL_USAGE_FORCE;
      }
      else {
        $selected_pools[$id] = Pool::POOL_USAGE_ALLOW;
      }
    }
    else {
      $selected_pools[$id] = Pool::POOL_USAGE_FORBID;
    }
  }
  $entity_plugin_manager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
  $field_plugin_manager = \Drupal::service('plugin.manager.cms_content_sync_field_handler');
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $field_map = $entity_field_manager
    ->getFieldMap();
  $entity_types = \Drupal::service('entity_type.bundle.info')
    ->getAllBundleInfo();
  $entity_type_versions = $this
    ->getExportedEntityTypeVersions();
  $assign_pools_manually_to_dependencies = empty($settings['assignPoolsManuallyToDependencies']) ? [] : $settings['assignPoolsManuallyToDependencies'];
  foreach ($entity_types as $entity_type_name => $bundles) {
    if (empty($settings['entityTypeSettings'][$entity_type_name])) {
      continue;
    }
    if ($find_entity_type && $entity_type_name !== $find_entity_type) {
      continue;
    }
    foreach ($bundles as $bundle_name => $entity_bundle) {
      if ($find_entity_bundle && $bundle_name !== $find_entity_bundle) {
        continue;
      }
      $info = EntityHandlerPluginManager::getEntityTypeInfo($entity_type_name, $bundle_name);
      if (!empty($info['no_entity_type_handler']) || !empty($info['required_field_not_supported'])) {
        continue;
      }
      if (!empty($settings['entityTypeSettings'][$entity_type_name]['allBundles'])) {
        $bundle_settings = $settings['entityTypeSettings'][$entity_type_name]['allBundles'];
      }
      elseif (!empty($settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name])) {
        $bundle_settings = $settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name];
      }
      else {
        continue;
      }
      if ($bundle_settings['mode'] === self::MODE_IGNORE) {
        continue;
      }
      elseif ($bundle_settings['mode'] === self::MODE_DEFAULT) {
        $mode = $settings['mode'];
      }
      else {
        $mode = $bundle_settings['mode'];
      }
      $entity_handlers = $entity_plugin_manager
        ->getHandlerOptions($entity_type_name, $bundle_name, true);
      if (!count($entity_handlers)) {
        continue;
      }
      $entity_handler_names = array_keys($entity_handlers);
      $handler_id = reset($entity_handler_names);
      $pool_assignment = in_array($entity_type_name, $assign_pools_manually_to_dependencies) ? $selected_pools_manual_assignment : $selected_pools;
      $result[$entity_type_name][$bundle_name] = [
        'version' => $include_new_versions || $this->virtual ? Flow::getEntityTypeVersion($entity_type_name, $bundle_name) : (empty($entity_type_versions[$entity_type_name][$bundle_name]) ? null : $entity_type_versions[$entity_type_name][$bundle_name]),
        'handler' => $handler_id,
        'handler_settings' => [
          'ignore_unpublished' => $settings['ignoreUnpublishedChanges'] ? 1 : 0,
          'allow_explicit_unpublishing' => $settings['allowExplicitUnpublishing'] ? 1 : 0,
          'export_menu_items' => $settings['pushMenuItems'] ? 1 : 0,
          'export_crop' => 1,
        ],
        'export' => $is_push ? $mode : self::MODE_IGNORE,
        'export_pools' => $is_push ? $pool_assignment : [],
        'export_deletion_settings' => [
          'export_deletion' => $settings['deletions'] ? 1 : 0,
        ],
        'pool_export_widget_type' => $settings['poolSelectionWidget'],
        'preview' => $settings['pushPreviews'] && !empty($settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name]['previewViewMode']) ? $settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name]['previewViewMode'] : Flow::PREVIEW_DISABLED,
        'import' => $is_push ? self::MODE_IGNORE : $mode,
        'import_pools' => $is_push ? [] : $selected_pools,
        'import_deletion_settings' => [
          'import_deletion' => $settings['deletions'] ? 1 : 0,
          'allow_local_deletion_of_import' => $settings['allowLocalDeletion'] ? 1 : 0,
        ],
        'import_updates' => $settings['updateBehavior'],
        'properties' => [],
      ];
      $handler = $entity_plugin_manager
        ->createInstance($handler_id, [
        'entity_type_name' => $entity_type_name,
        'bundle_name' => $bundle_name,
        'settings' => $result[$entity_type_name][$bundle_name]['handler_settings'],
        'sync' => null,
      ]);
      if (isset($field_map[$entity_type_name])) {
        $forbidden_fields = $handler
          ->getForbiddenFields();
        $pools = Pool::getAll();
        if (count($pools)) {
          $reserved = reset($pools)
            ->getClient()
            ->getReservedPropertyNames();
          $forbidden_fields = array_merge($forbidden_fields, $reserved);
        }
        $fields = $entity_field_manager
          ->getFieldDefinitions($entity_type_name, $bundle_name);
        foreach ($fields as $key => $field) {
          $field_handlers = $field_plugin_manager
            ->getHandlerOptions($entity_type_name, $bundle_name, $key, $field, true);
          $ignore = in_array($key, $forbidden_fields) || empty($field_handlers);
          $handler_id = $ignore ? 'ignore' : key($field_handlers);
          $referenced_type = null;
          if (in_array($field
            ->getType(), [
            'entity_reference',
            'entity_reference_revisions',
            'webform',
          ])) {
            $referenced_type = $field
              ->getSetting('target_type');
          }
          elseif (in_array($field
            ->getType(), [
            'image',
            'file',
            'file_uri',
          ])) {
            $referenced_type = 'file';
          }
          elseif (in_array($field
            ->getType(), [
            'bricks',
          ])) {
            $referenced_type = 'brick';
          }
          elseif (in_array($field
            ->getType(), [
            'field_collection',
          ])) {
            $referenced_type = 'field_collection_item';
          }
          $push_referenced_entities = $this->virtual ? false : !$referenced_type || !in_array($referenced_type, $assign_pools_manually_to_dependencies);
          $subscribe_only_to = null;
          if (!empty($settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name]['filterByReference'])) {
            foreach ($settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle_name]['filterByReference'] as $filter) {
              if ($filter['fieldMachineName'] === $key) {
                $subscribe_only_to = [];
                foreach ($filter['values'] as $value) {
                  $subscribe_only_to[] = [
                    'type' => $value['namespaceMachineName'],
                    'bundle' => $value['machineName'],
                    'uuid' => $value['remoteUuid'],
                  ];
                }
              }
            }
          }
          $field_settings = [
            'handler' => $handler_id,
            'export' => null,
            'import' => null,
            'preview' => null,
            'entity_type' => $entity_type_name,
            'entity_bundle' => $bundle_name,
            'handler_settings' => [
              'identification' => $settings['resolveUserReferences'],
              'export_referenced_custom_blocks' => 1,
              'export_referenced_entities' => $push_referenced_entities ? 1 : 0,
              'merge_local_changes' => $merge_local_changes ? 1 : 0,
              'subscribe_only_to' => $subscribe_only_to,
            ],
          ];
          if (!$ignore) {

            /**
             * @var \Drupal\cms_content_sync\Plugin\FieldHandlerInterface $handler
             */
            $handler = $field_plugin_manager
              ->createInstance($handler_id, [
              'entity_type_name' => $entity_type_name,
              'bundle_name' => $bundle_name,
              'field_name' => $key,
              'field_definition' => $field,
              'settings' => $field_settings,
              'sync' => $this->flow,
            ]);
            $allowed_push_options = $handler
              ->getAllowedPushOptions();
            if ($is_push && in_array(PushIntent::PUSH_AUTOMATICALLY, $allowed_push_options)) {
              $field_settings['export'] = PushIntent::PUSH_AUTOMATICALLY;
            }
            $allowed_pull_options = $handler
              ->getAllowedPullOptions();
            if (!$is_push && in_array(PullIntent::PULL_AUTOMATICALLY, $allowed_pull_options)) {
              $field_settings['import'] = PullIntent::PULL_AUTOMATICALLY;
            }
          }
          $result[$entity_type_name][$bundle_name]['properties'][$key] = $field_settings;
        }
      }
      if ($find_entity_type && $find_entity_bundle) {
        return $result[$entity_type_name][$bundle_name];
      }
    }
  }
  return $result;
}