You are here

public function EventTypeForm::save in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::save()
  2. 3.x src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::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/EventTypeForm.php, line 293

Class

EventTypeForm
Form controller for event config entities.

Namespace

Drupal\rng\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\rng\EventTypeInterface $event_type */
  $event_type = $this
    ->getEntity();
  if ($event_type
    ->isNew()) {
    if ($this->moduleHandler
      ->moduleExists('node') && $form_state
      ->getValue('entity_type') == 'node') {
      $node_type = $this
        ->createContentType('Event');
      $t_args = [
        '%label' => $node_type
          ->label(),
        ':url' => $node_type
          ->toUrl()
          ->toString(),
      ];
      drupal_set_message(t('The content type <a href=":url">%label</a> has been added.', $t_args));
      $event_type
        ->setEventEntityTypeId($node_type
        ->getEntityType()
        ->getBundleOf());
      $event_type
        ->setEventBundle($node_type
        ->id());
    }
    else {
      $bundle = explode('.', $form_state
        ->getValue('bundle'));
      $event_type
        ->setEventEntityTypeId($bundle[0]);
      $event_type
        ->setEventBundle($bundle[1]);
    }
  }
  foreach ($form_state
    ->getValue([
    'registrants',
    'registrants',
  ]) as $row_key => $row) {
    list($entity_type, $bundle) = explode(':', $row_key);
    $event_type
      ->setIdentityTypeCreate($entity_type, $bundle, !empty($row['create']));
    $event_type
      ->setIdentityTypeReference($entity_type, $bundle, !empty($row['existing']));
    $event_type
      ->setIdentityTypeEntityFormMode($entity_type, $bundle, $row['entity_form_mode']);
  }
  $event_type
    ->setDefaultRegistrantType($form_state
    ->getValue([
    'registrants',
    'registrant_type',
  ]));

  // Set to the access operation for event.
  $op = $form_state
    ->getValue('mirror_update') ? 'update' : '';
  $event_type
    ->setEventManageOperation($op);
  $status = $event_type
    ->save();

  // Create default rules.
  if ($status == SAVED_NEW) {
    $this
      ->createDefaultRules($event_type
      ->getEventEntityTypeId(), $event_type
      ->getEventBundle());
  }
  $message = $status == SAVED_UPDATED ? '%label event type updated.' : '%label event type added.';
  $url = $event_type
    ->urlInfo();
  $t_args = [
    '%label' => $event_type
      ->id(),
    'link' => $this
      ->l(t('Edit'), $url),
  ];
  drupal_set_message($this
    ->t($message, $t_args));
  $this
    ->logger('rng')
    ->notice($message, $t_args);
}