From bba47976435904ce22ac4c1491bc51267525cc66 Mon Sep 17 00:00:00 2001
From: Kent Shelley <kent@passingphase.co.nz>
Date: Tue, 12 Jun 2018 19:33:42 +1200
Subject: [PATCH] message history and small fixes

---
 CHANGELOG.md                                  |  7 +++++-
 message_thread.module                         | 22 ++++++++++++++++++-
 .../Breadcrumbs/MessageThreadBreadcrumbs.php  |  9 +++++---
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 318db2c..b0bda7a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,4 +17,9 @@ Add suitable breadcrumb for navigation
 Modify contextual dropdowns for proper access
 
 8.x-1.0-alpha7
-Remove reference to MessagePrivateAccessControlHandler which doesn't exist yet
\ No newline at end of file
+Remove reference to MessagePrivateAccessControlHandler which doesn't exist yet
+
+8.x-1.0-alpha8
+Add route check on message_ui edit links
+Add cache context to breadcrumbs
+Add message history functionality
\ No newline at end of file
diff --git a/message_thread.module b/message_thread.module
index c0ea59f..1af2efb 100644
--- a/message_thread.module
+++ b/message_thread.module
@@ -46,7 +46,6 @@ function message_thread_help($route_name, RouteMatchInterface $arg) {
 function message_thread_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
   // When a message is created we need to ensure that the participants are included
   // This is best done in the validation hook to ensure participants are not missed from submit functions
-
   $thread_templates = \Drupal::entityTypeManager()->getListBuilder('message_thread_template')->load();
   foreach ($thread_templates as $name => $template) {
     $settings = $template->getSettings();
@@ -113,9 +112,15 @@ function message_thread_submit_message(array &$form, FormStateInterface $form_st
  *
  * @param $variables
  * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ *
+ * todo should use this hook: message_ui_message_ui_views_contextual_links_info
  */
 function message_thread_preprocess_links(&$variables) {
 
+  if (!message_thread_is_message_route()) {
+    return;
+  }
+
   if ($variables['theme_hook_original'] == 'links__dropbutton__operations') {
     $account = \Drupal::currentUser();
 
@@ -143,6 +148,21 @@ function message_thread_preprocess_links(&$variables) {
   }
 }
 
+/*
+ * Test if the current route is a message thread or message route
+ */
+function message_thread_is_message_route() {
+  $message = \Drupal::routeMatch()->getParameter('message');
+  if ($message) {
+    return TRUE;
+  }
+  $message_thread = \Drupal::routeMatch()->getParameter('message>_thread');
+  if ($message_thread) {
+    return TRUE;
+  }
+  return FALSE;
+}
+
 /**
  * Implements hook_entity_extra_field_info().
  */
diff --git a/src/Plugin/Breadcrumbs/MessageThreadBreadcrumbs.php b/src/Plugin/Breadcrumbs/MessageThreadBreadcrumbs.php
index cb4bf50..e9bc3f2 100644
--- a/src/Plugin/Breadcrumbs/MessageThreadBreadcrumbs.php
+++ b/src/Plugin/Breadcrumbs/MessageThreadBreadcrumbs.php
@@ -33,7 +33,7 @@ class MessageThreadBreadcrumbs implements BreadcrumbBuilderInterface{
     $breadcrumb = new Breadcrumb();
     $parameters = $route_match->getParameters()->all();
     $user = \Drupal::currentUser();
-    if (!empty($parameters['message_thread'])) {
+    if (!empty($parameters['message_thread']) && is_object($parameters['message_thread'])) {
       $message_thread = $parameters['message_thread'];
 
       // Get the parent messages link
@@ -57,7 +57,6 @@ class MessageThreadBreadcrumbs implements BreadcrumbBuilderInterface{
 
       $message = $parameters['message'];
       if ($message->bundle()) {
-
         $message_thread = $this->messageThreadRelationship($message->id());
         $thread_template = $message_thread->getTemplate();
 
@@ -71,7 +70,7 @@ class MessageThreadBreadcrumbs implements BreadcrumbBuilderInterface{
           $message_thread->get('field_thread_title')->getValue()[0]['value'],
           'entity.message_thread.canonical', [
             'message_thread' => $message_thread->id()
-            ]
+          ]
         ));
 // Probably don't need the current message
 //        $label = isset( $message->get('field_message_private_subject')->getValue()[0]['value'])
@@ -88,7 +87,11 @@ class MessageThreadBreadcrumbs implements BreadcrumbBuilderInterface{
 
     }
 
+    $contexts = [
+      'url'
+    ];
 
+    $breadcrumb->addCacheContexts($contexts);
     return $breadcrumb;
   }
 
-- 
GitLab