message_notification-1.0.1-beta1/message_notification.module

message_notification.module
<?php

/**
 * @file
 * Contains message_notification.module.
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Url;

/**
 * Implements hook_toolbar().
 */
function message_notification_toolbar() {
  $user = \Drupal::currentUser();

  $items = [];
  $items['notifications'] = [
    '#cache' => [
      'contexts' => [
        'user',
      ],
    ],
  ];

  if ($user->isAuthenticated()) {
    $items['notifications'] += [
      '#type' => 'toolbar_item',
      'tab' => [
        '#type' => 'link',
        '#title' => t('Notifications'),
        '#url' => URL::fromRoute('<none>'),
        '#attributes' => [
          'title' => t('Notifications'),
          'class' => ['toolbar-icon', 'toolbar-icon-notifications'],
        ],
      ],
      'tray' => [
        'children' => [
          '#lazy_builder' => [
            'message_notification.lazy_builders:lazyNotifications',
            [],
          ],
          '#create_placeholder' => TRUE,
          '#cache' => [
            'tags' => ['message_list'],
            'contexts' => ['user'],
          ],
        ],
      ],
      '#weight' => -5,
      '#attached' => [
        'library' => [
          'message_notification/notification_list',
          'message_notification/notification_counter',
          'message_notification/notification_toolbar',
        ],
      ],
    ];
  }

  return $items;
}

/**
 * Implements hook_entity_base_field_info().
 */
function message_notification_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'message') {
    $fields['recipients'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Recipients list'))
      ->setDescription(t('List of users that received message.'))
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayConfigurable('form', TRUE);

    return $fields;
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function message_notification_preprocess_toolbar(array &$variables) {
  if (isset($variables['tabs']['notifications'])) {
    $variables['tabs']['notifications']['attributes']->addClass('toolbar-tab__notification');
  }
}

/**
 * Implements template_preprocess_HOOK().
 */
function message_notification_preprocess_message__notification(array &$variables) {
  /** @var \Drupal\message\MessageInterface $message */
  $message = $variables['message'];
  $owner = $message->getOwner();

  if (!$owner->get('user_picture')->isEmpty()) {
    $variables['user_picture'] = $owner
      ->get('user_picture')
      ->view('notification');
  }
  else {
    $variables['user_first_letter'] = mb_substr($owner->getDisplayName(), 0, 1);
  }

  $variables['created'] = $variables['message']->getCreatedTime();
}

/**
 * Implements hook_theme_registry_alter().
 */
function message_notification_theme_registry_alter(array &$theme_registry) {
  if (isset($theme_registry['message__notification'])) {
    $theme_registry['message__notification']['path'] = \Drupal::service('module_handler')->getModule('message_notification')->getPath() . '/templates';
    $theme_registry['message__notification']['template'] = 'message--notification';
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc