18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6<br />

This hook defines a new form on the fly for every artwork type we have, and<br />

declares that all of them should use the same callback function, artwork_form().<br />

Now all we need is the artwork_form() function and all artwork forms will have<br />

the same processing.<br />

function artwork_form($form, &$form_state, $artwork) {<br />

// Set the id to identify this as an artwork edit form.<br />

$form['#id'] = 'artwork-form';<br />

// Save the artwork for later, in case we need it.<br />

$form['#artwork'] = $artwork;<br />

$form_state['artwork'] = $artwork;<br />

// Common fields. We don't have many.<br />

$form['title'] = array(<br />

'#type' => 'textfield',<br />

'#title' => t('Title'),<br />

'#default_value' => $artwork->title,<br />

'#weight' => -5,<br />

'#required' => TRUE,<br />

);<br />

$form['revision'] = array(<br />

'#access' => user_access('administer artworks'),<br />

'#type' => 'checkbox',<br />

'#title' => t('Create new revision'),<br />

'#default_value' => 0,<br />

);<br />

// Add the buttons.<br />

$form['buttons'] = array();<br />

$form['buttons']['#weight'] = 100;<br />

$form['buttons']['submit'] = array(<br />

'#type' => 'submit',<br />

'#value' => t('Save'),<br />

'#weight' => 5,<br />

'#submit' => array('artwork_form_submit'),<br />

);<br />

if (!empty($artwork->aid)) {<br />

$form['buttons']['delete'] = array(<br />

'#access' => user_access('delete artworks'),<br />

'#type' => 'submit',<br />

'#value' => t('Delete'),<br />

'#weight' => 15,<br />

[ 169 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!