You are here

public function AddUpdateForm::submitForm in Optimizely 8.0

Same name and namespace in other branches
  1. 8 src/AddUpdateForm.php \Drupal\optimizely\AddUpdateForm::submitForm()

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 FormInterface::submitForm

File

src/AddUpdateForm.php, line 256
Contains \Drupal\optimizely\AddUpdateForm

Class

AddUpdateForm
Implements the form for the Add Projects page.

Namespace

Drupal\optimizely

Code

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

  // Catch form submitted values and prep for processing
  $oid = $form_state
    ->getValue('optimizely_oid');
  $project_title = $form_state
    ->getValue('optimizely_project_title');
  $project_code = $form_state
    ->getValue('optimizely_project_code');
  $path_array = preg_split('/[\\r\\n]+/', $form_state
    ->getValue('optimizely_path'), -1, PREG_SPLIT_NO_EMPTY);
  $enabled = $form_state
    ->getValue('optimizely_enabled');

  // Process add or edit submission
  // No ID value included in submission - add new entry
  if (!isset($oid)) {
    db_insert('optimizely')
      ->fields(array(
      'project_title' => $project_title,
      'path' => serialize($path_array),
      'project_code' => $project_code,
      'enabled' => $enabled,
    ))
      ->execute();
    drupal_set_message(t('The project entry has been created.'), 'status');

    // Rebuild the provided paths to ensure Optimizely javascript is now included on paths
    if ($enabled) {
      CacheRefresher::doRefresh($path_array);
    }
  }
  else {
    db_update('optimizely')
      ->fields(array(
      'project_title' => $project_title,
      'path' => serialize($path_array),
      'project_code' => $project_code,
      'enabled' => $enabled,
    ))
      ->condition('oid', $oid)
      ->execute();
    drupal_set_message(t('The project entry has been updated.'), 'status');

    // Path originally set for project - to be compared to the updated value
    // to determine what cache paths needs to be refreshed
    $original_path_array = preg_split('/[\\r\\n]+/', $form_state
      ->getValue('optimizely_original_path'), -1, PREG_SPLIT_NO_EMPTY);
    CacheRefresher::doRefresh($path_array, $original_path_array);
  }

  // Return to project listing page
  $form_state
    ->setRedirect('optimizely.listing');
}