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-212 ( 6eb0ba...5e20ae )
by Kevin
13:55
created

df_tools_workflow_tokens()   C

Complexity

Conditions 12
Paths 17

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
c 1
b 0
f 0
nc 17
nop 5
dl 0
loc 52
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Contains df_tools_workflow.module.
6
 */
7
8
use Drupal\Core\Entity\ContentEntityInterface;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Render\Element;
11
use Drupal\Core\Url;
12
use Drupal\node\Entity\Node;
13
14
/**
15
 * Implements hook_modules_installed().
16
 */
17
function df_tools_workflow_modules_installed(array $modules) {
18
  // Don't do anything during config sync.
19
  if (\Drupal::isConfigSyncing()) {
20
    return;
21
  }
22
23
  // Grant 'Creators' the ability to create draft content and mark it for
24
  // review.
25
  if (in_array('df_tools_user', $modules)) {
26
    $creator_permissions = [
27
      'use moderation sidebar',
28
      // 'use moderation dashboard',
29
      'view latest version',
30
      'use editorial transition archive',
31
      'use editorial transition create_new_draft',
32
      'use editorial transition archived_published',
33
      'use editorial transition archived_draft',
34
      'use editorial transition review',
35
    ];
36
    user_role_grant_permissions('creator', $creator_permissions);
37
38
    // Grant 'Reviewers' the ability to review flagged content and publish it.
39
    $reviewer_permissions = [
40
      'use moderation sidebar',
41
      // 'use moderation dashboard',
42
      'view latest version',
43
      'use editorial transition archive',
44
      'use editorial transition create_new_draft',
45
      'use editorial transition publish',
46
      'use editorial transition archived_published',
47
      'use editorial transition archived_draft',
48
      'use editorial transition review',
49
    ];
50
    user_role_grant_permissions('reviewer', $reviewer_permissions);
51
  }
52
}
53
54
/**
55
 * Implements hook_form_alter().
56
 *
57
 * We auto-populate and hide the Revision log message on node/add/* forms.
58
 */
59
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_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

59
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...
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

59
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...
60
  if (\Drupal::routeMatch()->getRouteName() == 'node.add') {
61
    $replace = [
62
      '@user' => \Drupal::currentUser()->getDisplayName(),
63
      '@date' => date("F jS, Y")
64
    ];
65
    $form['revision_log']['widget'][0]['value']['#default_value'] = t('Created by @user on @date.', $replace);
66
    $form['revision_log']['widget']['#access'] = FALSE;
67
  }
68
  $form['#attached']['library'][] = 'df_tools_workflow/df_tools_workflow';
69
}
70
71
/**
72
 * Implements hook_form_FORM_ID_alter().
73
 *
74
 * Add a form submission handler to the content moderation module's entity
75
 * moderation form in order to redirect users to the latest version of the
76
 * content when creating a new draft.
77
 */
78
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

78
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

78
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...
79
  $form['#submit'][] = 'df_tools_workflow_content_moderation_entity_moderation_form_submit';
80
}
81
82
/**
83
 * Form submission callback for content_moderation_entity_moderation_form.
84
 */
85
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

85
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...
86
  /** @var ContentEntityInterface $entity */
87
  $entity = $form_state->get('entity');
88
89
  // Retrieve the content's new state.
90
  $new_state = $form_state->getValue('new_state');
91
92
  // When creating a new draft, redirect the user to the latest version of the
93
  // content.
94
  if ($new_state == 'draft') {
95
    $form_state->setRedirectUrl($entity->toUrl('latest-version'));
96
  }
97
}
98
99
/**
100
 * Implements hook_menu_local_tasks_alter();
101
 */
102
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

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