Hi,

I have php code for a small custom menu (I've done this as one of the menu items only needs to show on node pages, and another for a specific view page) but I'm pretty confused about how to go about getting this to display on my page (I want it in the sidebar only when a user is logged in). I see options like this:

1) Add a block with php code straight in the block, then place it on page via context (this works but is it a good idea to have php in blocks?)
2) Put it in a function in template.php (and then do you need to call that in the template page i.e. page.tpl.php??)
3) Make a module which creates this block, then place it on page via context

I can't make head nor tail over template.php/process/preprocess/hooks and finding a straightforward guide that explains these things is proving to be difficult. Is this something that is even done in the theme itself?

Some advice would be extremely helpful! Thanks.

Sam.

Comments

VM’s picture

I'd avoid PHP in blocks. IIRC the php filter is being removed from core in D8.

Based on your proposal, you could create multiple menus and use path visibility and therefore require no code at all.

samwillc’s picture

Hi,

Thanks but I'm trying to learn more advanced stuff, and not sure how I'd achieve the following through the UI alone. Here is the code so far, this may help to illustrate what I'm doing:

<?php
global $user;

if ($user->uid) {

echo '<ul class="menu">';

$views_page = views_get_page_view();
if (is_object($views_page)) { <----I also need another condition: is a view and also ==  Display name: FAQ page 
echo '<li>';
print l( 'Edit FAQ sort order', 'admin/FAQs/sort' );
echo '</li>';
}

echo '<li>';
print l( 'Content overview', 'admin/content' );
echo '</li>';

echo '<li>';
print l( 'Add content', 'node/add' );
echo '</li>';

if (arg(0) == 'node' && is_numeric(arg(1))) {
echo '<li>';
print l( 'Edit this page', 'node/' . arg(1) . '/edit' );
echo '</li>';
}

echo '<li>';
print l( 'My account', 'user' );
echo '</li>';

echo '<li>';
print l( 'Logout', 'user/logout' );
echo '</li>';

echo '</ul>';
}
?>

I have pasted this into a block (BAD!) and it works ok, other than the 'edit sort' link appears on ALL views, not just the one I want it on.

How do I then get this into my sidebar without putting this lot into a block with php filter? Is it done via template.php? This is where my confusion is.

Sam.

VM’s picture

if a block is needed you would create a module that would make the block available. template.php wouldn't be used to create a block.

samwillc’s picture

Finally, an answer that explains it!

Thanks so much, now I can work on making a small module for this. This is a small menu I always seem to need so having in a module would be a good idea.

I don't like the view/edit tabs (so I hide them) and don't need a whole module (i.e. admin) for when a client is logged in. Just a small sidebar menu suffices that allows them to do a few things.

Sam.

VM’s picture

I've nothing specific but since it's a menu it may make sense to hook into the menu api and that may take care of the issue with the edit link showing on all views but don't quote me.

samwillc’s picture

Hi, it's the 'Edit FAQ sort order' that's specific to this site that should only show on the view page it's associated with.

The 'Edit this page' link works as expected, only showing up when on a node page. I always have a little client menu and a module would be an easy way for me to implement this. I haven't got to the module permissions bit yet but things will become clearer as I work my way along.

Link to tutorial: http://drupal.org/node/1095546

I'll look into the hook menu api also, thanks. I have to start somewhere and this seems as good a place as any! It would be good to know where 'theme functionality' and 'module functionality' differ. It's these sorts of things that in my opinion are quite blurred for newbies, and documentation on Drupal seems rather technical, maybe more the terminology rather than anything else.

Sam.

VM’s picture

IIRC module vs theme is akin to logic vs display.

for example menus/blocks are built before they are sent to the theme layer. Which isn't to say that preprocessing can't be in a custom module as well nor that stuffing logic into the theme can't be accomplished. I tend to try and do everything I can to keep all logic out of the theme. Doing so keeps the theme and functionality as portable as possible.

samwillc’s picture

Thanks.

I know what you mean, wordpress has that problem where people put all kinds of cool stuff in the theme, until someone changes the theme, and the cool stuff goes with it!

Sam.