You are here

public function RuleFormController::save in JS injector 8

File

lib/Drupal/js_injector/RuleFormController.php, line 135
Contains \Drupal\js_injector\RuleFormController.

Class

RuleFormController
Form controller for the shortcut set entity edit forms.

Namespace

Drupal\js_injector

Code

public function save(array $form, array &$form_state) {
  $entity = $this->entity;

  // Prevent leading and trailing spaces in rule names.
  $entity
    ->set('label', trim($entity
    ->get('label')));

  // If preprocess is ticked, then ensure inline is not (they conflict).
  if ($entity
    ->get('preprocess')) {
    $entity
      ->set('inline', 0);
  }

  // Write the JavaScript file to the filesystem.
  $file_written = file_unmanaged_save_data($entity
    ->get('js'), _js_injector_rule_uri($entity
    ->id()), FILE_EXISTS_REPLACE);
  $url = $entity
    ->url();
  if ($entity
    ->save() == SAVED_UPDATED) {
    drupal_set_message(t('Rule %label has been updated.', array(
      '%label' => $entity
        ->label(),
    )));
    watchdog('js_injector', 'Rule %label has been updated.', array(
      '%label' => $entity
        ->label(),
    ), WATCHDOG_NOTICE, l(t('Edit'), $url));
  }
  else {
    drupal_set_message(t('Rule %label has been added.', array(
      '%label' => $entity
        ->label(),
    )));
    watchdog('js_injector', 'Rule %label has been added.', array(
      '%label' => $entity
        ->label(),
    ), WATCHDOG_NOTICE, l(t('Edit'), $url));
  }
  $form_state['redirect'] = 'admin/config/development/js-injector';
}