|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file |
|
5
|
|
|
* The Crop API Drupal module. |
|
6
|
|
|
* |
|
7
|
|
|
* Provides storage and API for image crops. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use \Drupal\Core\Form\FormStateInterface; |
|
11
|
|
|
use \Drupal\media_entity\MediaBundleInterface; |
|
12
|
|
|
use \Drupal\file\FileInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Implements hook_theme(). |
|
16
|
|
|
*/ |
|
17
|
|
|
function crop_theme() { |
|
18
|
|
|
return [ |
|
19
|
|
|
'crop_crop_summary' => [ |
|
20
|
|
|
'variables' => ['data' => [], 'effect' => []], |
|
21
|
|
|
], |
|
22
|
|
|
]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Prepares variables for crop_crop summary template. |
|
27
|
|
|
* |
|
28
|
|
|
* Default template: crop-crop-summary.twig.html. |
|
29
|
|
|
*/ |
|
30
|
|
|
function template_preprocess_crop_crop_summary(&$variables) { |
|
31
|
|
|
if (!empty($variables['data']['crop_type'])) { |
|
32
|
|
|
$type = \Drupal::entityManager()->getStorage('crop_type')->load($variables['data']['crop_type']); |
|
33
|
|
|
$variables['data']['crop_type'] = $type->label(); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Implements hook_form_BASE_ID_alter(). |
|
39
|
|
|
* |
|
40
|
|
|
* Adds crop configuraiton fields to media bundle form. |
|
41
|
|
|
*/ |
|
42
|
|
|
function crop_form_media_bundle_form_alter(&$form, FormStateInterface $form_state, $form_id) { |
|
|
|
|
|
|
43
|
|
|
/** @var \Drupal\media_entity\MediaBundleInterface $bundle */ |
|
44
|
|
|
$bundle = $form['#entity']; |
|
45
|
|
|
$options = []; |
|
46
|
|
|
$allowed_field_types = ['file', 'image']; |
|
47
|
|
|
foreach (\Drupal::entityManager()->getFieldDefinitions('media', $bundle->id()) as $field_name => $field) { |
|
48
|
|
|
if (in_array($field->getType(), $allowed_field_types) && !$field->getFieldStorageDefinition()->isBaseField()) { |
|
49
|
|
|
$options[$field_name] = $field->getLabel(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$form['#entity_builders'][] = 'crop_media_bundle_form_builder'; |
|
54
|
|
|
$form['crop'] = [ |
|
55
|
|
|
'#type' => 'fieldset', |
|
56
|
|
|
'#title' => t('Crop configuration'), |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
if (empty($options)) { |
|
60
|
|
|
$form['crop']['image_field'] = [ |
|
61
|
|
|
'#type' => 'value', |
|
62
|
|
|
'#value' => NULL, |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$form['crop']['message'] = [ |
|
66
|
|
|
'#markup' => t('There are no file or image fields on this bundle at the moment. In order to configure crop add at least one such field and come back.') |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$form['crop']['image_field'] = [ |
|
73
|
|
|
'#type' => 'select', |
|
74
|
|
|
'#title' => t('Image field'), |
|
75
|
|
|
'#default_value' => $bundle->getThirdPartySetting('crop', 'image_field'), |
|
76
|
|
|
'#options' => $options, |
|
77
|
|
|
'#description' => t('Select field that stores image which needs to be cropped.'), |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Entity builder for Media bundle. |
|
84
|
|
|
* |
|
85
|
|
|
* Adds third party settings to Media bundle config entity. |
|
86
|
|
|
* |
|
87
|
|
|
* @see crop_form_media_bundle_form_alter() |
|
88
|
|
|
*/ |
|
89
|
|
|
function crop_media_bundle_form_builder($entity_type, MediaBundleInterface $bundle, &$form, FormStateInterface $form_state) { |
|
|
|
|
|
|
90
|
|
|
$bundle->setThirdPartySetting('crop', 'image_field', $form_state->getValue('image_field')); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Implements hook_ENTITY_TYPE_delete(). |
|
95
|
|
|
* |
|
96
|
|
|
* Deletes orphaned crops when a file is deleted. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \Drupal\file\FileInterface $file |
|
99
|
|
|
* The file being deleted. |
|
100
|
|
|
*/ |
|
101
|
|
|
function crop_file_delete(FileInterface $file) { |
|
102
|
|
|
// Get all crops for the file being deleted. |
|
103
|
|
|
$crops = \Drupal::entityTypeManager() |
|
104
|
|
|
->getStorage('crop') |
|
105
|
|
|
->loadByProperties(['uri' => $file->getFileUri()]); |
|
106
|
|
|
|
|
107
|
|
|
foreach ($crops as $crop) { |
|
108
|
|
|
$crop->delete(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.