18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6<br />

array($artwork->aid => $artwork),<br />

$view_mode);<br />

entity_prepare_view('artwork', array($artwork->aid => $artwork));<br />

$artwork->content += field_attach_view('artwork', $artwork,<br />

$view_mode);<br />

}<br />

return $artwork->content;<br />

The title callback needs no explanation. The artwork_page_view() function<br />

again doesn't do much beyond shepherd data off to the Field API. In particular, note<br />

that it does take a $view_mode parameter, which should be one of the view modes<br />

we defined back in the artwork_entity_info(). Even if we don't want to leverage<br />

it ourselves the Field system will require it.<br />

If we're in "teaser" mode, we want to inject the title of the artwork into the viewable<br />

result. If not, we have no data to display other than the attached fields as the title is<br />

already being displayed as the page title. The field code is a fairly boilerplate three<br />

lines of code that produce a renderable array, which we simply return.<br />

That wasn't so hard, was it?<br />

Editing an artwork<br />

Now that we have artworks we can see, we want to be able to go back and edit them.<br />

Fortunately most of the work we already did as part of the creation process. In fact,<br />

we're going to reuse the same form and same save() method of the controller. All<br />

we really need to add is the page callback to use it.<br />

function artwork_menu() {<br />

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

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

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

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

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

'weight' => 0,<br />

'type' => MENU_LOCAL_TASK,<br />

'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,<br />

);<br />

// ...<br />

}<br />

function artwork_page_edit($artwork) {<br />

$types = artwork_types();<br />

[ 177 ]

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

Saved successfully!

Ooh no, something went wrong!