You are here

public function EventTypeDefaultMessagesListForm::buildForm in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/EventTypeDefaultMessagesListForm.php \Drupal\rng\Form\EventTypeDefaultMessagesListForm::buildForm()

Form constructor.

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

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/EventTypeDefaultMessagesListForm.php, line 100

Class

EventTypeDefaultMessagesListForm
Form for event type default message.

Namespace

Drupal\rng\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
  $event_type = $this->entity;

  /** @var array $default_messages */
  $default_messages = $form_state
    ->get('default_messages');
  if (empty($default_messages)) {
    $default_messages = $event_type
      ->getDefaultMessages();
    $form_state
      ->set('default_messages', $default_messages);
  }

  // @TODO : Move this and other occurences into a common place?.
  // @see EventTypeDefaultMessagesAddForm::buildForm.
  $trigger_options = [
    'rng:custom:date' => $this
      ->t('To all registrations, on a date.'),
    (string) $this
      ->t('Registrations') => [
      'entity:registration:new' => $this
        ->t('To a single registration, when it is created.'),
      'entity:registration:update' => $this
        ->t('To a single registration, when it is updated.'),
    ],
  ];
  $trigger_labels = [
    'entity:registration:new' => $this
      ->t('Registration creation'),
    'entity:registration:update' => $this
      ->t('Registration updated'),
    'rng:custom:date' => $this
      ->t('Send on a date'),
  ];
  $form['messages'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'rng-default-messages-wrapper',
    ],
    '#tree' => TRUE,
  ];
  foreach ($default_messages as $key => $message) {
    $form['messages'][$key] = [
      '#type' => 'details',
      '#tree' => TRUE,
      '#title' => $this
        ->t('@label (@status)', [
        '@label' => isset($trigger_labels[$message['trigger']]) ? $trigger_labels[$message['trigger']] : $message['trigger'],
        '@status' => $message['status'] ? $this
          ->t('active') : $this
          ->t('disabled'),
      ]),
    ];
    $form['messages'][$key]['trigger'] = [
      '#type' => 'select',
      '#options' => $trigger_options,
      '#title' => $this
        ->t('Trigger'),
      '#default_value' => $message['trigger'],
    ];
    $form['messages'][$key]['status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#default_value' => $message['status'],
    ];
    $form['messages'][$key]['subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Subject'),
      '#default_value' => $message['subject'],
      '#required' => TRUE,
    ];
    $form['messages'][$key]['body'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Body'),
      '#default_value' => $message['body'],
      '#required' => TRUE,
    ];
    $form['messages'][$key]['remove'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Remove this message'),
      '#message_key' => $key,
      '#name' => 'button-message-remove-' . $key,
      '#submit' => [
        '::removeMessageCallback',
      ],
      '#ajax' => [
        'callback' => '::processMessageCallback',
        'wrapper' => 'rng-default-messages-wrapper',
      ],
    ];
  }
  return $form;
}