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.
Passed
Push — 8.x-4.x ( 32209d...8dde4e )
by Kevin
04:52
created

df_tools_workflow_moderation_sidebar_alter()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 21
c 2
b 1
f 0
nc 14
nop 2
dl 0
loc 45
rs 7.6666

How to fix   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
}
69
70
/**
71
 * Implements hook_form_FORM_ID_alter().
72
 *
73
 * Add a form submission handler to the content moderation module's entity
74
 * moderation form in order to redirect users to the latest version of the
75
 * content when creating a new draft.
76
 */
77
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_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

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

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

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

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