-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwinbitFrontendFile.php
119 lines (103 loc) · 3.3 KB
/
TwinbitFrontendFile.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @file
* Provides an simple way to render a set of files from an entity.
*
*
* Example: Render a set of file uploads from an Node as images using a style and some attributes
*
* $image_renderers = new TwinbitFrontendFile('node', $file_ref, 'field_file_upload');
* foreach ($image_renderers->renderers as $image_render) {
* $image_render->setStyle('product_photo');
* $image_render->setAttributes(array('alt' => $alt, 'title' => $alt, 'attributes' => array('class' => 'test')));
* $html .= $image_render->render();
* // you can also pass custom theme functions, for example calling "theme_custom_image_formatter"
* $html .= $image_render->render('image', array('#theme' => 'custom_image_formatter'));
* }
*
* Example: Render a set of file uploads from an Node as links using a style and some attributes
*
* $file_renderers = new TwinbitFrontendFile('node', $file_ref, 'field_file_upload');
* foreach ($file_renderers->renderers as $file_render) {
* $file_render->setStyle('product_photo');
* $file_render->setAttributes(array('alt' => $alt, 'title' => $alt, 'attributes' => array('class' => 'test')));
* // please note that this function will use "theme_file_link" theme function (https://api.drupal.org/api/drupal/modules!file!file.module/function/theme_file_link/7)
* $html .= $file_render->render('link');
* // you can also pass custom theme functions, for example calling "theme_custom_image_formatter"
* $html .= $file_render->render('link', array('#theme' => 'custom_link_formatter'));
*
* }
*
*/
class TwinbitFrontendFile {
private $_type;
private $_entity;
private $_field;
private $_image;
private $_field_name;
public $renderers = array();
/**
* @param @type
* Entity type (node, user, taxonomy_term)
* @param $entity
* Entity object
* @param $field_name
* Entity field name
*/
public function __construct($type, $entity, $field_name) {
$this->setType($type);
$field_metadata_wrapper = new TwinbitFrontendMetadata($type, $entity);
$this->setField($field_metadata_wrapper);
$this->setFieldName($field_name);
$this->setEntity($entity);
// generate image array
$renderers = $this->generateRenderers();
}
private function generateRenderers() {
$images = field_view_field($this->getType(), $this->getEntity(), $this->getFieldName());
foreach ($images as $key => $item) {
if (is_numeric($key)) {
if (isset($item['#file'])) {
$file = $item['#file'];
}
if (isset($item['#item'])) {
$file = $item['#item'];
}
// check if file exists
if ($file) {
$this->renderers[] = new TwinbitFrontendFileRender($file);
}
}
}
}
public function count() {
return count($this->renderers);
}
public function getRenderers() {
return $this->renderers;
}
private function setFieldName($name) {
$this->_field_name = $name;
}
private function getFieldName() {
return $this->_field_name;
}
private function setType($type) {
$this->_type = $type;
}
private function getType() {
return $this->_type;
}
private function setEntity($entity) {
$this->_entity = $entity;
}
private function getEntity() {
return $this->_entity;
}
private function setField($field) {
$this->_field = $field;
}
public function getField() {
return $this->field;
}
}