You are here

protected static function FlowControllerSimple::getFormConfig in CMS Content Sync 2.1.x

2 calls to FlowControllerSimple::getFormConfig()
FlowControllerSimple::getFormValues in src/Controller/FlowControllerSimple.php
Get the values for the embed Flow form.
FlowControllerSimple::getFormValuesForNewFlow in src/Controller/FlowControllerSimple.php

File

src/Controller/FlowControllerSimple.php, line 407

Class

FlowControllerSimple

Namespace

Drupal\cms_content_sync\Controller

Code

protected static function getFormConfig(?Flow $flow) {
  $all_pools = Pool::getAll();
  $pools = [];
  foreach ($all_pools as $pool) {
    $pools[] = [
      'name' => $pool
        ->label(),
      'machineName' => $pool
        ->id(),
    ];
  }
  $all_flows = Flow::getAll(TRUE);
  $reserved_flows = [];
  $pushed_bundles = [];
  $pulled_bundles = [];
  $pushed_pools = [];
  $pulled_pools = [];
  foreach ($all_flows as $flow_item) {
    if ($flow && $flow->id === $flow_item->id) {
      continue;
    }
    $reserved_flows[] = $flow_item->id;
    foreach ($flow_item
      ->getController()
      ->getEntityTypeConfig() as $entity_type_name => $bundles) {
      foreach ($bundles as $bundle_name => $settings) {
        $bundle = [
          'namespaceMachineName' => $entity_type_name,
          'machineName' => $bundle_name,
        ];
        if ($settings['export'] === PushIntent::PUSH_AUTOMATICALLY || $settings['export'] === PushIntent::PUSH_MANUALLY) {
          if (!in_array($bundle, $pushed_bundles)) {
            $pushed_bundles[] = $bundle;
          }
          foreach ($settings['export_pools'] as $pool_id => $pool_mode) {
            if ($pool_mode !== Pool::POOL_USAGE_FORBID && !in_array($pool_id, $pushed_pools)) {
              $pushed_pools[] = $pool_id;
            }
          }
        }
        if ($settings['import'] === PullIntent::PULL_AUTOMATICALLY || $settings['import'] === PullIntent::PULL_MANUALLY) {
          if (!in_array($bundle, $pulled_bundles)) {
            $pulled_bundles[] = $bundle;
          }
          foreach ($settings['import_pools'] as $pool_id => $pool_mode) {
            if ($pool_mode !== Pool::POOL_USAGE_FORBID && !in_array($pool_id, $pulled_pools)) {
              $pulled_pools[] = $pool_id;
            }
          }
        }
      }
    }
  }
  $bundles = [];
  $entity_types = \Drupal::service('entity_type.bundle.info')
    ->getAllBundleInfo();
  $entity_field_manager = \Drupal::service('entity_field.manager');
  $display_modes = \Drupal::service('entity_type.manager')
    ->getStorage('entity_view_display')
    ->loadMultiple();
  foreach ($entity_types as $entity_type_machine_name => $type_bundles) {
    foreach ($type_bundles as $bundle_machine_name => $bundle) {

      // Always ignore webform submissions as they will not even provide a
      // correct machine name (using numbers).
      if ($entity_type_machine_name === 'webform_submission') {
        continue;
      }
      $info = EntityHandlerPluginManager::getEntityTypeInfo($entity_type_machine_name, $bundle_machine_name);
      $display_modes_ids = array_keys($display_modes);
      $available_preview_modes = [];
      foreach ($display_modes_ids as $id) {
        $length = strlen($entity_type_machine_name) + strlen($bundle_machine_name) + 2;
        if (substr($id, 0, $length) != $entity_type_machine_name . '.' . $bundle_machine_name . '.') {
          continue;
        }
        $id = substr($id, $length);
        $label = $id;
        $available_preview_modes[$id] = $label;
      }
      $missing_fields = array_merge(isset($info['unsupported_required_fields']) ? $info['unsupported_required_fields'] : [], isset($info['unsupported_optional_fields']) ? $info['unsupported_optional_fields'] : []);
      $reference_fields = [];
      if (EntityHandlerPluginManager::isEntityTypeFieldable($entity_type_machine_name)) {

        /**
         * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
         */
        $fields = $entity_field_manager
          ->getFieldDefinitions($entity_type_machine_name, $bundle_machine_name);
        foreach ($fields as $key => $field) {
          $referenced_type = null;
          $referenced_bundles = 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';
          }
          if (!$referenced_type) {
            continue;
          }
          $item = [
            'machineName' => $key,
            'name' => $field
              ->getLabel(),
            'targetNamespaceMachineName' => $referenced_type,
          ];
          $field_settings = $field
            ->getSettings();
          if ((!empty($field_settings['handler_settings']) || 'brick' === $referenced_type) && !empty($field_settings['handler_settings']['target_bundles'])) {
            $referenced_bundles = [];
            foreach ($field_settings['handler_settings']['target_bundles'] as $target_bundle) {
              $referenced_bundles[] = $target_bundle;
            }
            $item['targetMachineNames'] = $referenced_bundles;
          }
          $reference_fields[] = $item;
        }
      }
      $bundles[] = [
        'namespaceMachineName' => $entity_type_machine_name,
        'machineName' => $bundle_machine_name,
        'name' => $bundle['label'],
        'supported' => $info['is_supported'],
        'isConfiguration' => EntityHandlerPluginManager::isEntityTypeConfiguration($entity_type_machine_name),
        'viewModes' => $available_preview_modes,
        'unsupportedFields' => $missing_fields,
        'referenceFields' => $reference_fields,
      ];
    }
  }
  return [
    'bundles' => $bundles,
    'pools' => $pools,
    'reservedFlowMachineNames' => $reserved_flows,
    'pushedEntityTypes' => $pushed_bundles,
    'pulledEntityTypes' => $pulled_bundles,
    'pushedPools' => $pushed_pools,
    'pulledPools' => $pulled_pools,
  ];
}