forked from ding2/ting_infomedia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting_infomedia.field.inc
151 lines (138 loc) · 4.06 KB
/
ting_infomedia.field.inc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* @file
* An example field using the Field API.
*
* This is updated from Barry Jaspan's presentation at Drupalcon Paris,
* @link http://acquia.com/community/resources/acquia-tv/intro-field-api-module-developers Video Presentation @endlink
*
* Providing a field requires:
* - Defining a field
* - hook_field_info()
* - hook_field_schema()
* - hook_field_validate()
* - hook_field_is_empty()
*
* - Defining a formatter for the field (the portion that outputs the field for
* display)
* - hook_field_formatter_info()
* - hook_field_formatter_view()
*
* - Defining a widget for the edit form
* - hook_field_widget_info()
* - hook_field_widget_form()
*
* *
/**
* Implements hook_field_info()
* description of field
*/
function ting_infomedia_field_info() {
$ret = array(
'ting_infomedia' => array(
'label' => t('A link to infomedia articles'),
'description' => t('This field links to infomedia articles'),
// optional (for database)
'settings' => array('max_length'=>255,),
// see hook_widget_info
'default_widget' => 'hidden',
// see hook formatter_info
'default_formatter' => 'ting_infomedia_default',
// A boolean specifying that users should not be allowed to
// create fields and instances of this field type through the UI.
// Such fields can only be created programmatically with
// field_create_field() and field_create_instance().
'no_ui' => TRUE,
),
);
return $ret;
}
/**
* Implements hook_field_formatter_info
*
* Notes; 'field types' are passed on to hook_field_formatter_view
*/
function ting_infomedia_field_formatter_info() {
return array(
'ting_infomedia_default' => array(
'label' => t('Default'),
'field types' => array(
'ting_infomedia',
),
)
);
}
/**
* Implements hook_field_formatter_view()
*/
function ting_infomedia_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$id = $entity->ding_entity_id;
$relation_types = ting_infomedia_get_ting_relations();
switch($display['type']) {
case 'ting_infomedia_default' :
foreach($items as $delta=>$items) {
foreach ($entity->relations as $relation) {
if( isset($relation_types[$relation->type]) && $entity->ac_source == 'Artikler' ) {
$element[$delta] =
array(
'#markup' => l(t('Read article'), ting_infomedia_get_article_link($id), array('attributes' => array('class' => 'infomedia_group','name'=>$relation->type))),
);
}
}
}
break;
}
return $element;
}
/**
* Parse the entityid and return an url
*
* @param String $entityid
* @return String
*/
function ting_infomedia_get_article_link($entityid) {
$ids = explode(':', $entityid);
return 'ting/infomedia/' . $ids[1];
}
/**
* Implements hook_field_validate
* TODO is some kind of validation needed?
*/
function ting_infomedia_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
// do nothing
}
/**
* Implements hook_field_is_empty
* TODO; check
*/
function ting_infomedia_is_empty($item, $field) {
return false;
}
/**
* Implements hook_field_load().
* Notes: Define custom load behavior for this module's field types.
*/
function ting_infomedia_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
foreach ($entities as $id => $entity) {
// We could create relations as multiple values here, but it's
// cached after this hook, so we don't.
$items[$id][0] = array(
'id' => $id,
);
}
}
/**
* Implements hook_field_formatter_prepare_view
* Notes; add the field to given item(s)
* we could also use hook_field_load or hook_field_prepare_view
*/
function ting_infomedia_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
foreach ($entities as $id => $entity) {
// We could create relations as multiple values here, but it's
// cached after this hook, so we don't.
$items[$id][0] = array(
'id' => $id,
);
}
}