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 page will simply generate a bulleted list of menu items that are immediate<br />

children of the current page being viewed. menu_get_item() returns the current<br />

menu item while system_admin_menu_block(), despite its name, is a useful general<br />

purpose function for getting the children of an arbitrary menu item.<br />

There are two other points to note here:<br />

• First, the link takes the localized_options key of the menu item as its own<br />

options array. That way we allow the menu system to handle what attributes<br />

the link should have, any interaction with the translation system, and so on.<br />

• Second, we run the description through filter_xss_admin(). The function<br />

will simply strip out HTML tags that are unsafe. The filter_xss() function<br />

lets us define an arbitrary list of allowed tags, while filter_xss_admin() is<br />

simply a wrapper that includes a fairly permissive set of allowed tags.<br />

Adding new artwork<br />

Now we need to provide a form to add our new artworks. We defined a menu<br />

callback named artwork_add earlier, so let's define that callback.<br />

function artwork_add($type) {<br />

global $user;<br />

}<br />

$types = artwork_types();<br />

$type = isset($type) ? str_replace('-', '_', $type) : NULL;<br />

if (empty($types[$type])) {<br />

return MENU_NOT_FOUND;<br />

}<br />

$artwork = entity_get_controller('artwork')->create($type);<br />

drupal_set_title(t('Create @name', array('@name' =><br />

$types[$type]->name)), PASS_THROUGH);<br />

return drupal_get_form($type . '_artwork_form', $artwork);<br />

As before, we clean up the artwork type (bundle) name and then if there is no such<br />

artwork type we return a 404 not found error page. Next we create a new, empty<br />

artwork object, set a page title, and then display a form.<br />

[ 167 ]

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

Saved successfully!

Ooh no, something went wrong!