I need a way to make the image attach required for certain content types. Is there any support for this?

Comments

drewish’s picture

there is not. you might want to look at the imagefield module for that.

ebeyrent’s picture

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

ebeyrent’s picture

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
Brian@brianpuccio.net’s picture

Status: Active » Closed (works as designed)

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).