Help for custom menu - drupal

Drupal 7 hook_menu() is confusing me; I have tried everything and I can't seem to get this to work.
What I need: In a custom module, I'd like to create a new menu, and add about four links to that menu. It sounds simple, but I am struggling. I've been able to create the menu itself using the $menu array in the .install file, but adding items to that menu doesn't make sense.
Code that is working:
$menu = array(
'menu_name' => 'project-menu',
'title' => $t('Project Menu'),
'description' => 'Project Menu',
);
menu_save($menu);
Code that isn't working:
$items = array();
$items['project-menu/%'] = array(
'title' => 'Test Link',
'page callback' => 'dc_project_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
This is all in the dc_project.install file under the dc_project_menu() function. Hopefully I'm just doing something stupid, any and all help is extremely appreciated. Even just pointing to me to a module that does this cleanly as an example, thanks. I did look at the example project, haven't been able to get anything as far as adding links to my new menu working.

Passing to menu_save() the content of $items doesn't work because menu_save() accepts only an array containing menu_name, title, and description.
What you use in $items is an array describing the menu callbacks implemented by a module, and the definitions of the menu callbacks implemented by all the modules are not saved in "menu_custom" (the table used from menu_save()) but are cached in a Drupal cache table.
If you are trying to change the menu callbacks defined by another module, then you should implement hook_menu_alter(); otherwise, if you just want do define the menu callbacks of your module, you should implement hook_menu().
Both the hooks implementations (hook_menu() and hook_menu_alter()) must be in the module file (in your case, in dc_project.module), not in dc_project.install. Drupal doesn't load the installation file when it normally loads the enabled modules; it loads the installation file when a module is being updated (or installed), but it doesn't load it in other cases.
The code that saves the menu with menu_save() can be in the installation file, in the implementation of hook_install() or hook_update_N(). It could also be put in the implementation of hook_enable(); in that case, the code (which is executed when the module is enabled) should first verify the menu has not been already added. (hook_enable() and hook_disable() should be placed in the installation file.)

Related

Very basic Hello World Wordpress Plugin

What I'm looking to do is create a very basic plugin for Wordpress. I've followed countless tutorials and examples, but haven't found anything close to what I'm looking for.
I'm trying to create something simple where it has a link on the admin nav bar, and just shows some static html on the right side of the page. The public wouldn't see anything at all.
This sounds like a simple task, but so far, has been everything but simple. Any help or pointing in the right direction would be appreciated. :)
Create a folder named helloworld in your plugins folder and add a file called helloworld.php. In your PHP file, use a hook to add the link to the admin menu.
<?php
/*
Plugin Name: Hello World
*/
add_action('admin_bar_menu', 'add_navbar_item', 100);
function add_navbar_item($admin_bar){
$admin_bar->add_menu( array(
'id' => 'my-item',
'title' => 'My Item',
'href' => '#',
'meta' => array(
'title' => __('My Item'),
),
));
}

Drupal: Module output not appearing on front end

I am trying to create a Drupal module. I've been able to setup the configuration form on the admin section - it runs fine: I can add the component and set configurations and save.
However, nothing appears on the front end of the site. There are no errors. I'm not sure why and, as I am new to Drupal, I'm not sure where to look.
My hook_theme in my .module file looks like:
function gallery_grid_theme($existing, $type, $theme, $path) {
return array(
'gallery_grid' => array(
'template' => 'gallery-grid',
'path' => 'plugins/content_types/gallery_grid/templates',
'type' => 'theme',
'variables' => [],
)
);
}
The .tpl file is intact and has no markup errors.
Would anyone know what file I should be looking at?
EDIT:
I've tried clearing the cache and rebuilding the registry as well as disabling and re-enabling the module, to no affect.
The module is added to a page panel as a component (gear icon, Add Content).
For some reasons this needed an absolute path:
'path' => drupal_get_path('module', 'gallery_grid') . '/plugins/content_types/gallery_grid/templates',

Why should I use theme() function in page.tpl.php?

Im working on my first site in drupal and I have also read some basics of theming and module development. Now I am creating (overriding stark theme) my own theme, i.e. page.tpl.php and there is an theme() function called for outputting main menu items:
<?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu'))); ?>
I roughly understand what this function is for, but why should I use it in this case? It would make sense if outputting data from module - to stylize that output by selected theme. But in this case everything I need is directly in $main_menu array and I can stylize it however I want, so what's the use for theme() function in page.tpl.php?
Why should I use theme() function in page.tpl.php?
You shouldn't.
To avoid calling theme() function in your page template, you can do this in your template.php:
/**
* Implements hook_preprocess_page().
*/
function yourtheme_preprocess_page(&$vars) {
$vars['main_menu'] = theme('links__system_main_menu', array('links' => $vars['main_menu'], 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu')));
}
And then simply print $main_menu; in your page.tpl.php.
The point of using theme('links__system_main_menu', ...) is to re-use the existing generic links theme implementation of the theme. The system_main_menu suffix (after the two _), allow you to provide a more specific implementation of the generic links. If you then override the page template for the front page (ie. page-front.tpl.php) and for nodes of a specific content, you don't have to duplicate your HTML code. Which make it easier to maintain since you won't have to duplicate changes to multiple files.
The point is that if you go trough drupal's theme system it gives a chance to other installed modules to do their changes - their hook functions will be called:
https://www.drupal.org/node/933976
In other words, if you don't do it "clean" way it may happen that some other's module feature won't work.

Creating a delete confirmation for images using the Wordpress meta box plugin

I am using the Meta Box plugin for Wordpress. I can successfully create fields in the cms for users to upload images. I would like to extend this in two ways:
First, I would like a delete confirmation when users remove an image from the image gallery
Here is the code:
$meta_boxes[] = array(
'id' => 'project_media',
'title' => 'Project Media',
'pages' => array( 'project' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Media Gallery',
'desc' => 'Images should be sized to 983px x 661px',
'id' => $prefix . 'project_media_gallery',
'type' => 'image'
)
);
This creates upload functionality in the custom post type where users can add images to a slideshow. The problem is if the user accidentally clicks the delete button, there is no confirmation to make sure it is deleted. Can I somehow extend the plugin through functions and call an alert when this button is clicked? Something that does not involve editing the WP core?
Second, the base functionality requires the user to upload an image from their local machine. Is there a way to tap into the Media Library for this?
No idea how to even start tackling this one.
To answer the first question
First, I would like a delete confirmation when users remove an image from the image gallery
You can do that by calling a custom script file from the functions.php.
function alert_delete() {
if(is_admin()){
wp_register_script( 'alert_delete', get_bloginfo('template_url'). '/js/alert_delete.js', array('jquery'));
wp_enqueue_script('alert_delete');
}
}
and create a file named alert_delete.js in the js directory of your theme.
alert_delete.js:
// admin delete check
jQuery(document).ready(function(){
jQuery(".rwmb-delete-file").click(function() {
if (!confirm("Are you sure? This process cannot be undone.")){
return false;
}
});
});
In response to the second question...
Second, the base functionality requires the user to upload an image
from their local machine. Is there a way to tap into the Media Library
for this?
Get the latest version of the Meta Box Plugin first.
then change
'type' => 'image'
to
'type' => 'image_advanced'
which will allow you to upload from the existing Media Gallery or a new file from your computer.

Drupal programmatically adding an item to a menu

I wish to conditionally add an item to a menu. I have a custom module and a menu called "links". How would I add an item to a menu in my module code?
You need to implement hook_menu in your module. Example:
<?php
function mymodule_menu() {
$items['mymodule/links'] = array(
'title' => 'Links',
'page callback' => 'mymodule_links_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
?>
The 'type' => MENU_SUGGESTED_ITEM, part makes it optional, so it can be enabled by the end user - is that what you meant with "conditionally"? If not, please explain what kind of "conditionally" you're looking for.
Or you can use the 'type' => MENU_NORMAL_ITEM, since it is enabled by default, but can be disabled any time. This of course depends on your preferences. See http://api.drupal.org/api/drupal/includes--menu.inc/group/menu/7 for further reference.
Another good thing to know when using module defined menu items in custom menus might be how to programmatically create the menu you want to use so that everything is created "out of the box". Simply add a mymodule.install-file where you put the following code:
<?php
function mymodule_install() {
$menu = array(
'menu_name' => 'links',
'title' => 'My Custom Links',
'description' => 'Descriptive text.',
);
menu_save($menu);
}
?>
If you have an uninstall function, don't forget to not only deactivate the module, but also to uninstall it. Reenable the module, flush your caches and the menu item should be there!
You can dynamically show or hide a menu item based on a condition ( access callback ).
Here is an example from https://drupal.org/project/examples:
<?php
function mymodule_menu() {
$items = array();
$items['my-menu-item'] = array(
'title' => 'My Menu',
'description' => 'My description',
'page callback' => 'my_page_link_callback_function_name',
'access callback' => 'can_the_user_see_this_item',
'expanded' => TRUE,
'weight' => -100,
'menu_name' => 'primary-links',
);
return $items;
}
// Here we determine if the user can or can not see the item.
function can_the_user_see_this_item(){
if (MY_CONDITION){
return TRUE;
}
else {
return FALSE;
}
}
The menu system is cached, so you can't add or remove menu items as you please based on user, page viewed, custom logic etc. That is you can't do it without having to clear the menu cache which would cause a severe performance hit.
What you could do, to create this effect is to create some custom logic to define the access control on the menu item. Since Drupal hides menu items that users doesn't have access to, you could under certain circumstances deny permission to hide the menu item. This is a bit hackish solution.
Another solution which I would prefer, would be to use js or css to hide or show the menu. You could dynamically add/remove a class on the body to determine if the menu item should be shown or not. This would quickly become unmanagable if you need several of these kind of menu items, however.
Use menu_link_save() function
Saves a menu link.
After calling this function, rebuild the menu cache using menu_cache_clear_all().
Parameters
$item: An associative array representing a menu link item, with elements:
link_path: (required) The path of the menu item, which should be normalized first by calling drupal_get_normal_path() on it.
link_title: (required) Title to appear in menu for the link.
menu_name: (optional) The machine name of the menu for the link. Defaults to 'navigation'.
weight: (optional) Integer to determine position in menu. Default is 0.
expanded: (optional) Boolean that determines if the item is expanded.
options: (optional) An array of options, see l() for more.
mlid: (optional) Menu link identifier, the primary integer key for each menu link. Can be set to an existing value, or to 0 or NULL to insert a new link.
plid: (optional) The mlid of the parent.
router_path: (optional) The path of the relevant router item.
$existing_item: Optional, the current record from the {menu_links} table as an array.
$parent_candidates: Optional array of menu links keyed by mlid. Used by _menu_navigation_links_rebuild() only.
Return value
The mlid of the saved menu link, or FALSE if the menu link could not be saved.

Resources