This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraft_theme.theme
79 lines (69 loc) · 2.25 KB
/
draft_theme.theme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* @file
* Functions to support theming in the Draft theme.
*/
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
require_once 'includes/preprocess/form.inc';
require_once 'includes/suggestions.inc';
/**
* Utility function to get the url of the media image.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* Entity containing the image field.
* @param string $field_media
* Name of the media field to use.
* @param string $field_image_name
* Name of the media's image field to use.
* @param string $image_style
* Name of the image style to use.
*
* @return string
* A string containing an absolute URL.
*/
function draft_get_entity_image_url(ContentEntityInterface $entity, $field_media, $field_image_name, $image_style) {
$output = '';
// Ensure that field $field_name exists and not empty.
if (!$entity->hasField($field_media) || $entity->get($field_media)->isEmpty()) {
return $output;
}
// Ensure that media entity exists.
/** @var \Drupal\media_entity\MediaInterface|\Drupal\media\MediaInterface $media_entity */
if (!($media_entity = $entity->get($field_media)->entity)) {
return $output;
}
// Ensure that $field_image_name exists and not empty.
if (!$media_entity->hasField($field_image_name) || $media_entity->get($field_image_name)->isEmpty()) {
return $output;
}
// Ensure that file entity exists.
/** @var \Drupal\file\Entity\FileInterface $file_entity */
if (!($file_entity = $media_entity->get($field_image_name)->entity)) {
return $output;
}
return draft_get_image_style_url($file_entity, $image_style);
}
/**
* Get the url for a specific image style of an image file.
*
* @param \Drupal\file\FileInterface $file
* Image to be used.
* @param string $image_style_name
* Name of the image style to use.
*
* @return string
* A string containing an absolute URL.
*/
function draft_get_image_style_url(FileInterface $file, $image_style_name) {
$uri = $file->getFileUri();
/** @var \Drupal\image\ImageStyleInterface $image_style */
if ($image_style = ImageStyle::load($image_style_name)) {
$url = $image_style->buildUrl($uri);
}
else {
$url = file_create_url($uri);
}
return $url;
}