You are here

private function MailListController::getAllTemplates in Mail Editor 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/MailListController.php \Drupal\mail_edit\Controller\MailListController::getAllTemplates()

Get a list of all templates provided by the site.

Return value

array A list of all templates provided by module hooks.

1 call to MailListController::getAllTemplates()
MailListController::listAll in src/Controller/MailListController.php
List the emails.

File

src/Controller/MailListController.php, line 64

Class

MailListController
List all of the emails that may be edited by the module.

Namespace

Drupal\mail_edit\Controller

Code

private function getAllTemplates() {
  $all_templates = [];
  $module_handler = \Drupal::moduleHandler();

  // Trigger hook_mail_edit_templates().
  // Get a list of the email templates as defined by other modules.
  foreach ($module_handler
    ->invokeAll('mail_edit_templates') as $config_name => $templates) {
    $config_data = $this
      ->config($config_name)
      ->getRawData();

    // Make sure data was actually found.
    if (empty($config_data)) {
      $config_data = [];
    }

    // Process each of the defined templates.
    foreach ($templates as $key => $label) {

      // Keys will not be numeric if they are in $key => $label format, so if
      // the key is numeric then it was provided as just a list of keys.
      if (is_numeric($key)) {
        $key = $label;
      }

      // Make sure the config structure exists.
      if (!isset($config_data[$key])) {
        $config_data[$key] = [
          'subject' => '',
          'body' => '',
        ];
      }
      $data = $config_data[$key];

      // If an email's description is provided then use it, otherwise just
      // use the email's subject line.
      if ($key != $label) {
        $data['description'] = $label;
      }
      else {
        $data['description'] = $data['subject'];
      }
      $all_templates[$config_name][$key] = $data;
    }
  }

  // Trigger hook_mail_edit_templates_list_alter().
  // Allow modules to adjust the list of available templates, e.g. to remove
  // items from the list for security purposes.
  $module_handler
    ->alter('mail_edit_templates_list', $all_templates);
  return $all_templates;
}