How make single page for WP_List_Table edit? - wordpress

How I can create admin page, to edit single item from custom table WP_List_Table?
Data: Custom DB Table, on admin I have add menu item with method which build this table through WP_List_Table.
There I want to create 'manage' button, to manage single item from this table.
How can I do this?
Is there some action hook
or i have to just add second class
Or like menu item
I have try add like menu item, But how can I add it without adding to the menu? add_submenu_page with parent slug = null is it really clear WP solution?

For my situation good solution:
if ( array_key_exists( 'single', $_REQUEST ) ) {
//function to process single item
} else {
//Create an instance of our package class...
$withdraw = new Class_Table();
//Fetch, prepare, sort, and filter our data...
$withdraw->prepare_items();
}

Related

Automatically select parent page if custom field equals page title

Is it possible to automatically set/select a parent page on page creation when a custom field and page title are equal?
Example:
I have the following page hierarchy:
Providers
Provider Page (Page title = user1)
Provider Package
When a new page is created, and the custom field value is “user1” and there is a page with the title “user1”. Then the Provider Page, in this case “user1”, should automatically be set as the parent for the page that is created (Provider Package page).
I hope it is a bit clear because it is quite difficult to explain I notice myself.
Is such a thing possible?
You can achieve this by using post editing hook filters/actions. Like this
add_action( 'edit_post', 'parentsetter_save_post' );
function parentsetter_save_post()
{
global $post;
$custom_field=get_post_meta($post->ID,'customfieldname',true);
if ($custom_field!=''){
$parent_page=get_page_by_title($custom_field);
if (!empty($parent_page) and $post->post_parent!=$parent_page->ID){
global $wpdb;
$wpdb->query($wpdb->prepare("update $wpdb->posts set post_parent=%d
where ID=%d",$parent_page->ID,$post->ID));
}
}
}
Just replace "customfieldname" with your custom field name and it will work.

WP Plugin Pull out data from db for edit or delete

Hey Guys, hopefully you can help. this is my first WP Plugin, and it allows user to add car details to a DB which works fine, then outputs those details to a page.
I can add cars just fine, but now I need to be able to delete them and edit them. And this is where im a little stumped... to edit, i want to click on the item from the list up the top and its details be placed in the fields below, edit then click save to update it.
but im struggling to find out how to grab the data? should i be using Ajax or is there another way?
thanks.
I have created such plugin without using ajax
In the table I've created 2 links - "edit" and "delete", and sent the id of the element
Edit
Delete
if $info=del
if($info=="del")
{
$delid=$_GET["did"];
$wpdb->query("delete from ".$table_name." where id=".$delid);
}
and similarly for "edit"
$act=$_REQUEST["act"];
if($act=="upd")
{
$recid=$_REQUEST["id"];
$sqlL="select * from ".$table_name = $wpdb->prefix . "member where id=$recid";
$result = mysql_query($sqlL) or die ('Error, query failed');
if (mysql_num_rows($result) > 0 )
{
if($row = mysql_fetch_assoc($result))
{
$id = $row['id'];
$film_name = $row['film_name'];
// etc
}
}
}
And also I've added submenu page, where I'm adding or editing records

Adding a Drupal Commerce line item to an order from a module

I'm trying to create a gift wrapping module for Drupal commerce. I have created the checkout pane that has a select box for the user to choose if they want their order gift wrapped (and a field to select the giftwrap price on the configuration form). I've also created a giftwrap line item type. In the pane's base_checkout_form_submit() function I would like to create a giftwrap line item that is added to the order alongside the products. This is what I've got so far:
/**
* Implements base_checkout_form_submit()
*/
function commerce_giftwrap_pane_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
$default_currency_code = commerce_default_currency();
if ($balance = commerce_payment_order_balance($order)) {
$default_currency_code = $balance['currency_code'];
}
// Create the new line item.
$line_item = commerce_line_item_new('giftwrap', $order->order_id);
$line_item->line_item_label = 'Gift Wrapping';
$line_item->quantity = 1;
$line_item->commerce_unit_price['amount'] = variable_get('commerce_giftwrap_price', '2.00');
$line_item->commerce_unit_price['currency_code'] = $default_currency_code;
commerce_line_item_save($line_item);
}
I haven't wrapped it in an if statement yet, I wanted to get it working first. This code is creating a line item in the database however it isn't adding the line item to shopping cart contents view on the checkout review page. I've altered the shopping cart view to include product line items and my newly created giftwrap line items.
Any help on this would be greatly appreciated.
I've worked this out and uploaded the module as a sandbox project on drupal.
I'm not sure if this helps, but you can try to implement: hook_commerce_line_item_type_info().

Editing menu items on the fly in Drupal?

I have a menu item called "Inbox" on a menu called "dealer-menu". I want to change "Inbox" to "Inbox (1)" or "Inbox (2)" depending on the number of messages the user has in his inbox. How do I change the value ON THE FLY. I.e. on every page refresh?
I ended up solving it like this:
$dealerMenu = menu_navigation_links('menu-dealer-menu');
$menu = theme('links', $dealerMenu);
print str_replace("Inbox", "Inbox (".get_number_of_messages_in_inbox().")", $menu);
If you call hook_menu_link_alter then you can set $item['options']['alter'] = TRUE; on each menu item - you will need to edit the menu item for this hook to be called and the alter permission set to true.
Once this has been set then hook_translated_menu_link_alter will be called before each menu item is rendered allowing you to change the menu item title.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['title'] .= ' ('.get_number_of_messages_in_inbox().')';
}
}
The only limitation of this is that hook_menu_link_alter will mark every link as alterable which is not necessarily desirable (adverse effect on performance) - some additional checks on the $item here would mean you are only marking them menu items you want as alterable.

Drupal: automatically add menu items when new nodes are added

can I automatically add a menu item when I add a node to the page in Drupal?
In other words, can I associate a menu parent with a node content-type, and then automatically add the children if new nodes are added ?
thanks
You can do it with Rules on Drupal 7. This module: http://drupal.org/project/menu_rules adds some actions to rules. One of them is to create a menu item for a node. You select:
Event: Create a node | Update a node
Condition: Content type is "your content type"
Action: Update a menu item for node (there is a checkbox to create the menu item if it doesnt exist)
There's also the Menu Position module that allows to put content under specific menu entries, depending on their content type, their language and taxonomy. It also has a small API to add other criteria.
Yes.
I am sure there is a module do to something like that, but you could also create your own.
There are two ways you could go about it.
You could use hook_menu() to query for the items you want and return the correct menu structure. You would need to also make sure the menu cache is rebuilt on a node save using hook_nodeapi().
See henricks' comments below about why this is a bad idea
Alternitivly you could use hook_nodeapi() to add custom menu items with menu_link_save().
Edit
hook_menu should return an array of menu items, often these are pretty static however there is nothing wrong with these arrays being dynamically generated.
So you can query the node table to get a list of nodes you want, loop through these items and dynamically create an array which contains the correct menu items.
very roughly:
function example_menu() {
$result = db_query('select * from node where ...'); // put in your own select items and where clause
$menu = array();
while ($row = db_fetch_object($result)) {
$menu['my_path/' . $row->nid;] = array(
// See hook menu docs for what to put here.
);
}
return $menu;
}
You should take a look at the Auto Menu module - while the Drupal 6 version is still a dev release, it might cover your needs. If not, you can take it as an example of how to use menu_link_save() to create your own solution.
I would also go for a menu_link_save() call. Together with the Rules module, you can set up an action whenever a new node is saved, to create an appropriate menu item automatically.
You might want to have a look at the tutorial I wrote some time ago, which deals with programatically creating menu items using menu_link_save() and Rules: http://jan.tomka.name/blog/programmatically-creating-menu-items-drupal
Here is case where you can do this....
A node campaign creating menu item 'CAMPAIGN 001' when it is created. Using default_menu_link
Now another content type, 'Sub Campaign' creating a node, using campaign as EntityRef so its menu item should be under the Menu Item of campaign created earlier.
function mymodule_node_insert($node) {
if ($node->type == 'sub-campaign') {
if (isset($node->field_reference_campaign['und'][0]['target_id'])) {
$campaign_node_id = $node->field_photo_album_campaign['und'][0]['target_id'];
$campaign_loaded = node_load($campaign_node_id);
// Get menu link id for the campaign node.
$campaign_node_id_mlid = custom_node_mlid($campaign_node_id);
$campaign_loaded_title = strtolower(str_replace(' ', "-", $campaign_loaded->title));
$campaign_loaded_title_link_path = 'campaign/' . $campaign_loaded_title . '/photo-albums';
//I will query if it exist or not, if not then will create a sub menu item.
$link_exist = db_query("SELECT * FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $campaign_loaded_title_link_path))->fetchField();
dsm($link_exist);
if (!$link_exist) {
// Create menu item under campaign.
custom_create_menu_item($campaign_loaded_title_link_path, 'photo-albums', $campaign_node_id_mlid);
//watchdog('glue_site - Menu Item', 'Link Created');
}
else {
//dsm('Link Exist.');
watchdog('glue_site - Menu Item', 'Link Already Exist');
}
}
}
if ($node->type == 'campaign') {
}
}
Then a custom function to create menu item
function custom_create_menu_item($campaign_loaded_title_link_path, $type, $plid) {
switch ($type) {
case 'photo-albums':
$item = array(
'link_path' => $campaign_loaded_title_link_path,
// If changing the title here, change it in template.php as well.
'link_title' => 'Sub Campaign',
'menu_name' => 'menu-campaign-menu', // Menu machine name, for example: main-menu
'weight' => 0,
'plid' => $plid, // Parent menu item, 0 if menu item is on top level
'module' => 'menu',
'router_path' => 'campaign/%/sub-campaign',
'customized' => '1',
);
menu_link_save($item);
menu_cache_clear_all();
watchdog('glue_site - Menu Item', 'Link Created');
break;
}
}
To get the mlid of parent node. Campaign node...
function custom_node_mlid($nid) {
// Require menu node module.
$arr = menu_node_get_links($nid);
$mlid = array_keys($arr);
return $mlid[0];
}
For this you need menu_node
This is a simple problem that unfortunately the Drupal community has decided it wants to make complicated. Forget about all the hacky solutions with rules and hooks. There are two modules, depending on whether you're on Drupal 6 or Drupal 7, that solve the problem very elegantly. I advise against actually creating menu entries. Instead the two modules below dynamically render the nodes in the menu, so that your menu editor doesn't get filled with thousands of nodes. Then, for example, if you decide you want all the blog posts to be moved from [Our Blog] to [About Us]->[News] it's just a mater of changing one setting. No updating thousands of nodes.
D6 Menu Trails
D7 Menu Position
It looks like there's a Drupal module that does this: Auto Menu. Some more details about this module (from its project page):
The Auto Menu module automatically generates menu entries on node creation/edition. Parent menu item can be specified on a per content type basis.
This module acts when the menu section of a node is left empty only. So, users can still organize menus manually. Moreover, default setting for content types is to not create menu items automatically.
Menu Views is an interesting module for Drupal 7 to automatically generate menu links. It allows you to use the power of Views to create menu links and can be used out-of-the-box in combination with modules such as Superfish and Nice Menus.
(PS: my reputation is not high enough to provide more than two links, therefore I have marked the other modules bold instead of providing hyperlinks)

Resources