You are here

private function MailEditTemplateForm::getTemplate in Mail Editor 8

Load an email template from a combination string.

Parameters

string $id: A combination of the config entity's machine name and the email's name.

Return value

array Will contain the following elements:

  • subject - The email's subject line.
  • body - The email's body text.
  • config - The name of the config object this was found in.
  • name - The name of email template.
1 call to MailEditTemplateForm::getTemplate()
MailEditTemplateForm::buildForm in src/Form/MailEditTemplateForm.php
Form constructor.

File

src/Form/MailEditTemplateForm.php, line 200

Class

MailEditTemplateForm
Edit an email template.

Namespace

Drupal\mail_edit\Form

Code

private function getTemplate($id) {

  // Load the config entity.

  /* @var $config \Drupal\Core\Config\Config */
  $config = $this
    ->getConfig($id);

  // The email structure's name.
  $template_name = $this
    ->getEmailName($id);

  // If the config object was found, generate it.
  if (!empty($config)) {

    // Extract the specific config object that was requested.
    $template = $config
      ->get($template_name);
  }

  // If the config object didn't exist or the template wasn't defined, try
  // checking to make sure it was defined via the hook. This will allow email
  // config objects to be dynamically generated but block someone from being
  // able to create random config objects.
  if (empty($config) || !isset($template)) {
    $module_handler = \Drupal::moduleHandler();
    $module_name = $this
      ->getModuleName($id);
    $config_name = $this
      ->getConfigName($id);

    // Trigger hook_mail_edit_templates().
    $data = $module_handler
      ->invoke($module_name, 'mail_edit_templates');
    if (!isset($data, $data[$config_name], $data[$config_name][$template_name])) {
      throw new NotFoundHttpException();
    }
  }

  // If the template wasn't loaded, or doesn't exist, create an empty one so
  // that it can be saved.
  if (empty($template) || !is_array($template) || !isset($template['subject']) || !isset($template['body'])) {
    $template = [
      'subject' => '',
      'body' => '',
    ];
  }
  $template['config'] = $config
    ->getName();
  $template['name'] = $template_name;
  return $template;
}