You are here

public function AddUpdateForm::submitForm in Optimizely 8

Same name and namespace in other branches
  1. 8.0 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

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);

  // For uniformity and ease of string matching, ensure each path starts
  // with a slash / except for the site-wide wildcard * or special aliases
  // such as <front>.
  self::checkPaths($path_array);
  $enabled = $form_state
    ->getValue('optimizely_enabled');

  // Process the submission to add or edit.
  // If no ID value is included in submission then add new entry.
  if (!isset($oid)) {
    \Drupal::database()
      ->insert('optimizely')
      ->fields([
      '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 {
    \Drupal::database()
      ->update('optimizely')
      ->fields([
      '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');
}