When migrating from image to imagefield, I want to keep the image_attach functionality, so that requires an image_field attach module.
I have mostly copied and replaced in the image_attach module to get this far, but I suspect most of the dificult work still remains, for instance using imagecache to get the desired image size. There are also some calls to image.module functions remaining that need to be replaced.
I am replacing the _image_attach_get_image_nodes() function in image attach with:
function _imagefield_attach_get_images() {
$query = db_query('SELECT DISTINCT field_name FROM {content_node_field_instance} WHERE widget_module = imagefield');
$rows = array(0 => t('None'));
while ($field_name = db_fetch_object($query)) {
$table_name = 'content_' . $field_name;
$result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n INNER JOIN {$table_name} i ON n.nid = i.nid n WHERE n.status = 1 AND n.type = 'image' ORDER BY n.sticky DESC, n.title ASC"));
while ($node = db_fetch_object($result)) {
$rows[$node->nid] = $node->title;
}
}
return $rows;
}
and also the selection to select the attached image through the use of hook_form_alter() :
if (isset($form['type']['#value'])) {
$type = $form['type']['#value'];
// If enabled adjust the form.
if ($form_id == $type .'_node_form' && variable_get('imagefield_attach_'. $type, 0)) {
$node = $form['#node'];
_image_check_settings();
$form['#attributes'] = array("enctype" => "multipart/form-data");
$form['imagefield_attach'] = array(
'#type' => 'fieldset',
'#title' => t('Attached images'),
'#collapsible' => TRUE,
'#collapsed' => empty($node->iid),
);
if (!empty($node->iid)) {
$image = node_load($node->iid);
$form['imagefield_attach']['image_thumbnail'] = array(
'#type' => 'item',
'#title' => t('Thumbnail'),
'#value' => image_display($image, 'thumbnail')
);
}
$form['imagefield_attach']['iid'] = array(
'#type' => 'select',
'#title' => t('Existing image'),
'#options' => _imagefield_attach_get_images(),
'#default_value' => empty($node->iid) ? NULL : $node->iid,
'#description' => t('Choose an image already existing on the server if you do not upload a new one.')
);
}
}
Any help here (even comments on approach) and elsewhere would be much appreciated.
Also, if there is a better way than attaching pre-existing images to nodes is available, I am all ears. (I want to attach easily a resized image to multiple nodes.)
(so yes, I did the easy bits :D)
| Comment | File | Size | Author |
|---|---|---|---|
| imagefield_attach.zip | 21.51 KB | naheemsays |
Comments
Comment #1
naheemsays commentedok, it seems there is another way to do this - I found this modue: http://drupal.org/project/noderef_image_helper. Step by step if it helps others.
1. install http://drupal.org/project/noderef_image_helper along with imagecache and other required modules.
2. Add a node reference field to the content type you want to attach the image to.
3. Set up the node type in "node reference image helper" that is the image node.
4. Add css to theme the position of the image as required.
5. wahay!