-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_attribute_defaults.module
188 lines (169 loc) · 7.13 KB
/
image_attribute_defaults.module
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @file
* Implements image alt and title field defaults with support for tokens.
*/
/**
* Implements hook_field_info_alter().
*/
function image_attribute_defaults_field_info_alter(&$info) {
$info['image']['instance_settings'] += array(
'alt_field_default' => '',
'alt_field_sync_title' => 1,
'alt_field_update_on_edit' => 0,
'title_field_default' => '',
'title_field_update_on_edit' => 1,
);
}
/*
* Implements hook_form_FORM_NAME_alter() for field-ui-field-edit-form
*/
function image_attribute_defaults_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
$field = $form['#field'];
if ($field['type'] == 'image') {
$instance = $form['#instance'];
$settings = $instance['settings'];
$form_settings = &$form['instance']['settings'];
$form_settings['alt_settings'] = array(
'#type' => 'fieldset',
'#title' => t('<em>Alt</em> field settings'),
'#weight' => 11,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#parents' => array('instance', 'settings'),
);
$form_settings['alt_settings']['alt_field_sync_title'] = array(
'#type' => 'checkbox',
'#title' => t('Synchronize with <em>Title</em> field'),
'#description' => t('Replaces <em>Alt</em> field value with <em>Title</em> field value during node_save().'),
'#default_value' => $settings['alt_field_sync_title'],
);
$form_settings['alt_settings']['alt_field_update_on_edit'] = array(
'#type' => 'checkbox',
'#title' => t('Update every edit'),
'#description' => t('Reevaluate the default value and replace <em>Alt</em> field value after each edit; recommended with tokens. Requires non-empty Default value.'),
'#default_value' => $settings['alt_field_update_on_edit'],
);
$form_settings['alt_settings']['alt_field_default'] = array(
'#type' => 'textfield',
'#title' => t('Default value'),
'#description' => t('Default field value. Used after image upload, and during node_save() when <em>Alt</em> field is empty. Tokens are replaced only during node_save().'),
'#default_value' => $settings['alt_field_default'],
'#size' => 60,
'#maxlength' => 128,
);
if (module_exists('token')) {
$form_settings['alt_settings']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('node'),
);
}
$form_settings['title_settings'] = array(
'#type' => 'fieldset',
'#title' => t('<em>Title</em> field settings'),
'#weight' => 12,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#parents' => array('instance', 'settings'),
);
$form_settings['title_settings']['title_field_update_on_edit'] = array(
'#type' => 'checkbox',
'#title' => t('Update every edit'),
'#description' => t('Reevaluate the default value and replace <em>Title</em> field value after each edit; recommended with tokens. Requires non-empty Default value.'),
'#default_value' => $settings['title_field_update_on_edit'],
);
$form_settings['title_settings']['title_field_default'] = array(
'#type' => 'textfield',
'#title' => t('Default value'),
'#description' => t('Default field value. Used after image upload, and during node_save() when <em>Title</em> field is empty. Tokens are replaced only during node_save().'),
'#default_value' => $settings['title_field_default'],
'#size' => 60,
'#maxlength' => 128,
);
if (module_exists('token')) {
$form_settings['title_settings']['token_help'] = array(
'#theme' => 'token_tree',
'#token_types' => array('node'),
);
}
$form_settings['title_field']['#weight'] = 12;
}
}
/**
* Implements hook_field_widget_form_alter().
*/
function image_attribute_defaults_field_widget_form_alter(&$element, &$form_state, $context) {
if ($context['field']['type'] == 'image') {
$settings = $context['instance']['settings'];
foreach (element_children($element) as $delta) {
$element[$delta]['#process'][] = 'image_attribute_defaults_field_widget_process';
}
}
}
/**
* An element #process callback for the image_image field type.
*/
function image_attribute_defaults_field_widget_process($element, &$form_state, $form) {
$instance = field_info_instance($form['#entity_type'], $element['#parents'][0], $form['#bundle']);
$settings = $instance['settings'];
$item = $element['#value'];
// Possibly prefill the alt and title fields.
if($element['#parents'][3] == 'alt') {
$element['#default_value'] = isset($item) ? $item : $settings['alt_field_default'];
}
if($element['#parents'][3] == 'title') {
$element['#default_value'] = isset($item) ? $item : $settings['title_field_default'];
}
return $element;
}
/**
* Implements hook_preprocess_image_formatter().
*/
function image_attribute_defaults_preprocess_image_formatter(&$variables) {
//Tokens during theming, w/o node tokens.
//$variables['item']['alt'] = token_replace($variables['item']['alt']);
//$variables['item']['title'] = token_replace($variables['item']['title']);
}
/**
* Implements hook_field_attach_presave().
*/
function image_attribute_defaults_field_attach_presave($entity_type, $entity) {
// Includes interation code from _field_invoke().
$options = array(
'default' => FALSE,
'deleted' => FALSE,
'language' => NULL,
);
// Determine the list of instances to iterate on.
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
$instances = _field_invoke_get_instances($entity_type, $bundle, $options);
foreach ($instances as $instance) {
if (($instance['widget']['type'] == 'image_image') || ($instance['widget']['type'] == 'media_generic')) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
// Determine the list of languages to iterate on.
$available_languages = field_available_languages($entity_type, $field);
$languages = _field_language_suggestion($available_languages, $options['language'], $field_name);
foreach ($languages as $langcode) {
foreach ($entity->{$field_name}[$langcode] as &$field_data) {
$settings = $instance['settings'];
// If ("Update every edit" or title is empty) and a default is specified
if (($settings['title_field_update_on_edit'] || empty($field_data['title'])) && !empty($settings['title_field_default'])) {
$field_data['title'] = $settings['title_field_default'];
}
$field_data['title'] = token_replace($field_data['title'], array($entity_type => $entity), array('sanitize' => TRUE, 'clear' => TRUE));
// If Alt should equal title field, else same as above.
if ($settings['alt_field_sync_title']) {
$field_data['alt'] = $field_data['title'];
}
else {
if (($settings['alt_field_update_on_edit'] || empty($field_data['alt'])) && !empty($settings['alt_field_default'])) {
$field_data['alt'] = $settings['alt_field_default'];
}
$field_data['alt'] = token_replace($field_data['alt'], array($entity_type => $entity), array('sanitize' => TRUE, 'clear' => TRUE));
}
}
}
}
}
}