1

I want to create a block and put it in the sidebar of each content type that says "Add a new [content type]" So if the user was viewing an article, it would have "Add a new article", if the user was viewing a car, it would say "add a new car" etc.

Obviously I could make a different block and then control their display via context, but I'd rather create one block that essentially always links to add/new/[content-type] where content-type is whatever is being viewed.

Is there a way to pass in this argument via context?

1

In the hook that return the block content, call a function with a code similar to the following one.

function mymodule_node_add_link() {
  $node = menu_get_object();

  if (!empty($node->type)) {
    $types = node_type_get_types();
    return l(t('Add a new @name', array('@name' => $types[$node->type]->name)), 'node/add/' str_replace('_', '-', $node->type));
  }

  return '';
}

The hook calling this function should always check the value returned from the function. In the case the showed page is not a node page, menu_get_object() doesn't return a node object, and this function returns an empty string.

Notice that every string shown in the user interface should be passed to t(). When passing a string to t(), you should avoid concatenating string, but you should use the placeholders the function supports, as in the code I shown.

0

In D6, you could use something like this snippet...

function create_another() {

  $node=menu_get_object();

  if (!empty($node->type)) {

    return l('Add a new ' . $node->type, 'node/add/' . str_replace('_', '-', $node->type));

  }

}

which, if used on a page displaying a full node, would return a link to node/add/content type. In D7, menu_get_object() does the same thing, so you'd probably just have to change the actual link path. Just be careful if you have _'s in your node type. In D6 they need to be translated into -'s in the link above. I'm not sure about D7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.