GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — 8.x-4.x ( 77c6bb...2b207c )
by Kevin
05:48
created

df_tools_workflow_moderation_sidebar_alter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 3
b 1
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @file
5
 * Contains df_tools_workflow.module.
6
 */
7
8
use Drupal\Core\Entity\ContentEntityInterface;
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\Core\Render\Element;
12
use Drupal\Core\Url;
13
use Drupal\node\Entity\Node;
14
15
/**
16
 * Implements hook_modules_installed().
17
 */
18
function df_tools_workflow_modules_installed(array $modules) {
19
  // Don't do anything during config sync.
20
  if (\Drupal::isConfigSyncing()) {
21
    return;
22
  }
23
24
  // Grant 'Creators' the ability to create draft content and mark it for
25
  // review.
26
  if (in_array('df_tools_user', $modules)) {
27
    $creator_permissions = [
28
      'use moderation sidebar',
29
      // 'use moderation dashboard',
30
      'view latest version',
31
      'use editorial transition archive',
32
      'use editorial transition create_new_draft',
33
      'use editorial transition archived_published',
34
      'use editorial transition archived_draft',
35
      'use editorial transition review',
36
    ];
37
    user_role_grant_permissions('creator', $creator_permissions);
38
39
    // Grant 'Reviewers' the ability to review flagged content and publish it.
40
    $reviewer_permissions = [
41
      'use moderation sidebar',
42
      // 'use moderation dashboard',
43
      'view latest version',
44
      'use editorial transition archive',
45
      'use editorial transition create_new_draft',
46
      'use editorial transition publish',
47
      'use editorial transition archived_published',
48
      'use editorial transition archived_draft',
49
      'use editorial transition review',
50
    ];
51
    user_role_grant_permissions('reviewer', $reviewer_permissions);
52
  }
53
}
54
55
/**
56
 * Implements hook_form_alter().
57
 *
58
 * We auto-populate and hide the Revision log message on node/add/* forms.
59
 */
60
function df_tools_workflow_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
function df_tools_workflow_form_alter(&$form, /** @scrutinizer ignore-unused */ \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

60
function df_tools_workflow_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, /** @scrutinizer ignore-unused */ $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
  if (\Drupal::routeMatch()->getRouteName() == 'node.add') {
62
    $replace = [
63
      '@user' => \Drupal::currentUser()->getDisplayName(),
64
      '@date' => date("F jS, Y")
65
    ];
66
    $form['revision_log']['widget'][0]['value']['#default_value'] = t('Created by @user on @date.', $replace);
67
    $form['revision_log']['widget']['#access'] = FALSE;
68
  }
69
  $form['#attached']['library'][] = 'df_tools_workflow/df_tools_workflow';
70
}
71
72
/**
73
 * Implements hook_form_FORM_ID_alter().
74
 *
75
 * Add a form submission handler to the content moderation module's entity
76
 * moderation form in order to redirect users to the latest version of the
77
 * content when creating a new draft.
78
 */
79
function df_tools_workflow_form_content_moderation_entity_moderation_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_state is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
function df_tools_workflow_form_content_moderation_entity_moderation_form_alter(&$form, /** @scrutinizer ignore-unused */ \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $form_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
function df_tools_workflow_form_content_moderation_entity_moderation_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, /** @scrutinizer ignore-unused */ $form_id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
  $form['#submit'][] = 'df_tools_workflow_content_moderation_entity_moderation_form_submit';
81
}
82
83
/**
84
 * Form submission callback for content_moderation_entity_moderation_form.
85
 */
86
function df_tools_workflow_content_moderation_entity_moderation_form_submit($form, FormStateInterface $form_state) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
function df_tools_workflow_content_moderation_entity_moderation_form_submit(/** @scrutinizer ignore-unused */ $form, FormStateInterface $form_state) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
  /** @var ContentEntityInterface $entity */
88
  $entity = $form_state->get('entity');
89
90
  // Retrieve the content's new state.
91
  $new_state = $form_state->getValue('new_state');
92
93
  // When creating a new draft, redirect the user to the latest version of the
94
  // content.
95
  if ($new_state == 'draft') {
96
    $form_state->setRedirectUrl($entity->toUrl('latest-version'));
97
  }
98
}
99
100
/**
101
 * Implements hook_menu_local_tasks_alter();
102
 */
103
function df_tools_workflow_menu_local_tasks_alter(&$data, $route_name) {
0 ignored issues
show
Unused Code introduced by
The parameter $route_name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

103
function df_tools_workflow_menu_local_tasks_alter(&$data, /** @scrutinizer ignore-unused */ $route_name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
  // Runs when cache tag config:block.block.[ActiveTheme]_local_actions is invalidated.
105
  if (isset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab'])) {
106
    $url = $data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#link']['url'];
107
    $param = $url->getRouteParameters();
108
    if (isset($param['node'])) {
109
      $node = Node::load($param['node']);
110
      $moderation_info = \Drupal::service('content_moderation.moderation_information');
111
      $latest_node = $moderation_info->getLatestRevision('node', $node->id());
112
      // Modify the tabs when the canonical route is the latest revision.
113
      if ($latest_node->getRevisionId() == $node->getRevisionId()) {
114
        // Highlight the 'View' tab at /node/%/latest.
115
        if ($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']['#active']) {
116
          $data['tabs'][0]['entity.node.canonical']['#active'] = TRUE;
117
        }
118
        // Remove the latest version tab.
119
        unset($data['tabs'][0]['content_moderation.workflows:node.latest_version_tab']);
120
      }
121
    }
122
  }
123
}
124
125
/**
126
 * Implements hook_tokens().
127
 */
128
function df_tools_workflow_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
129
  $replacements = [];
130
131
  if ($type == 'node' && !empty($data['node'])) {
132
    /** @var \Drupal\node\NodeInterface $node */
133
    $node = $data['node'];
134
    $url_options = ['absolute' => TRUE];
135
    if (isset($options['langcode']) && $node->hasTranslation($options['langcode'])) {
136
      $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
137
      $langcode = $options['langcode'];
138
    }
139
    else {
140
      $langcode = NULL;
141
    }
142
143
    /** @var \Drupal\content_moderation\ModerationInformation $moderation_information */
144
    $moderation_information = \Drupal::service('content_moderation.moderation_information');
145
146
    /** @var \Drupal\node\NodeInterface $latest */
147
    if (!($latest = $moderation_information->getLatestRevision('node', $node->id()))) {
148
      $latest = $node;
149
    }
150
151
    if ($langcode) {
152
      $latest = $latest->getTranslation($langcode);
153
    }
154
155
    foreach ($tokens as $name => $original) {
156
      switch ($name) {
157
        case 'latest_author':
158
          $replacements[$original] = $latest->getRevisionUser()->label();
159
          break;
160
        case 'latest_url':
161
          if ($node->getRevisionId() !== $latest->getRevisionId()) {
162
            $replacements[$original] = Url::fromRoute('entity.node.latest_version', [
163
              'node' => $node->id(),
164
            ], $url_options)->toString();
165
          }
166
          else {
167
            $replacements[$original] = $latest->toUrl('canonical', $url_options)->toString();
168
          }
169
          break;
170
      }
171
    }
172
173
    $token_service = \Drupal::token();
174
    if ($author_tokens = $token_service->findWithPrefix($tokens, 'latest_author')) {
175
      $replacements += $token_service->generate('user', $author_tokens, ['user' => $latest->getRevisionUser()], $options, $bubbleable_metadata);
176
    }
177
  }
178
179
  return $replacements;
180
}
181
182
/**
183
 * Implements hook_library_info_alter().
184
 */
185
function df_tools_workflow_library_info_alter(&$libraries, $extension) {
186
  if ($extension === 'moderation_note') {
187
    unset($libraries['main']['dependencies']);
188
  }
189
}
190
191
/**
192
 * Implements hook_moderation_sidebar_alter().
193
 */
194
function df_tools_workflow_moderation_sidebar_alter(array &$build, EntityInterface $entity) {
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

194
function df_tools_workflow_moderation_sidebar_alter(array &$build, /** @scrutinizer ignore-unused */ EntityInterface $entity) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    $build['actions']['primary']['view_latest']['#attributes']['class'][]
196
      = 'view-latest';
197
    $build['actions']['primary']['view_default']['#attributes']['class'][]
198
      = 'view-default';
199
    $build['actions']['primary']['edit']['#attributes']['class'][] = 'edit';
200
}
201