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 ( 6839b1...4d2d39 )
by Brant
05:16
created

df_tools_media_media_presave()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.5222
1
<?php
2
3
/**
4
 * @file
5
 * Contains df_tools_media.module.
6
 */
7
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\views\ViewEntityInterface;
10
use Drupal\Core\Template\Attribute;
11
use Drupal\media\MediaInterface;
12
13
/**
14
 * Implements hook_form_FORM_ID_alter().
15
 */
16
function df_tools_media_form_entity_browser_media_browser_in_modal_form_alter(array &$form, FormStateInterface $form_state) {
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

16
function df_tools_media_form_entity_browser_media_browser_in_modal_form_alter(array &$form, /** @scrutinizer ignore-unused */ 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...
17
  $form['#attached']['library'][] = 'lightning_media/browser.styling';
18
}
19
20
/**
21
 * Implements hook_ENTITY_TYPE_insert().
22
 */
23
function df_tools_media_views_insert(ViewEntityInterface $view) {
24
  if (\Drupal::isConfigSyncing()) {
25
    return;
26
  }
27
  elseif (drupal_installation_attempted() && $view->id() == 'media') {
28
    $display1 = &$view->getDisplay('entity_browser_1');
29
    $display2 = &$view->getDisplay('entity_browser_2');
30
    $display1['display_options']['filters']['field_tags_target_id'] = $display2['display_options']['filters']['field_tags_target_id'] = [
31
      'id' => 'field_tags_target_id',
32
      'table' => 'media__field_tags',
33
      'field' => 'field_tags_target_id',
34
      'relationship' => 'none',
35
      'group_type' => 'group',
36
      'admin_label' => '',
37
      'operator' => 'or',
38
      'value' => [],
39
      'group' => 1,
40
      'exposed' => true,
41
      'expose' => [
42
        'operator_id' => 'field_tags_target_id_op',
43
        'label' => 'Tags',
44
        'description' => '',
45
        'use_operator' => false,
46
        'operator' => 'field_tags_target_id_op',
47
        'identifier' => 'field_tags_target_id',
48
        'required' => false,
49
        'remember' => false,
50
        'multiple' => false,
51
        'remember_roles' => [
52
          'authenticated' => 'authenticated',
53
          'anonymous' => '0',
54
          'administrator' => '0',
55
          'layout_manager' => '0',
56
          'creator' => '0',
57
          'reviewer' => '0',
58
          'media_creator' => '0',
59
          'media_manager' => '0',
60
        ],
61
        'reduce' => false,
62
      ],
63
      'is_grouped' => false,
64
      'group_info' => [
65
        'label' => '',
66
        'description' => '',
67
        'identifier' => '',
68
        'optional' => true,
69
        'widget' => 'select',
70
        'multiple' => false,
71
        'remember' => false,
72
        'default_group' => 'All',
73
        'default_group_multiple' => [],
74
        'group_items' => [],
75
      ],
76
      'reduce_duplicates' => false,
77
      'type' => 'textfield',
78
      'limit' => true,
79
      'vid' => 'media_tags',
80
      'hierarchy' => false,
81
      'error_message' => true,
82
      'plugin_id' => 'taxonomy_index_tid',
83
    ];
84
    $view->save();
85
  }
86
}
87
88
/**
89
 * Implements hook_theme().
90
 */
91
function df_tools_media_theme() {
92
  return [
93
    'media_video_file_formatter__df_tools_media' => [
94
      'base hook' => 'media_video_file_formatter',
95
    ],
96
  ];
97
}
98
99
/**
100
 * Implements hook_theme_suggestions_HOOK_alter() for media_video_file_formatter.
101
 */
102
function df_tools_media_theme_suggestions_media_video_file_formatter_alter(array &$suggestions, array $variables, $hook) {
0 ignored issues
show
Unused Code introduced by
The parameter $variables 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_media_theme_suggestions_media_video_file_formatter_alter(array &$suggestions, /** @scrutinizer ignore-unused */ array $variables, $hook) {

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 $hook 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_media_theme_suggestions_media_video_file_formatter_alter(array &$suggestions, array $variables, /** @scrutinizer ignore-unused */ $hook) {

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
  $suggestions[] = 'media_video_file_formatter__df_tools_media_video';
104
}
105
106
/**
107
 * Implements template_preprocess_HOOK() for media_video_file_formatter.
108
 */
109
function df_tools_media_preprocess_media_video_file_formatter__df_tools_media_video(&$variables) {
110
  // By default, 'extravalue' contains a string. If the HMTL5 video formatter
111
  // has some attributes with values, special chars (like double comma) will be
112
  // escaped.
113
  // To fix this, we replace 'extravalue' with a properly set Twig Attribute
114
  // object and properly update the 'attribute' array as well.
115
  $attribute = new Attribute();
116
  if (!empty($variables['extravalue'])) {
117
    $attributes_raw = explode(' ', trim($variables['extravalue']));
118
119
    foreach ($attributes_raw as $attribute_raw) {
120
      if (($delimiter_pos = strpos($attribute_raw, '=')) !== FALSE) {
121
        $key = substr($attribute_raw, 0, $delimiter_pos);
122
        $value = trim(substr($attribute_raw, ($delimiter_pos + 1)), " \"\t\n\r\0\x0B");
123
124
        if (!in_array($value, ['true', 'TRUE'])) {
125
          $variables['attributes'][$key] = $value;
126
          $attribute->setAttribute($key, $value);
127
          continue;
128
        }
129
      }
130
131
      $variables['attributes'][$attribute_raw] = TRUE;
132
      $attribute->setAttribute($attribute_raw, TRUE);
0 ignored issues
show
Bug introduced by
TRUE of type true is incompatible with the type array|string expected by parameter $value of Drupal\Core\Template\Attribute::setAttribute(). ( Ignorable by Annotation )

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

132
      $attribute->setAttribute($attribute_raw, /** @scrutinizer ignore-type */ TRUE);
Loading history...
133
    }
134
  }
135
136
  $variables['extravalue'] = $attribute;
137
}
138
139
/**
140
 * Implements hook_ENTITY_TYPE_presave().
141
 *
142
 * @param $entity
143
 */
144
function df_tools_media_media_presave(MediaInterface $entity) {
145
  $type = $entity->bundle();
146
  $types = ['image'];
147
  $unsplash_field = 'field_unsplash_image_id';
148
  $image_field = 'image';
149
  if (in_array($type, $types)
150
      && $entity->hasField($unsplash_field)
151
      && !$entity->get($unsplash_field)->isEmpty()
152
      && $entity->hasField($image_field)
153
  ) {
154
    $id = $entity->get($unsplash_field)->value;
155
    $raw_image = file_get_contents('unsplash://' . $id);
156
    $filename = $id . '.jpg';
157
    $file = file_save_data($raw_image, "public://images/$filename", FILE_EXISTS_REPLACE);
0 ignored issues
show
introduced by
The constant FILE_EXISTS_REPLACE has been deprecated: in Drupal 8.7.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\File\FileSystemInterface::EXISTS_REPLACE. ( Ignorable by Annotation )

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

157
    $file = file_save_data($raw_image, "public://images/$filename", /** @scrutinizer ignore-deprecated */ FILE_EXISTS_REPLACE);
Loading history...
158
    $entity->$image_field->setValue(['target_id' => $file->id()]);
159
  }
160
}
161