You are here

public function TemplateForm::form in Workbench Email 8

Same name and namespace in other branches
  1. 2.x src/Form/TemplateForm.php \Drupal\workbench_email\Form\TemplateForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/TemplateForm.php, line 104

Class

TemplateForm
Class TemplateForm.

Namespace

Drupal\workbench_email\Form

Code

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

  /** @var \Drupal\workbench_email\TemplateInterface $workbench_email_template */
  $workbench_email_template = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $workbench_email_template
      ->label(),
    '#description' => $this
      ->t("Label for the Email Template."),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $workbench_email_template
      ->id(),
    '#maxlength' => EntityTypeInterface::ID_MAX_LENGTH,
    '#machine_name' => [
      'exists' => '\\Drupal\\workbench_email\\Entity\\Template::load',
    ],
    '#disabled' => !$workbench_email_template
      ->isNew(),
  ];
  $form['contents'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Email contents'),
    '#open' => TRUE,
  ];
  $form['contents']['subject'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#maxlength' => 255,
    '#default_value' => $workbench_email_template
      ->getSubject(),
    '#description' => $this
      ->t('Email subject. You can use tokens like [node:title] depending on the entity type being updated.'),
    '#required' => TRUE,
  ];
  $form['contents']['replyTo'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Reply-To'),
    '#maxlength' => 255,
    '#default_value' => $workbench_email_template
      ->getReplyTo(),
    '#description' => $this
      ->t('Email Reply-To. You can use tokens like [node:author:mail] depending on the entity type being updated.'),
    '#required' => FALSE,
  ];
  $default_body = $workbench_email_template
    ->getBody() + [
    'value' => '',
    'format' => 'plain_text',
  ];
  $form['contents']['body'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Body'),
    '#description' => $this
      ->t('Email body, you may use tokens like [node:title] depending on the entity type being updated.'),
    '#required' => TRUE,
    '#format' => $default_body['format'],
    '#default_value' => $default_body['value'],
  ];

  // Display a token browser if the Token module is available.
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['contents']['tokens'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'node',
      ],
    ];
    $form['contents']['tokens-warning'] = [
      '#type' => 'item',
      '#title' => '',
      '#markup' => '<b>Warning:</b> The token browser currently only shows node tokens. However, there may be other tokens available depending on the entity type being updated.',
    ];
  }

  // Recipient types.
  $recipient_types = $workbench_email_template
    ->recipientTypes();
  $form['enabled_recipient_types'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enabled recipient types'),
    '#required' => TRUE,
    '#options' => [],
    '#default_value' => [],
  ];
  $form['recipient_types_settings'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Recipient type configuration'),
  ];

  /** @var \Drupal\workbench_email\Plugin\RecipientTypeInterface $plugin */
  foreach ($recipient_types as $plugin_id => $plugin) {
    $form['enabled_recipient_types']['#options'][$plugin_id] = $plugin
      ->getLabel();
    if ($plugin
      ->isEnabled()) {
      $form['enabled_recipient_types']['#default_value'][$plugin_id] = $plugin_id;
    }
    if ($plugin
      ->hasFormClass('configure')) {
      $form['recipient_types']['settings'][$plugin_id] = [
        '#tree' => TRUE,
        '#access' => FALSE,
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $plugin
          ->getLabel(),
        '#group' => 'recipient_types_settings',
        '#parents' => [
          'recipient_types',
          $plugin_id,
          'settings',
        ],
      ];
      $subform_state = SubformState::createForSubform($form['recipient_types']['settings'][$plugin_id], $form, $form_state);
      $configurationForm = $plugin
        ->buildConfigurationForm($form['recipient_types']['settings'][$plugin_id], $subform_state);
      if ($configurationForm) {
        $form['recipient_types']['settings'][$plugin_id] += $configurationForm;
        $form['recipient_types']['settings'][$plugin_id]['#access'] = TRUE;
      }
    }
  }

  // Bundles.
  $bundle_options = [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$this
      ->isModeratableEntityType($entity_type)) {

      // Irrelevant - continue.
      continue;
    }
    $bundles = $this->entityBundleInfo
      ->getBundleInfo($entity_type_id);
    if ($bundle_entity_type = $entity_type
      ->getBundleEntityType()) {
      $bundle_storage = $this->entityTypeManager
        ->getStorage($bundle_entity_type);
      $bundle_entities = $bundle_storage
        ->loadMultiple(array_keys($bundles));
      foreach ($bundle_entities as $bundle_id => $bundle) {
        if ($this
          ->isModeratableBundle($entity_type, $bundle_id)) {
          $bundle_options["{$entity_type_id}:{$bundle_id}"] = $bundle
            ->label() . ' (' . $entity_type
            ->getLabel() . ')';
        }
      }
    }
    elseif ($this
      ->isModeratableBundle($entity_type, $entity_type_id)) {
      $bundle_options["{$entity_type_id}:{$entity_type_id}"] = $entity_type
        ->getLabel() . ' (' . $entity_type
        ->getLabel() . ')';
    }
  }
  $form['bundles'] = [
    '#type' => 'checkboxes',
    '#options' => $bundle_options,
    '#access' => !empty($bundle_options),
    '#default_value' => $workbench_email_template
      ->getBundles(),
    '#title' => $this
      ->t('Bundles'),
    '#description' => $this
      ->t('Limit to the following bundles. Select none to include all bundles.'),
  ];
  return $form;
}