MessageThreadBreadcrumbs.php 3.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?php

namespace Drupal\message_thread\Plugin\Breadcrumbs;

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\message_thread\Entity\MessageThread;

class MessageThreadBreadcrumbs implements BreadcrumbBuilderInterface{
  /**
   * {@inheritdoc}
   */
  public function applies(RouteMatchInterface $attributes) {
//    ksm($attributes);
    $parameters = $attributes->getParameters()->all();

    if (!empty($parameters['message_thread'])) {
      return TRUE;
    }
    if (!empty($parameters['message'])) {
      return TRUE;
    }
  }


  /**
   * {@inheritdoc}
   */
  public function build(RouteMatchInterface $route_match) {
//    ksm($route_match);
    $breadcrumb = new Breadcrumb();
    $parameters = $route_match->getParameters()->all();
    $user = \Drupal::currentUser();
New Zeal's avatar
New Zeal committed
36
    if (!empty($parameters['message_thread']) && is_object($parameters['message_thread'])) {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      $message_thread = $parameters['message_thread'];

      // Get the parent messages link
      // We use the current user as a reference since user can only view own messages
      $thread_template = $message_thread->getTemplate();

      $breadcrumb->addLink(Link::createFromRoute(
        'Messages',
        'message_thread.' . $thread_template->id(),
        ['user' => $user->id()]
      ));

//      $breadcrumb->addLink(Link::createFromRoute(
//        $message_thread->get('field_thread_title')->getValue()[0]['value'],
//        'entity.message_thread.canonical', [
//        'message_thread' => $message_thread->id()
//      ]));

    }
    if (!empty($parameters['message'])) {

      $message = $parameters['message'];
      if ($message->bundle()) {
        $message_thread = $this->messageThreadRelationship($message->id());
        $thread_template = $message_thread->getTemplate();

        $breadcrumb->addLink(Link::createFromRoute(
          $thread_template->label(),
          'message_thread.' . $thread_template->id(),
          ['user' => $user->id()]
        ));

        $breadcrumb->addLink(Link::createFromRoute(
          $message_thread->get('field_thread_title')->getValue()[0]['value'],
          'entity.message_thread.canonical', [
            'message_thread' => $message_thread->id()
New Zeal's avatar
New Zeal committed
73
          ]
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        ));
// Probably don't need the current message
//        $label = isset( $message->get('field_message_private_subject')->getValue()[0]['value'])
//          ?  $message->get('field_message_private_subject')->getValue()[0]['value'] :
//          'Message';
//
//        $breadcrumb->addLink(Link::createFromRoute(
//           $label,
//          'entity.message.canonical', [
//          'message' => $message->id()
//        ]));
      }


    }

New Zeal's avatar
New Zeal committed
90 91 92
    $contexts = [
      'url'
    ];
93

New Zeal's avatar
New Zeal committed
94
    $breadcrumb->addCacheContexts($contexts);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    return $breadcrumb;
  }

  /*
   * Helper function to relate a message to its thread
   */
  function messageThreadRelationship($mid) {
    $thread_id = db_select('message_thread_index', 'mdi')
      ->condition('mdi.mid', $mid)
      ->fields('mdi', ['thread_id'])
      ->execute()
      ->fetchField();

    return MessageThread::load($thread_id);
  }
}