Skip to content

Commit f48ca30

Browse files
jrglasgowJames R Glasgow
and
James R Glasgow
authored
Fixes #2 - simplified the module by removing a new theme hook/preprocess function/template and instead just added the wrapper div "<div class='c2pa-wrapper'></div>" to the render array (#8)
Co-authored-by: James R Glasgow <james.glasgow@associates.hq.dhs.gov>
1 parent 0fab7bb commit f48ca30

File tree

3 files changed

+15
-63
lines changed

3 files changed

+15
-63
lines changed

c2pa.module

-42
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,3 @@
44
* @file
55
* Primary module hooks for c2pa module.
66
*/
7-
8-
function c2pa_page_attachments(array &$page) {
9-
$page['#attached']['library'][] = 'c2pa/c2pa';
10-
}
11-
12-
function c2pa_theme($existing, $type, $theme, $path) {
13-
return [
14-
'c2pa_image_formatter' => [
15-
'variables' => [
16-
'item' => NULL,
17-
'item_attributes' => NULL,
18-
'url' => NULL,
19-
'image_style' => NULL
20-
]
21-
]
22-
];
23-
}
24-
25-
function template_preprocess_c2pa_image_formatter(&$variables) {
26-
$variables['image'] = [
27-
'#theme' => 'image',
28-
];
29-
$variables['image']['#attributes'] = $variables['item_attributes'];
30-
31-
$item = $variables['item'];
32-
33-
// Do not output an empty 'title' attribute.
34-
if (!is_null($item->title) && mb_strlen($item->title) != 0) {
35-
$variables['image']['#title'] = $item->title;
36-
}
37-
38-
if (($entity = $item->entity) && empty($item->uri)) {
39-
$variables['image']['#uri'] = $entity->getFileUri();
40-
}
41-
else {
42-
$variables['image']['#uri'] = $item->uri;
43-
}
44-
45-
foreach (['width', 'height', 'alt'] as $key) {
46-
$variables['image']["#$key"] = $item->$key;
47-
}
48-
}

src/Plugin/Field/FieldFormatter/C2paImageFormatter.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class C2paImageFormatter extends ImageFormatter {
2222
* {@inheritdoc}
2323
*/
2424
public function settingsSummary() {
25-
$summary = [];
25+
$summary = parent::settingsSummary();
2626
$summary[] = $this->t('Formats the image for use with the c2pa module');
2727
return $summary;
2828
}
@@ -34,7 +34,19 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
3434
$elements = parent::viewElements($items, $langcode);
3535

3636
foreach ($elements as &$element) {
37-
$element['#theme'] = 'c2pa_image_formatter';
37+
// wrap the element in a container with the appropriate class
38+
$element = [
39+
'#type' => 'container',
40+
'#attributes' => [
41+
'class' => 'c2pa-wrapper',
42+
],
43+
'image' => $element,
44+
'#attached' => [
45+
// the library doesn't need to be on every page load, just those with
46+
// images and a wrapping container with the c2pa-wrapper class
47+
'library' => 'c2pa/c2pa'
48+
],
49+
];
3850
}
3951

4052
// foreach ($items as $delta => $item) {
@@ -47,4 +59,4 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
4759
return $elements;
4860
}
4961

50-
}
62+
}

templates/c2pa-image-formatter.html.twig

-18
This file was deleted.

0 commit comments

Comments
 (0)