The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

148

This information is valid for Drupal 6 and 7. In Drupal 8, hook_menu() has been replaced by new routing system. Below we implement hook_menu() in three simple steps. Step one Create an empty module following the instructions in How to create an empty module. In the code shown here, it's assumed the module is named helloworld. Step two Add the following ...


56

To get the current route name, use: $route_name = \Drupal::routeMatch()->getRouteName(); You can add the current page's route name as a variable in your theme's ".theme" file. Add a _preprocess_page function like this, and clear the drupal cache. /** * Implements hook_preprocess_page(). * */ function mytheme_preprocess_page(&$variables) { $...


44

You need to define a 'root' path which is a normal menu item, and then add another item for each of the tabs. The first of these will inherit the properties of the root item, and the second will define its own callbacks. Combined with the MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK type constants, you should get the tabs you expect: function ...


41

Here comes a hopefully good explanation behind the idea of the routing system as well as the drupal specific additions to it. General overview The Symfony components have two important concepts here. The http kernel is a system which gets the request, somehow ask other systems to produce to define the piece of code which produces the requested output (a ...


37

The correct routename for view is view.VIEW_MACHINE_NAME.PAGE_MACHINENAME(in another word "view.$view_id.$display_id" ) . try something like following use \Drupal\Core\Url; $url = Url::fromRoute('view.VIEW_MACHINE_NAME.PAGE_MACHINENAME'); $form_state->setRedirectUrl($url); and for your case $url = Url::fromRoute('view.team.page_1'); // page_1 is my ...


29

access arguments is for the arguments that will be passed to the callback function...you need to use access callback instead: 'access callback' => 'user_is_logged_in' Since user_is_logged_in() doesn't take any arguments, you can safely remove access arguments for your menu item. Your current code works for the admin account only because the default ...


29

You need to make sure the menu is translatable. Do this by clicking on the "Edit Menu" option and clicking on "Translate and Localize. Menu items with language will allow translations. Menu items without language will be localized." The click "Save". The path would be as follows: site/admin/structure/menu/manage/menu_id/edit


25

If you are trying to do this in an update script, this should work: $menus = array( array( 'menu_name' => 'menu_test_one', 'title' => 'My Menu One', 'description' => 'Lorem Ipsum', ), array( 'menu_name' => 'menu_test_two', 'title' => 'My Menu Two', 'description' => 'Lorem Ipsum', ), array( 'menu_name' =...


24

If you do it like this, with the system.module callback for the parent item, you get the nice listing page when you visit 'admin/config/mymodule' /** * Implements hook_menu(). */ function MYMODULE_menu() { $items = []; $items['admin/config/mymodule'] = [ 'title' => 'My configuration section', 'description' => 'This is ...


20

You can use drush ev '\Drupal::service("router.builder")->rebuild();' to rebuild the routing information without clearing all the caches.


20

I'm not sure if there's a direct function, but one route is to use the path alias manager service to lookup the internal path then regex it for a valid node id to perform an object load on: $path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias'); if(preg_match('/node\/(\d+)/', $path, $matches)) { $node = \Drupal\node\...


18

You can test a user's status very early with event subscriber in a custom module that subscribes to KernelEvents::REQUEST. First, you register the event subscriber in mymodule.services.yml in your module folder: services: mymodule.event_subscriber: class: Drupal\mymodule\EventSubscriber\RedirectAnonymousSubscriber arguments: [] tags: - {...


17

See Strings at well-known places: built-in menus, permissions, log messages and .info files community documentation: The Drupal 6 and 7 menu system stores menu item titles and descriptions in English. This allows the system to cache the data, but display to users using various languages on demand. For this to work, you should not use t() on the title or ...


16

You can do exactly this using the Menu Node API and the Menu Node Views modules. Create a view with your required content type, and choose a contextual filter of "Menu: Menu link ID" (this is only available with the above modules installed). Under, "When the filter value is not available," select "Provide default value" and choose "PHP code." The ...


16

The function invoking hook_menu() is menu_router_build(), called by menu_rebuild(). It contains the following code. foreach (module_implements('menu') as $module) { $router_items = call_user_func($module . '_menu'); if (isset($router_items) && is_array($router_items)) { foreach (array_keys($router_items) as $path) { $...


16

There is a clever way to do this by using join:/ In my case, I used: [node:field-taxonomy-skolski:parents:join:/] where field-taxonomy-skolski is the field for choosing the term from the taxonomy tree. Forward slashes will be stripped, though there is a setting under Administration » Configuration » Search and metadata » URL aliases » URL aliases - ...


16

The correct code would be the following one; notice the commented line. function custom_food_orders_menu() { $items['order_food'] = array( 'title' => 'Order Food', 'page callback' => 'page_creator_function', 'access callback' => TRUE, 'menu_name' => 'main-menu', // This is the changed line. 'type' => MENU_NORMAL_ITEM, ...


16

Drupal 8 has a new menu system, there is no hook_menu and no hook_menu_alter anymore. If you want to alter an existing route, it is a little bit more complicated in comparison to Drupal 7. In your module you have to create a class file at YOURMODULE/src/Routing/CLASSNAME.php that extends RouteSubscriberBase: /** * @file * Contains \Drupal\YOURMODULE\...


15

Follow what reported in a comment for the l() documentation. To create a link to a named anchor (e.g. "#namedanchor"), you will need to use a small work-around. l('linktext', '', array('fragment' => 'namedanchor', 'external' => TRUE)); To create a hash-only link (to "#"), you'll need to adapt it to: l('linktext', '', array('fragment' => ' ...


15

Drupal 7 If you're using drush, you may run: drush eval 'menu_rebuild();' If won't work, see: How to do menu rebuild when having PHP Fatal error? Drupal 6 drush php-eval 'drupal_rebuild_theme_registry()' memcached If you're using memcached, you've to restart it. On Linux: /etc/init.d/memcached restart On OS X (if using Homebrew): brew services ...


15

* Updated * When I first answered this question, node edit and taxonomy edit pages weren't set as admin routes. So I added separate checks for those. Such separate checks aren't necessary any more. The following seems to cover it pretty well: if (\Drupal::service('router.admin_context')->isAdminRoute()) { // do stuff } If you want to check a route ...


14

They access $_POST in the same way other functions do. Take file_ajax_upload() as example; it contains the following code. if (empty($_POST['form_build_id']) || $form_build_id != $_POST['form_build_id']) { // Invalid request. drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) ...


14

They correspond to the type parameter of items returned from hook_menu() Normal menu entry = MENU_NORMAL_ITEM Menu type -- A "normal" menu item that's shown in menu and breadcrumbs. Normal menu items show up in the menu tree and can be moved/hidden by the administrator. Use this for most menu items. It is the default value if no menu item type is ...


14

I have just created a views page with tabs. Following are the instructions. Add a views display of type page Set its path to "/test" Then its menu option: Select "Default menu tab" and on next screen select "Already exist" under the "parent menu item". Then adds another page display set its path to "test/tab1" and in its menu option: select "Menu tab" ...


13

http://drupal.org/node/123103#comment-4955236 node/16#gohere the auto url will change it to be like about-us#gohere


13

Their is a lot of ways. I list here only things compatible with D7 and eventually on D6. Use a modules https://drupal.org/project/menu_position (need rules, watch http://vimeo.com/27074705) https://drupal.org/project/menu_trail_by_path https://drupal.org/project/context Code your stuff Their is now a native API menu_tree_set_path to set the path you want ...


13

Your page callback is drupal_get_form, and your page argument is _mymodule_confirm_offline, so the call Drupal makes is drupal_get_form('_mymodule_confirm_offline');, which doesn't at all seem to be what you want to do. What you need is a custom callback instead. In hook_menu: 'page callback' => '_confirmation_form_page', 'page arguments' => array(1,...


13

Drupal's implementation of menus is a bit special, It doesn't always work the way it seems it should. You can take a look at the core implementation of template_preprocess_page() to how the Main Menu links are added as a variable to the page template. You have to drill down a bit in the API docs, but the function you want to call in your implementation of ...


13

I've also been beating my head against a wall the last while with Drupal memory issues. Here's my collected knowledge on the topic: 1. Views (can) use a lot of memory I love me some Views (and Panels and CTools and everything merlinofchaos touches with his mighty, mighty fingers), but it's possible to create configurations with multiple relationships that ...


13

Option a) Unless I am mistaken, can you not just pick where your Taxonomy Menu will be parented (Menu Location) when you create it? The only problem I think you might get with this is I think it requires you to rebuild the menu every time the taxonomy is updated Option b) I have done something reasonably similar to this on my site, I used Menu Views module, ...


Only top voted, non community-wiki answers of a minimum length are eligible