You are here

public function MessageController::addPage in Message UI 8

Generates output of all message template with permission to create.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse A render array for a list of the message templates that can be added; however, if there is only one message template defined for the site, the function will return a RedirectResponse to the message.add page for that one message template.

1 string reference to 'MessageController::addPage'
message_ui.routing.yml in ./message_ui.routing.yml
message_ui.routing.yml

File

src/Controller/MessageController.php, line 39

Class

MessageController
Controller for adding messages.

Namespace

Drupal\message_ui\Controller

Code

public function addPage() {
  $content = [];

  // Only use message templates the user has access to.
  foreach ($this
    ->entityTypeManager()
    ->getStorage('message_template')
    ->loadMultiple() as $template) {
    $access = $this
      ->entityTypeManager()
      ->getAccessControlHandler('message')
      ->createAccess($template
      ->id(), NULL, [], TRUE);
    if ($access
      ->isAllowed()) {
      $content[$template
        ->id()] = $template;
    }
  }

  // Bypass the message/add listing if only one message template is available.
  if (count($content) == 1) {
    $template = array_shift($content);
    return $this
      ->redirect('message_ui.add', [
      'message_template' => $template
        ->id(),
    ]);
  }

  // Return build array.
  if (!empty($content)) {
    return [
      '#theme' => 'message_add_list',
      '#content' => $content,
    ];
  }
  else {
    $url = Url::fromRoute('message.template_add');
    return [
      '#markup' => 'There are no messages templates. You can create a new message template <a href="/' . $url
        ->getInternalPath() . '">here</a>.',
    ];
  }
}