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-translate ( d1297f )
by Kevin
09:09
created

df_tools_translation_enable_translation()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
rs 8.0555
c 0
b 0
f 0
cc 9
nc 10
nop 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains df_tools_translation.module.
6
 */
7
8
use Drupal\Core\Entity\FieldableEntityInterface;
9
10
/**
11
 * Enables translation for the given entity bundles and all their fields.
12
 *
13
 * @param array $entity_info An array mapping entity types to arrays of bundles.
14
 */
15
function df_tools_translation_enable_translation($entity_info) {
16
  // Enable translation for all of our entities/bundles.
17
  $type_manager = \Drupal::entityTypeManager();
18
  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
19
  $field_manager = \Drupal::service('entity_field.manager');
20
  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */
21
  $translation_manager = \Drupal::service('content_translation.manager');
22
  $supported_types = $translation_manager->getSupportedEntityTypes();
23
  foreach ($entity_info as $entity_type_id => $bundles) {
24
    foreach ($bundles as $bundle) {
25
      // Store whether a bundle has translation enabled or not.
26
      if (isset($supported_types[$entity_type_id])) {
27
        $translation_manager->setEnabled($entity_type_id, $bundle, TRUE);
28
      }
29
      // Make every field translatable as well.
30
      $entity_type = $type_manager->getDefinition($entity_type_id);
31
      if ($entity_type && $entity_type->isSubclassOf(FieldableEntityInterface::class)) {
0 ignored issues
show
Deprecated Code introduced by
The function Drupal\Core\Entity\Entit...terface::isSubclassOf() has been deprecated: in drupal:8.3.0 and is removed from drupal:9.0.0. Use Drupal\Core\Entity\EntityTypeInterface::entityClassImplements() instead. ( Ignorable by Annotation )

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

31
      if ($entity_type && /** @scrutinizer ignore-deprecated */ $entity_type->isSubclassOf(FieldableEntityInterface::class)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
32
        $fields = $field_manager->getFieldDefinitions($entity_type_id, $bundle);
33
        foreach ($fields as $field) {
34
          $field_config = $field->getConfig($bundle);
35
          if ($field_config->isTranslatable() && strpos($field->getName(), 'content_translation_') !== 0) {
36
            $field_config->setTranslatable(TRUE)->save();
37
          }
38
        }
39
      }
40
    }
41
  }
42
  // Ensure entity and menu router information are correctly rebuilt.
43
  $type_manager->clearCachedDefinitions();
44
  \Drupal::service('router.builder')->setRebuildNeeded();
45
}
46
47
/**
48
 * Updates the current site's translations via a batch process.
49
 */
50
function df_tools_translation_update_config_translation() {
51
  // The Locale module splits its translation functions into separate include
52
  // files, based on utility.
53
  // To ensure that each function we require is available, load its respective
54
  // include file.
55
  \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
56
  \Drupal::moduleHandler()->loadInclude('locale', 'compare.inc');
57
  \Drupal::moduleHandler()->loadInclude('locale', 'fetch.inc');
58
  \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc');
59
60
  // Get a list of all currently installed languages as langcodes.
61
  $languageManager = \Drupal::languageManager();
62
  $langcodes = array_keys($languageManager->getLanguages());
63
64
  // Set a batch to download and import translations.
65
  locale_translation_flush_projects();
66
  locale_translation_check_projects();
67
  $options = _locale_translation_default_update_options();
68
  $batch = locale_translation_batch_fetch_build([], $langcodes, $options);
69
  batch_set($batch);
70
  // Set a batch to update configuration as well.
71
  if ($batch = locale_config_batch_update_components($options, $langcodes)) {
72
    $batch['file'] = drupal_get_path('module', 'df_tools_translation') . '/df_tools_translation.batch.inc';
73
    batch_set($batch);
74
  }
75
}
76
77
/**
78
 * Imports all relevant translations from a modules /translations directory.
79
 *
80
 * @param string $type The project type.
81
 * @param string $name The name of the project.
82
 *
83
 * @return bool FALSE if the project does not exist.
84
 */
85
function df_tools_translation_import_translations($type, $name) {
86
  // Attempt to pull module path.
87
  $path = drupal_get_path($type, $name);
88
  if (!$path) {
89
    return FALSE;
90
  }
91
92
  // Get a list of all currently installed languages as langcodes.
93
  $languageManager = \Drupal::languageManager();
94
  $langcodes = array_keys($languageManager->getLanguages());
95
96
  // Import each file.
97
  foreach ($langcodes as $langcode) {
98
    $filepath = DRUPAL_ROOT . '/' . $path . '/translations/' . $langcode . '.po';
99
    if (file_exists($filepath)) {
100
      \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
101
      \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc');
102
      $options = array_merge(_locale_translation_default_update_options(), [
103
        'langcode' => $langcode,
104
        'overwrite_options' => [
105
          'customized' => TRUE,
106
          'not_customized' => TRUE
107
        ],
108
        'customized' => TRUE
109
      ]);
110
111
      $original_file = (object) [
112
        'filename' => $langcode . '.po',
113
        'uri' => $filepath
114
      ];
115
      $file = locale_translate_file_attach_properties($original_file, $options);
116
      $batch = locale_translate_batch_build([$file->uri => $file], $options);
117
      batch_set($batch);
118
    }
119
  }
120
}
121
122
/**
123
 * Implements hook_preprocess_page().
124
 */
125
function df_tools_translation_preprocess_page(&$variables) {
126
  // Add a new page variable with the current link.
127
  if (!isset($variables['language_current_link']) && isset($variables['language'])) {
128
    $variables['language_current_link'] = [
129
      '#markup' => t($variables['language']->getName())
130
    ];
131
  }
132
133
  // Add the rest of the language links  as well, with links to switch to the
134
  // correct language.
135
  if (!isset($variables['language_links'])) {
136
    // Get a list of the current languages.
137
    $languageManager = \Drupal::languageManager();
138
    $languages = $languageManager->getLanguages();
139
140
    // Remove the current language.
141
    unset($languages[$variables['language']->getId()]);
142
143
    // Add each link to the language list.
144
    $links = [];
145
    foreach ($languages as $language) {
146
      $langcode = $language->getId();
147
148
      // Get the path to the current node, translated.
149
      $current_path = \Drupal::service('path.current')->getPath();
150
      $alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path, $langcode);
151
      // We don't need to alias English links.
152
      if ($langcode == 'en') {
153
        $langcode = '';
154
      }
155
      $url = \Drupal\Core\Url::fromUri('base:/' . $langcode . $alias);
156
157
      $current_name = [
158
        '#markup' => t($language->getName())
159
      ];
160
161
      $links[] = \Drupal::l($current_name, $url);
0 ignored issues
show
Deprecated Code introduced by
The function Drupal::l() has been deprecated: in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Link::fromTextAndUrl() instead. ( Ignorable by Annotation )

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

161
      $links[] = /** @scrutinizer ignore-deprecated */ \Drupal::l($current_name, $url);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
$current_name of type array<string,Drupal\Core...ion\TranslatableMarkup> is incompatible with the type string expected by parameter $text of Drupal::l(). ( Ignorable by Annotation )

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

161
      $links[] = \Drupal::l(/** @scrutinizer ignore-type */ $current_name, $url);
Loading history...
162
    }
163
164
    $variables['language_links'] = $links;
165
  }
166
}
167
168