Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
I ended up writing a module that implements hook_form_alter:
function mymodule_forms_form_alter($form_id, &$form)
{
switch($form_id)
{
case 'some_content_type_node_form':
if ($form['#node_type']->type != 'image')
{
$type = $form['type']['#value'];
// If enabled adjust the form.
if ($form_id == $type .'_node_form' && variable_get('image_attach_'. $type, 0))
{
// Make the image attach field required
$form['image_attach']['image']['#required'] = TRUE;
}
}
break;
}
} // End Function
The code above had an error - if you attached and image and submitted the content type, an error was thrown saying that the image field was required. This code here fixes the issue:
function mymodule_forms_form_alter($form_id, &$form)
{
switch($form_id)
{
case 'some_content_type_node_form':
if ($form['#node_type']->type != 'image')
{
$type = $form['type']['#value'];
// If enabled adjust the form.
if ($form_id == $type .'_node_form' && variable_get('image_attach_'. $type, 0))
{
// If an image has been attached, there will be a #value key. If it's not there, make the field required.
if(! array_key_exists('#value', $form['image_attach']['iid']))
{
// Make the image attach field required
$form['image_attach']['image']['#required'] = TRUE;
}
}
}
break;
}
} // End Function
Open this up again if need be, but it seems like this is by design and you've created your own solution (and documented it here for others to find, thanks).
Comments
Comment #1
drewish commentedthere is not. you might want to look at the imagefield module for that.
Comment #2
ebeyrent commentedI ended up writing a module that implements hook_form_alter:
Comment #3
ebeyrent commentedThe code above had an error - if you attached and image and submitted the content type, an error was thrown saying that the image field was required. This code here fixes the issue:
Comment #4
Brian@brianpuccio.net commentedOpen this up again if need be, but it seems like this is by design and you've created your own solution (and documented it here for others to find, thanks).