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 ( 1c09b3...ddb096 )
by Kevin
08:50
created

df_tools_translation_preprocess_page()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 2
b 0
f 0
nc 8
nop 1
dl 0
loc 40
rs 8.9297
1
<?php
2
3
/**
4
 * @file
5
 * Contains df_tools_translation.module.
6
 */
7
8
use Drupal\Core\Url;
9
use Drupal\Core\Link;
10
use Drupal\Core\Entity\FieldableEntityInterface;
11
12
/**
13
 * Enables translation for the given entity bundles and all their fields.
14
 *
15
 * @param array $entity_info An array mapping entity types to arrays of bundles.
16
 */
17
function df_tools_translation_enable_translation($entity_info) {
18
  // Enable translation for all of our entities/bundles.
19
  $type_manager = \Drupal::entityTypeManager();
20
  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
21
  $field_manager = \Drupal::service('entity_field.manager');
22
  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $translation_manager */
23
  $translation_manager = \Drupal::service('content_translation.manager');
24
  $supported_types = $translation_manager->getSupportedEntityTypes();
25
  foreach ($entity_info as $entity_type_id => $bundles) {
26
    foreach ($bundles as $bundle) {
27
      // Store whether a bundle has translation enabled or not.
28
      if (isset($supported_types[$entity_type_id])) {
29
        $translation_manager->setEnabled($entity_type_id, $bundle, TRUE);
30
      }
31
      // Make every field translatable as well.
32
      $entity_type = $type_manager->getDefinition($entity_type_id);
33
      if ($entity_type && $entity_type->entityClassImplements(FieldableEntityInterface::class)) {
34
        $fields = $field_manager->getFieldDefinitions($entity_type_id, $bundle);
35
        foreach ($fields as $field) {
36
          $field_config = $field->getConfig($bundle);
37
          if ($field_config->isTranslatable() && strpos($field->getName(), 'content_translation_') !== 0) {
38
            $field_config->setTranslatable(TRUE)->save();
39
          }
40
        }
41
      }
42
    }
43
  }
44
  // Ensure entity and menu router information are correctly rebuilt.
45
  $type_manager->clearCachedDefinitions();
46
  \Drupal::service('router.builder')->setRebuildNeeded();
47
}
48
49
/**
50
 * Updates the current site's translations via a batch process.
51
 */
52
function df_tools_translation_update_config_translation() {
53
  // The Locale module splits its translation functions into separate include
54
  // files, based on utility.
55
  // To ensure that each function we require is available, load its respective
56
  // include file.
57
  \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
58
  \Drupal::moduleHandler()->loadInclude('locale', 'compare.inc');
59
  \Drupal::moduleHandler()->loadInclude('locale', 'fetch.inc');
60
  \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc');
61
62
  // Get a list of all currently installed languages as langcodes.
63
  $languageManager = \Drupal::languageManager();
64
  $langcodes = array_keys($languageManager->getLanguages());
65
66
  // Set a batch to download and import translations.
67
  locale_translation_flush_projects();
68
  locale_translation_check_projects();
69
  $options = _locale_translation_default_update_options();
70
  $batch = locale_translation_batch_fetch_build([], $langcodes, $options);
71
  batch_set($batch);
72
  // Set a batch to update configuration as well.
73
  if ($batch = locale_config_batch_update_components($options, $langcodes)) {
74
    $batch['file'] = drupal_get_path('module', 'df_tools_translation') . '/df_tools_translation.batch.inc';
75
    batch_set($batch);
76
  }
77
}
78
79
/**
80
 * Imports all relevant translations from a modules /translations directory.
81
 *
82
 * @param string $type The project type.
83
 * @param string $name The name of the project.
84
 *
85
 * @return bool FALSE if the project does not exist.
86
 */
87
function df_tools_translation_import_translations($type, $name) {
88
  // Attempt to pull module path.
89
  $path = drupal_get_path($type, $name);
90
  if (!$path) {
91
    return FALSE;
92
  }
93
94
  // Get a list of all currently installed languages as langcodes.
95
  $languageManager = \Drupal::languageManager();
96
  $langcodes = array_keys($languageManager->getLanguages());
97
98
  // Import each file.
99
  foreach ($langcodes as $langcode) {
100
    $filepath = DRUPAL_ROOT . '/' . $path . '/translations/' . $langcode . '.po';
101
    if (file_exists($filepath)) {
102
      \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
103
      \Drupal::moduleHandler()->loadInclude('locale', 'translation.inc');
104
      $options = array_merge(_locale_translation_default_update_options(), [
105
        'langcode' => $langcode,
106
        'overwrite_options' => [
107
          'customized' => TRUE,
108
          'not_customized' => TRUE
109
        ],
110
        'customized' => TRUE
111
      ]);
112
113
      $original_file = (object) [
114
        'filename' => $langcode . '.po',
115
        'uri' => $filepath
116
      ];
117
      $file = locale_translate_file_attach_properties($original_file, $options);
118
      $batch = locale_translate_batch_build([$file->uri => $file], $options);
119
      batch_set($batch);
120
    }
121
  }
122
}
123
124
/**
125
 * Implements hook_preprocess_page().
126
 */
127
function df_tools_translation_preprocess_page(&$variables) {
128
  // Add a new page variable with the current link.
129
  if (!isset($variables['language_current_link']) && isset($variables['language'])) {
130
    $path = \Drupal::service('path.current')->getPath();
131
    $language = $variables['language']->getId();
132
    $alias = \Drupal::service('path_alias.manager')->getAliasByPath($path, $language);
133
    $url = Url::fromUri('internal:/' . $alias);
134
    $name = t($variables['language']->getName());
135
    $link = Link::fromTextAndUrl($name, $url)->toString();
136
    $variables['language_current_link'] = $link;
137
  }
138
139
  // Add the rest of the language links  as well, with links to switch to the
140
  // correct language.
141
  if (!isset($variables['language_links'])) {
142
    // Get a list of the current languages.
143
    $languageManager = \Drupal::languageManager();
144
    $languages = $languageManager->getLanguages();
145
146
    // Remove the current language.
147
    unset($languages[$variables['language']->getId()]);
148
149
    // Add each link to the language list.
150
    $links = [];
151
    foreach ($languages as $language) {
152
      $langcode = $language->getId();
153
154
      // Get the path to the current node, translated.
155
      $current_path = \Drupal::service('path.current')->getPath();
156
      $alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path, $langcode);
157
      // We don't need to alias English links.
158
      if ($langcode == 'en') {
159
        $langcode = '';
160
      }
161
      $url = Url::fromUri('base:/' . $langcode . $alias);
162
      $current_name = t($language->getName());
163
      $links[] = Link::fromTextAndUrl($current_name, $url)->toString();
164
    }
165
166
    $variables['language_links'] = $links;
167
  }
168
}
169