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.

Working with Content<br />

Viewing artworks<br />

The last line of our form submit handler redirected the user to the artwork/$aid<br />

page, presumably to view the artwork we just created. That would be great if there<br />

was a page there to display the artwork. Let's add that now.<br />

First we need an appropriate menu item:<br />

function artwork_menu() {<br />

$items['artwork/%artwork'] = array(<br />

'title callback' => 'artwork_page_title',<br />

'title arguments' => array(1),<br />

'page callback' => 'artwork_page_view',<br />

'page arguments' => array(1),<br />

'access arguments' => array('view artworks'),<br />

'type' => MENU_CALLBACK,<br />

);<br />

$items['artwork/%artwork/view'] = array(<br />

'title' => 'View',<br />

'type' => MENU_DEFAULT_LOCAL_TASK,<br />

'weight' => -10,<br />

);<br />

// ...<br />

}<br />

We are planning ahead for adding more tabs ("local tasks") later, but we definitely<br />

want the View tab to be the default. The menu item defines a title callback and<br />

page callback, both of which are reasonably straightforward.<br />

function artwork_page_title($artwork) {<br />

return $artwork->title;<br />

}<br />

function artwork_page_view($artwork, $view_mode = 'full') {<br />

// Remove previously built content, if exists.<br />

$artwork->content = array();<br />

if ($view_mode == 'teaser') {<br />

$artwork->content['title'] = array(<br />

'#markup' => filter_xss($artwork->title),<br />

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

);<br />

}<br />

// Build fields content.<br />

field_attach_prepare_view('artwork',<br />

[ 176 ]

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

Saved successfully!

Ooh no, something went wrong!