1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Drupal\Core\Url; |
4
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
|
5
|
|
|
use Drupal\taxonomy\Entity\Term; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Implements hook_entity_insert(). |
9
|
|
|
*/ |
10
|
|
|
function df_tools_slack_entity_insert(EntityInterface $entity) { |
11
|
|
|
if ($entity->getEntityTypeId() == 'node') { |
12
|
|
|
if ($entity->hasField('field_send_to_slack') && $entity->isPublished()) { |
13
|
|
|
// node info |
14
|
|
|
$entity_id = $entity->id(); |
15
|
|
|
$entity_title = $entity->getTitle(); |
16
|
|
|
$entity_url = Url::fromRoute('entity.node.canonical', ['node' => $entity_id], ['absolute' => TRUE])->toString(); |
17
|
|
|
$summary = $entity->get('body')->summary; |
18
|
|
|
// $author = strip_tags($entity->getAuthorName()); |
19
|
|
|
|
20
|
|
|
if ($entity->hasField('field_tags')) { |
21
|
|
|
$target_id = $entity->get('field_tags')->target_id; |
22
|
|
|
if ($target_id !== null) { |
23
|
|
|
$term = Term::load($target_id); |
24
|
|
|
$term_name = $term->getName(); |
25
|
|
|
// todo: select slack channel by taxonomy term, e.g. office location |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// site name |
30
|
|
|
$config = \Drupal::config('system.site'); |
31
|
|
|
$sitename = $config->get('name'); |
32
|
|
|
|
33
|
|
|
// slack info |
34
|
|
|
$config = \Drupal::config('slack.settings'); |
35
|
|
|
$channel = $config->get('slack_channel'); |
36
|
|
|
$username = $config->get('slack_username'); |
37
|
|
|
|
38
|
|
|
// build message |
39
|
|
|
$output = []; |
40
|
|
|
$output[] = "A new content has been published on $sitename: "; |
41
|
|
|
$output[] = "<$entity_url | $entity_title>"; |
42
|
|
|
if (isset($term) && !empty($term)) { |
43
|
|
|
$output[] = " tag: _$term_name _"; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
$output[] = "$summary"; |
46
|
|
|
// $output[] = "_Comment author_: $author"; |
47
|
|
|
|
48
|
|
|
// send slack message |
49
|
|
|
if ($entity->get('field_send_to_slack')->value == 1) { |
50
|
|
|
\Drupal::service('slack.slack_service') |
51
|
|
|
->sendMessage(implode("\n", $output), $channel, $username); |
52
|
|
|
|
53
|
|
|
\Drupal::logger('publish_to_slack')->info('Message sent.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths