Make image attach required?

ebeyrent - December 18, 2007 - 20:44
Project:Image
Version:5.x-1.6
Component:image_attach
Category:support request
Priority:normal
Assigned:Unassigned
Status:by design
Description

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

#1

drewish - December 18, 2007 - 22:47

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

#2

ebeyrent - December 19, 2007 - 15:49

I ended up writing a module that implements hook_form_alter:

<?php
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
?>

#3

ebeyrent - December 21, 2007 - 17:29

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:

<?php
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
?>

#4

Brian@brianpucc... - April 6, 2008 - 19:03
Status:active» by design

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

 
 

Drupal is a registered trademark of Dries Buytaert.