You are here

public function EntityConfigSettingsForm::submitForm in Acquia Content Hub 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/EntityConfigSettingsForm.php, line 370

Class

EntityConfigSettingsForm
Defines the form to configure the entity types and bundles to be exported.

Namespace

Drupal\acquia_contenthub\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  /** @var \Drupal\rest\RestResourceConfigInterface $contenthub_entity_config_storage */
  $contenthub_entity_config_storage = $this->entityTypeManager
    ->getStorage('acquia_contenthub_entity_config');

  /** @var \Drupal\acquia_contenthub\Entity\ContentHubEntityTypeConfig[] $contenthub_entity_config_ids */
  $contenthub_entity_config_ids = $contenthub_entity_config_storage
    ->loadMultiple();
  $values = $form_state
    ->getValues();
  foreach ($values['entities'] as $entity_type => $bundles) {
    $contenthub_entity_config_ids_bundles = [];
    if (isset($contenthub_entity_config_ids[$entity_type])) {
      $contenthub_entity_config_ids_bundles = $contenthub_entity_config_ids[$entity_type]
        ->getBundles();
    }

    // Checkboxes come with integer values. Convert them to boolean.
    foreach ($bundles as $name => $fields) {
      $bundles[$name]['enable_index'] = (bool) $bundles[$name]['enable_index'];
      $bundles[$name]['enable_viewmodes'] = $bundles[$name]['enable_index'] ? (bool) $bundles[$name]['enable_viewmodes'] : FALSE;
      if (!empty($contenthub_entity_config_ids_bundles)) {
        if (isset($contenthub_entity_config_ids_bundles[$name]['preview_image_field'])) {
          $bundles[$name]['preview_image_field'] = $contenthub_entity_config_ids_bundles[$name]['preview_image_field'];
        }
        if (isset($contenthub_entity_config_ids_bundles[$name]['preview_image_style'])) {
          $bundles[$name]['preview_image_style'] = $contenthub_entity_config_ids_bundles[$name]['preview_image_style'];
        }
      }
    }
    if (!isset($contenthub_entity_config_ids[$entity_type])) {

      // If we do not have this configuration entity, then create it.
      $data = [
        'id' => $entity_type,
        'bundles' => $bundles,
      ];
      $contenthub_entity_config = $contenthub_entity_config_storage
        ->create($data);
      $contenthub_entity_config
        ->save();
    }
    else {

      // Update Configuration entity.
      $contenthub_entity_config_ids[$entity_type]
        ->setBundles($bundles);
      $contenthub_entity_config_ids[$entity_type]
        ->save();
    }
  }
  $config = $this
    ->config('acquia_contenthub.entity_config');
  $config
    ->set('user_role', $values['user_role']);
  $config
    ->save();
}