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 />

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

'weight' => 1,<br />

'menu_name' => 'management',<br />

'file' => 'artwork.pages.inc',<br />

);<br />

foreach (artwork_types() as $type) {<br />

$type_url_str = str_replace('_', '-', $type->type);<br />

$items['artwork/add/' . $type_url_str] = array(<br />

'title' => $type->name,<br />

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

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

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

'access arguments' => array('create artwork'),<br />

'description' => $type->description,<br />

);<br />

}<br />

// ...<br />

return $items;<br />

}<br />

The first menu item creates an index page of artwork types for us to be able to create<br />

artwork types. We then loop over all available artwork types and create a menu<br />

item for each, converting underscores in each artwork type into dashes. Each menu<br />

item's title is the name of the artwork type, but to be safe we run it through the<br />

check_plain() function. check_plain() will take whatever is specified in the title<br />

and escape any and all HTML in it, making it safe to use as the page title. That's<br />

always necessary when displaying a user-generated string as a page title to avoid<br />

both security risks and potentially broken pages.<br />

The listing page is fairly simple, but does show a nice feature of the menu system.<br />

The following is an example:<br />

function artwork_add_page() {<br />

$item = menu_get_item();<br />

$links = system_admin_menu_block($item);<br />

foreach ($links as $link) {<br />

$items[] = l($link['title'], $link['href'],<br />

$item['localized_options'])<br />

. ': ' . filter_xss_admin($link['description']);<br />

}<br />

return theme('item_list', array('items' => $items));<br />

}<br />

[ 166 ]

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

Saved successfully!

Ooh no, something went wrong!