You are here

protected function FlowForm::getCurrentValues in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::getCurrentValues()
  2. 2.0.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::getCurrentValues()

Get the current values for the config entity. Data is collected in the following order:

  • Data from existing config entity
  • Extended and overwritten by user submitted data (if POST'd already)
  • Extended by default values for implicit values (e.g. field config not

shown, so implicitly fields are included).

5 calls to FlowForm::getCurrentValues()
FlowForm::renderBundleSummary in src/Form/FlowForm.php
Bundle has settings already, but the user is editing the Flow so by default we don't show all bundle edit forms as open but hide them all to save space and make the form faster. The user can then click Edit to change settings.
FlowForm::renderEnabledBundle in src/Form/FlowForm.php
Render the bundle edit form.
FlowForm::renderEntityType in src/Form/FlowForm.php
Render all bundles for the given entity type. Displayed in vertical tabs from the parent form.
FlowForm::renderFields in src/Form/FlowForm.php
Render the fields of the given entity type; either all of them or only those that either have:
FlowForm::validateForm in src/Form/FlowForm.php
Form validation handler.

File

src/Form/FlowForm.php, line 873

Class

FlowForm
Form handler for the Flow add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

protected function getCurrentValues(FormStateInterface $form_state) {

  /**
   * @var \Drupal\cms_content_sync\Entity\Flow $flow
   */
  $flow = $this->entity;
  $result = [
    'name' => $flow->name,
    'id' => $flow->id,
    'status' => $flow
      ->status(),
    'sync_entities' => $flow->sync_entities,
  ];
  $submitted = $form_state
    ->cleanValues()
    ->getValues();
  $entity_types = $this->bundleInfoService
    ->getAllBundleInfo();
  if (!empty($submitted)) {
    if (isset($submitted['name'])) {
      $result['name'] = $submitted['name'];
    }
    if (isset($submitted['id'])) {
      $result['id'] = $submitted['id'];
    }
    if (isset($submitted['status'])) {
      $result['status'] = $submitted['status'];
    }
    foreach ($entity_types as $type_key => $bundles) {
      foreach ($bundles as $entity_bundle_name => $bundle) {
        $bundle_id = $type_key . '-' . $entity_bundle_name;

        // If this is not given that means that the user didn't change anything here (didn't click "edit"). So then
        // we simply keep the existing values, i.e. nothing to do here.
        if (!isset($submitted[$type_key][$entity_bundle_name]['handler'])) {
          continue;
        }
        $bundle_settings = $submitted[$type_key][$entity_bundle_name];

        // We handle field config outside of this array (same level as bundle config).
        $result['sync_entities'][$bundle_id]['handler'] = $bundle_settings['handler'];
        if (isset($bundle_settings['handler_settings'])) {
          $result['sync_entities'][$bundle_id]['handler_settings'] = $bundle_settings['handler_settings'];
        }
        else {
          $result['sync_entities'][$bundle_id]['handler_settings'] = [];
        }
        if (!empty($bundle_settings['export'])) {
          $result['sync_entities'][$bundle_id]['export'] = $bundle_settings['export'][$bundle_id]['export'];
          $result['sync_entities'][$bundle_id]['export_pools'] = $bundle_settings['export'][$bundle_id]['export_pools'];
          $result['sync_entities'][$bundle_id]['preview'] = $bundle_settings['export'][$bundle_id]['preview'];
          $result['sync_entities'][$bundle_id]['pool_export_widget_type'] = $bundle_settings['export'][$bundle_id]['pool_export_widget_type'];
          if (isset($bundle_settings['export'][$bundle_id]['export_deletion_settings']['export_deletion'])) {
            $result['sync_entities'][$bundle_id]['export_deletion_settings']['export_deletion'] = $bundle_settings['export'][$bundle_id]['export_deletion_settings']['export_deletion'];
          }
          else {
            $result['sync_entities'][$bundle_id]['export_deletion_settings']['export_deletion'] = 0;
          }
        }
        else {
          $result['sync_entities'][$bundle_id]['export'] = PushIntent::PUSH_DISABLED;
        }
        if (!empty($bundle_settings['import'])) {
          $result['sync_entities'][$bundle_id]['import'] = $bundle_settings['import'][$bundle_id]['import'];
          $result['sync_entities'][$bundle_id]['import_pools'] = $bundle_settings['import'][$bundle_id]['import_pools'];
          if (isset($bundle_settings['import'][$bundle_id]['import_deletion_settings']['import_deletion'])) {
            $result['sync_entities'][$bundle_id]['import_deletion_settings']['import_deletion'] = $bundle_settings['import'][$bundle_id]['import_deletion_settings']['import_deletion'];
          }
          else {
            $result['sync_entities'][$bundle_id]['import_deletion_settings']['import_deletion'] = 0;
          }
          if (isset($bundle_settings['import'][$bundle_id]['import_deletion_settings']['allow_local_deletion_of_import'])) {
            $result['sync_entities'][$bundle_id]['import_deletion_settings']['allow_local_deletion_of_import'] = $bundle_settings['import'][$bundle_id]['import_deletion_settings']['allow_local_deletion_of_import'];
          }
          else {
            $result['sync_entities'][$bundle_id]['import_deletion_settings']['allow_local_deletion_of_import'] = 0;
          }
          $result['sync_entities'][$bundle_id]['import_updates'] = $bundle_settings['import'][$bundle_id]['import_updates'];
        }
        else {
          $result['sync_entities'][$bundle_id]['import'] = PullIntent::PULL_DISABLED;
        }

        // If the user set this to disabled, remove all associated configuration.
        if ('ignore' === $bundle_settings['handler']) {

          // Remove field configuration completely for this.
          foreach ($result['sync_entities'] as $key => $value) {
            if (substr($key, 0, strlen($bundle_id) + 1) === $bundle_id . '-') {
              unset($result['sync_entities'][$key]);
            }
          }
        }
        else {
          if (EntityHandlerPluginManager::isEntityTypeFieldable($type_key)) {

            /**
             * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
             */
            $fields = $this->entityFieldManager
              ->getFieldDefinitions($type_key, $entity_bundle_name);

            // @todo Test that the field config is in the right format (so as before).
            foreach ($bundle_settings['fields'] as $field_name => $settings) {
              if ('advanced' === $field_name) {
                continue;
              }
              if (empty($settings['handler'])) {
                $result['sync_entities'][$field_name] = [
                  'handler' => 'ignore',
                  'export' => PushIntent::PUSH_DISABLED,
                  'import' => PullIntent::PULL_DISABLED,
                ];
                continue;
              }
              list(, , $key) = explode('-', $field_name);
              if (isset($settings['handler_settings']['subscribe_only_to'])) {

                // @todo This should be handled by the Handler itself with another callback for saving / altering.
                if (!empty($settings['handler_settings']['subscribe_only_to'])) {

                  /**
                   * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
                   */
                  $field_definition = $this->entityFieldManager
                    ->getFieldDefinitions($type_key, $entity_bundle_name)[$key];
                  $type = $field_definition
                    ->getSetting('target_type');
                  $storage = \Drupal::entityTypeManager()
                    ->getStorage($type);
                  foreach ($settings['handler_settings']['subscribe_only_to'] as $i => $ref) {
                    $entity = $storage
                      ->load($ref['target_id']);
                    $settings['handler_settings']['subscribe_only_to'][$i] = [
                      'type' => $entity
                        ->getEntityTypeId(),
                      'bundle' => $entity
                        ->bundle(),
                      'uuid' => $entity
                        ->uuid(),
                    ];
                  }
                }
              }
              $result['sync_entities'][$field_name] = $settings;
            }
            $handler = $this->entityPluginManager
              ->createInstance($result['sync_entities'][$bundle_id]['handler'], [
              'entity_type_name' => $type_key,
              'bundle_name' => $entity_bundle_name,
              'settings' => $result['sync_entities'][$bundle_id],
              'sync' => null,
            ]);
            $forbidden_fields = $handler
              ->getForbiddenFields();
            $pools = Pool::getAll();
            if (count($pools)) {
              $reserved = reset($pools)
                ->getClient()
                ->getReservedPropertyNames();
              $forbidden_fields = array_merge($forbidden_fields, $reserved);
            }
            foreach ($fields as $key => $field) {
              $field_name = $bundle_id . '-' . $key;
              if (isset($bundle_settings['fields'][$field_name])) {
                continue;
              }
              $field_handlers = $this->fieldPluginManager
                ->getHandlerOptions($type_key, $entity_bundle_name, $key, $field, true);
              if (empty($field_handlers) || in_array($key, $forbidden_fields)) {
                $handler_id = 'ignore';
                $result['sync_entities'][$field_name] = [
                  'handler' => $handler_id,
                  'handler_settings' => [],
                  'export' => PushIntent::PUSH_DISABLED,
                  'import' => PullIntent::PULL_DISABLED,
                ];
              }
              else {
                reset($field_handlers);
                $handler_id = empty($field_default_values['handler']) ? key($field_handlers) : $field_default_values['handler'];
                $result['sync_entities'][$field_name] = [
                  'handler' => $handler_id,
                  'handler_settings' => [],
                  'export' => PushIntent::PUSH_AUTOMATICALLY,
                  'import' => PullIntent::PULL_AUTOMATICALLY,
                ];
              }
            }
          }
        }
      }
    }
  }
  if (!isset($result['sync_entities'])) {
    $result['sync_entities'] = [];
  }
  foreach ($result['sync_entities'] as $key => &$config) {
    if ('#' == $key[0]) {
      continue;
    }
    $count = substr_count($key, '-');
    if (0 == $count) {
      continue;
    }
    if (1 == $count) {
      list($entity_type, $bundle) = explode('-', $key);
      if (empty($entity_types[$entity_type][$bundle])) {
        unset($result['sync_entities'][$key]);
        continue;
      }
    }
    else {
      list($entity_type, $bundle, $field) = explode('-', $key);
      if (empty($entity_types[$entity_type][$bundle])) {
        unset($result['sync_entities'][$key]);
        continue;
      }

      /**
       * @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields
       */
      $fields = $this->entityFieldManager
        ->getFieldDefinitions($entity_type, $bundle);

      // Handle removed fields gracefully.
      if (empty($fields[$field])) {
        unset($result['sync_entities'][$key]);
        continue;
      }
    }
  }
  return $result;
}