|
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\Core\File\FileSystemInterface; |
|
12
|
|
|
use Drupal\media\MediaInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Implements hook_form_FORM_ID_alter(). |
|
16
|
|
|
*/ |
|
17
|
|
|
function df_tools_media_form_entity_browser_media_browser_in_modal_form_alter(array &$form, FormStateInterface $form_state) { |
|
|
|
|
|
|
18
|
|
|
$form['#attached']['library'][] = 'lightning_media/browser.styling'; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Implements hook_ENTITY_TYPE_insert(). |
|
23
|
|
|
*/ |
|
24
|
|
|
function df_tools_media_views_insert(ViewEntityInterface $view) { |
|
25
|
|
|
if (\Drupal::isConfigSyncing()) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
elseif (drupal_installation_attempted() && $view->id() == 'media') { |
|
29
|
|
|
$display1 = &$view->getDisplay('entity_browser_1'); |
|
30
|
|
|
$display2 = &$view->getDisplay('entity_browser_2'); |
|
31
|
|
|
$display1['display_options']['filters']['field_tags_target_id'] = $display2['display_options']['filters']['field_tags_target_id'] = [ |
|
32
|
|
|
'id' => 'field_tags_target_id', |
|
33
|
|
|
'table' => 'media__field_tags', |
|
34
|
|
|
'field' => 'field_tags_target_id', |
|
35
|
|
|
'relationship' => 'none', |
|
36
|
|
|
'group_type' => 'group', |
|
37
|
|
|
'admin_label' => '', |
|
38
|
|
|
'operator' => 'or', |
|
39
|
|
|
'value' => [], |
|
40
|
|
|
'group' => 1, |
|
41
|
|
|
'exposed' => true, |
|
42
|
|
|
'expose' => [ |
|
43
|
|
|
'operator_id' => 'field_tags_target_id_op', |
|
44
|
|
|
'label' => 'Tags', |
|
45
|
|
|
'description' => '', |
|
46
|
|
|
'use_operator' => false, |
|
47
|
|
|
'operator' => 'field_tags_target_id_op', |
|
48
|
|
|
'identifier' => 'field_tags_target_id', |
|
49
|
|
|
'required' => false, |
|
50
|
|
|
'remember' => false, |
|
51
|
|
|
'multiple' => false, |
|
52
|
|
|
'remember_roles' => [ |
|
53
|
|
|
'authenticated' => 'authenticated', |
|
54
|
|
|
'anonymous' => '0', |
|
55
|
|
|
'administrator' => '0', |
|
56
|
|
|
'layout_manager' => '0', |
|
57
|
|
|
'creator' => '0', |
|
58
|
|
|
'reviewer' => '0', |
|
59
|
|
|
'media_creator' => '0', |
|
60
|
|
|
'media_manager' => '0', |
|
61
|
|
|
], |
|
62
|
|
|
'reduce' => false, |
|
63
|
|
|
], |
|
64
|
|
|
'is_grouped' => false, |
|
65
|
|
|
'group_info' => [ |
|
66
|
|
|
'label' => '', |
|
67
|
|
|
'description' => '', |
|
68
|
|
|
'identifier' => '', |
|
69
|
|
|
'optional' => true, |
|
70
|
|
|
'widget' => 'select', |
|
71
|
|
|
'multiple' => false, |
|
72
|
|
|
'remember' => false, |
|
73
|
|
|
'default_group' => 'All', |
|
74
|
|
|
'default_group_multiple' => [], |
|
75
|
|
|
'group_items' => [], |
|
76
|
|
|
], |
|
77
|
|
|
'reduce_duplicates' => false, |
|
78
|
|
|
'type' => 'textfield', |
|
79
|
|
|
'limit' => true, |
|
80
|
|
|
'vid' => 'media_tags', |
|
81
|
|
|
'hierarchy' => false, |
|
82
|
|
|
'error_message' => true, |
|
83
|
|
|
'plugin_id' => 'taxonomy_index_tid', |
|
84
|
|
|
]; |
|
85
|
|
|
$view->save(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Implements hook_theme(). |
|
91
|
|
|
*/ |
|
92
|
|
|
function df_tools_media_theme() { |
|
93
|
|
|
return [ |
|
94
|
|
|
'media_video_file_formatter__df_tools_media' => [ |
|
95
|
|
|
'base hook' => 'media_video_file_formatter', |
|
96
|
|
|
], |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Implements hook_theme_suggestions_HOOK_alter() for media_video_file_formatter. |
|
102
|
|
|
*/ |
|
103
|
|
|
function df_tools_media_theme_suggestions_media_video_file_formatter_alter(array &$suggestions, array $variables, $hook) { |
|
|
|
|
|
|
104
|
|
|
$suggestions[] = 'media_video_file_formatter__df_tools_media_video'; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Implements template_preprocess_HOOK() for media_video_file_formatter. |
|
109
|
|
|
*/ |
|
110
|
|
|
function df_tools_media_preprocess_media_video_file_formatter__df_tools_media_video(&$variables) { |
|
111
|
|
|
// By default, 'extravalue' contains a string. If the HMTL5 video formatter |
|
112
|
|
|
// has some attributes with values, special chars (like double comma) will be |
|
113
|
|
|
// escaped. |
|
114
|
|
|
// To fix this, we replace 'extravalue' with a properly set Twig Attribute |
|
115
|
|
|
// object and properly update the 'attribute' array as well. |
|
116
|
|
|
$attribute = new Attribute(); |
|
117
|
|
|
if (!empty($variables['extravalue'])) { |
|
118
|
|
|
$attributes_raw = explode(' ', trim($variables['extravalue'])); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($attributes_raw as $attribute_raw) { |
|
121
|
|
|
if (($delimiter_pos = strpos($attribute_raw, '=')) !== FALSE) { |
|
122
|
|
|
$key = substr($attribute_raw, 0, $delimiter_pos); |
|
123
|
|
|
$value = trim(substr($attribute_raw, ($delimiter_pos + 1)), " \"\t\n\r\0\x0B"); |
|
124
|
|
|
|
|
125
|
|
|
if (!in_array($value, ['true', 'TRUE'])) { |
|
126
|
|
|
$variables['attributes'][$key] = $value; |
|
127
|
|
|
$attribute->setAttribute($key, $value); |
|
128
|
|
|
continue; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$variables['attributes'][$attribute_raw] = TRUE; |
|
133
|
|
|
$attribute->setAttribute($attribute_raw, TRUE); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$variables['extravalue'] = $attribute; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* |
|
142
|
|
|
* |
|
143
|
|
|
* @param \Drupal\media\MediaInterface $entity |
|
144
|
|
|
*/ |
|
145
|
|
|
function df_tools_media_media_presave(MediaInterface $entity) { |
|
146
|
|
|
$type = $entity->bundle(); |
|
147
|
|
|
$types = ['image']; |
|
148
|
|
|
$unsplash_field = 'field_unsplash_image_id'; |
|
149
|
|
|
$image_field = 'image'; |
|
150
|
|
|
if (in_array($type, $types) |
|
151
|
|
|
&& $entity->hasField($unsplash_field) |
|
152
|
|
|
&& !$entity->get($unsplash_field)->isEmpty() |
|
153
|
|
|
&& $entity->hasField($image_field) |
|
154
|
|
|
) { |
|
155
|
|
|
$id = $entity->get($unsplash_field)->value; |
|
156
|
|
|
$raw_image = file_get_contents('unsplash://' . $id); |
|
157
|
|
|
$filename = $id . '.jpg'; |
|
158
|
|
|
$images_dir = "public://images"; |
|
159
|
|
|
if (!is_dir($images_dir)) { |
|
160
|
|
|
mkdir($images_dir); |
|
161
|
|
|
} |
|
162
|
|
|
$file = file_save_data($raw_image, "$images_dir/$filename", FileSystemInterface::EXISTS_REPLACE); |
|
163
|
|
|
$x_y = \Drupal::config('focal_point.settings')->get('default_value'); |
|
164
|
|
|
$entity->$image_field->focal_point = $x_y; |
|
165
|
|
|
$entity->$image_field->alt = $entity->label(); |
|
166
|
|
|
$entity->$image_field->target_id = $file->id(); |
|
167
|
|
|
} |
|
168
|
|
|
elseif (in_array($type, $types) |
|
169
|
|
|
&& $entity->hasField($image_field) |
|
170
|
|
|
&& !$entity->get($image_field)->isEmpty() |
|
171
|
|
|
&& $entity->hasField($unsplash_field) |
|
172
|
|
|
&& $entity->get($unsplash_field)->isEmpty() |
|
173
|
|
|
&& empty($entity->$image_field->alt) |
|
174
|
|
|
) { |
|
175
|
|
|
$entity->$image_field->alt = $entity->label(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Implements hook_form_FORM_ID_alter(). |
|
181
|
|
|
*/ |
|
182
|
|
|
function df_tools_media_form_media_image_add_form_alter(array &$form, FormStateInterface $form_state) { |
|
|
|
|
|
|
183
|
|
|
$form['#attached']['library'][] = 'df_tools_media/main'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Implements hook_form_FORM_ID_alter(). |
|
188
|
|
|
*/ |
|
189
|
|
|
function df_tools_media_form_media_image_edit_form_alter(array &$form, FormStateInterface $form_state) { |
|
|
|
|
|
|
190
|
|
|
$form['#attached']['library'][] = 'df_tools_media/main'; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.