foldershare-8.x-1.2/foldershare.search.inc

foldershare.search.inc
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
36
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
 
/**
 * @file
 * Implements search hooks for the module.
 */
 
use Drupal\user\RoleInterface;
use Drupal\Core\Entity\EntityInterface;
 
use Drupal\foldershare\Entity\FolderShare;
 
/**
 * Implements hook_foldershare_update_index().
 */
function comment_foldershare_update_index(EntityInterface $item) {
  // This function is closely modeled after comment_node_update_index() in
  // the Drupal core Comment module. That function applies only to node
  // entities, while this function applies to FolderShare entities.
  //
  if (\Drupal::hasService('comment.manager') === FALSE) {
    // While this hook should only be called when the comment module is
    // installed, and when it is installed there should be a comment
    // manager, let us be extra sure and do nothing if the service
    // is absent.
    return '';
  }
 
  $indexComments = &drupal_static(__FUNCTION__);
 
  // Determine if comments should be included in the search index. It would
  // be inappropriate to include them if general users cannot view comments.
  if ($indexComments === NULL) {
    // Do not index in the following three cases:
    //
    // 1. 'Authenticated user' can search content but can't access comments.
    //
    // 2. 'Anonymous user' can search content but can't access comments.
    //
    // 3. Any role can search content but can't access comments and access
    //    comments is not granted by the 'authenticated user' role. In this
    //    case all users might have both permissions from various roles but
    //    it is also possible to set up a user to have only search content
    //    and so a user edit could change the security situation so it is
    //    not safe to index the comments.
    $indexComments = TRUE;
 
    // Get the current roles.
    $roles = \Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple();
 
    // Can authenticated users access comments?
    $authCanAccess = $roles[RoleInterface::AUTHENTICATED_ID]->hasPermission('access comments');
 
    // Loop through all of the roles.
    foreach ($roles as $rid => $role) {
      if ($role->hasPermission('search content') === TRUE &&
          $role->hasPermission('access comments') === FALSE) {
        // The role supports searching, but not comments.
        if ($rid === RoleInterface::AUTHENTICATED_ID ||
            $rid === RoleInterface::ANONYMOUS_ID ||
            $authCanAccess === FALSE) {
          // The role is either for authenticated users or anonymous users,
          // and they cannot access comments. Do not include them in the
          // search index.
          $indexComments = FALSE;
          break;
        }
      }
    }
  }
 
  if ($indexComments === FALSE) {
    // Do not include comments. Return nothing.
    return '';
  }
 
  // Do include comments in the search index.
  //
  // Loop through all the comment fields for our entity type.
  $mgr     = \Drupal::service('comment.manager');
  $builder = \Drupal::entityTypeManager()->getViewBuilder('comment');
  $storage = \Drupal::entityTypeManager()->getStorage('comment');
  $fields  = $mgr->getFields(FolderShare::ENTITY_TYPE_ID);
  $build   = [];
 
  foreach ($fields as $fieldName => $info) {
    // Skip fields that the entity does not have.
    if ($item->hasField($fieldName) === FALSE) {
      continue;
    }
 
    // Get the definition and characteristics.
    $def     = $item->getFieldDefinition($fieldName);
    $mode    = $def->getSetting('default_mode');
    $perPage = $def->getSetting('per_page');
 
    // If the field is visible, get the field's comment, build a
    // view of it, and append it to the build we'll be returning.
    if ($item->get($fieldName)->status === TRUE) {
      $comments = $storage->loadThread($item, $fieldName, $mode, $perPage);
      if ($comments !== NULL) {
        $build[] = $builder->viewMultiple($comments);
      }
    }
  }
 
  // Render and return the comments.
  return \Drupal::service('renderer')->renderPlain($build);
}

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

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