You are here

public function FlowForm::save 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::save()
  2. 2.0.x src/Form/FlowForm.php \Drupal\cms_content_sync\Form\FlowForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/FlowForm.php, line 620

Class

FlowForm
Form handler for the Flow add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $config = $this->entity;
  $config->{'sync_entities'} = $this
    ->getCurrentValues($form_state)['sync_entities'];
  $sync_entities =& $config->{'sync_entities'};
  $export_menu_items_automatically = false;
  if (isset($sync_entities['menu_link_content-menu_link_content']) && PushIntent::PUSH_AUTOMATICALLY == $sync_entities['menu_link_content-menu_link_content']['export']) {
    $export_menu_items_automatically = true;
  }
  foreach ($sync_entities as $key => $settings) {

    // Entity settings.
    if (1 != substr_count($key, '-')) {
      continue;
    }
    preg_match('/^(.+)-(.+)$/', $key, $matches);
    $type_key = $matches[1];
    $bundle_key = $matches[2];
    $sync_entities[$key]['version'] = Flow::getEntityTypeVersion($type_key, $bundle_key);
    $sync_entities[$key]['entity_type_name'] = $type_key;
    $sync_entities[$key]['bundle_name'] = $bundle_key;

    // If the Flow should pull manually, the Pool selection must also be set to Manual. Otherwise the entities won't
    // show up in the pull dashboard. To protect people from that scenario, we're changing that automatically.
    if (PullIntent::PULL_MANUALLY === $settings['import']) {
      foreach ($settings['import_pools'] as $pool => $setting) {
        if (Pool::POOL_USAGE_FORCE === $setting) {
          $sync_entities[$key]['import_pools'][$pool] = Pool::POOL_USAGE_ALLOW;
        }
      }
    }

    // If menu items should be exported automatically, the entity type option "export menu items"
    // have to be disabled to avoid a race condition.
    if (PushIntent::PUSH_DISABLED != $settings['export']) {
      if ($export_menu_items_automatically && isset($settings['handler_settings']['export_menu_items'])) {
        $sync_entities[$key]['handler_settings']['export_menu_items'] = 0;
      }
    }
  }
  $status = $config
    ->save();
  if ($status) {
    $this->messenger
      ->addMessage($this
      ->t('Saved the %label Flow.', [
      '%label' => $config
        ->label(),
    ]));
  }
  else {
    $this->messenger
      ->addMessage($this
      ->t('The %label Flow could not be saved.', [
      '%label' => $config
        ->label(),
    ]));
  }
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ('submit' == $triggering_element['#parents'][1]) {

    // Make sure that the export is executed.
    \Drupal::request()->query
      ->remove('destination');
    $form_state
      ->setRedirect('entity.cms_content_sync_flow.export', [
      'cms_content_sync_flow' => $config
        ->id(),
    ]);
  }
  else {
    $form_state
      ->setRedirect('entity.cms_content_sync_flow.collection');
  }
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('cms_content_sync_developer')) {
    $config_factory = $this->configFactory;
    $developer_config = $config_factory
      ->getEditable('cms_content_sync.developer');
    $mismatching_versions = $developer_config
      ->get('version_mismatch');
    if (!empty($mismatching_versions)) {
      unset($mismatching_versions[$config
        ->id()]);
      $developer_config
        ->set('version_mismatch', $mismatching_versions)
        ->save();
    }
  }

  // Invalidate the admin menu cache to ensure the Content Dashboard Menu gets shown or hidden.
  $this->cacheTagsInvalidator
    ->invalidateTags([
    'config:system.menu.admin',
  ]);
}