You are here

public function TemplateEditForm::buildForm in Courier 8

Same name and namespace in other branches
  1. 2.x src/Form/TemplateEditForm.php \Drupal\courier\Form\TemplateEditForm::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 FormInterface::buildForm

File

src/Form/TemplateEditForm.php, line 79

Class

TemplateEditForm
Create a message.

Namespace

Drupal\courier\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, TemplateCollectionInterface $template_collection = NULL, ContentEntityTypeInterface $courier_channel = NULL) {
  if (!isset($template_collection) || !isset($courier_channel)) {
    throw new \Exception('Missing template collection or courier channel.');
  }
  if (!($message = $template_collection
    ->getTemplate($courier_channel
    ->id()))) {

    // Create it if it does not exist.

    /** @var \Drupal\courier\ChannelInterface $message */
    $message = $this->entityTypeManager
      ->getStorage($courier_channel
      ->id())
      ->create();

    // Saving the template collection will auto save the message entity.
    $template_collection
      ->setTemplate($message)
      ->save();
  }
  $form_state
    ->set('message_entity', $message);
  $form_state
    ->set('template_collection', $template_collection);
  $t_args = [
    '@channel' => $message
      ->getEntityType()
      ->getLabel(),
  ];

  // Entity form display.
  $display = entity_get_form_display($message
    ->getEntityTypeId(), $message
    ->getEntityTypeId(), 'default');
  $form_state
    ->set([
    'form_display',
  ], $display);
  $form['message'] = [
    '#tree' => TRUE,
  ];
  $display
    ->buildForm($message, $form['message'], $form_state);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#attributes' => [
      'class' => [
        'use-ajax-submit',
      ],
    ],
    '#type' => 'submit',
    '#value' => t('Save @channel', $t_args),
    '#button_type' => 'primary',
  ];
  $form['actions']['close'] = [
    '#attributes' => [
      'class' => [
        'use-ajax-submit',
      ],
    ],
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#submit' => [
      '::cancelForm',
    ],
  ];
  return $form;
}