Get table header to use pager url drupal 7 - drupal

I have got a unique issue. In my drupal site, I have got a lot of nodes displayed in a teaser and for each node, I am using a table with a pager. The problem is, on initial load of the page, the table sort doesn't work. However, if I use the pager and move to a different page and then back, then the sorting works.
I investigated this issue and found out that on initial load, the url for the header links use the primary link, which is: mysite.com/node?sort=asc&order=Name. After I click on the pager, then the url for the header links, changes to this: mysite.com/mycallbackfunction?_=1348208999187&page=1&nid=13&pager_id=1&sort=desc&order=Name
If you notice, the url obviously are different, which is okay, but the main thing is, the nid is missing in the initial load. I would like the table headers to use the pager url, or some url that I specify, so the nid would appear in both cases as the function returns nothing if there is no nid.
This is the relevant code in mycallbackfunction which displays the table:
//Attach a theme table
$html = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array(
"class" => array(
"my_table_box"
),
"cellspacing" => '0',
"cellpadding" => '0'
)));
//Pager theme
$html .= theme('pager', array(
'element' => $pager_id,
'parameters' => array(
'nid' => $nid,
'pager_id' => $pager_id),
'quantity' => ceil($count/5)
));
Hope someone might have an insight into this weird problem.

You can pass the custom parameter in theme('pager') function.
<?php
// e.g.
theme('pager', array('parameters' => array('nid' => $nid))
?>);
Let me know if you are looking for something else.
Good Luck!
-Imran

Related

WordPress - How do I retrieve custom post type posts from output of wp_get_archives

I am new to WordPress and learning by implementing. Today I have created a list of archives using wp_get_archives for a custom post type. The list comes up fine!
$args = array(
'type' => 'monthly',
'limit' => '15',
'format' => 'html',
'before' = '',
'after' => '',
'show_post_count' = true,
'echo' => true,
'order' => 'DESC',
'post_type' => 'travelog'
);
wp_get_archives($args);
Now I am stuck with two things and no idea how to solve them!
[ A ]
What I want to accomplish:
I want to achieve the functionality that when somebody clicks on an item from the list it would take him to another page with posts listed for that period only.
What is happening:
WordPress takes me back to site's home page.
How can I tell WordPress to go to a page and display the posts for that period. Posts can be custom post types as well.
[ B ]
What I want
With a Custom Post Type passed ('post_type' => 'travelog') link for each item be like http://local.tourplanner.com/2017/03/travelog
What I am getting
It is like http://local.tourplanner.com/2017/03/?post_type=ravelog
With 'post_type' => 'post', however, there is no query string added to and they look like http://local.tourplanner.com/2017/03
If possible I would love to achieve this without using any plugin.
Looking forward to your suggestions.

wp_get_archives link to that particular archive page

Setup an Archive list on the right of my page and the design has a "view" button after each item.
I'm trying to have that view button link to the archive month page.
Looking at doing something with the after and adding a href="" to the view button, However not sure what to reference to get that to happen.
My current code is as follows :
<?php
// Get Archives'
$args = array (
'type' => 'monthly',
'order' => 'DESC',
'after' => '<div class="pull-right"><a class="view" href="#">View</a></div>',
'limit' => '6'
);
$archives = wp_get_archives( $args );
?>
As you can see the 'after' parameter in the array is where I am trying to add the href.
Hope that makes sense.
Thank You!
Few things about wp_get_archives:
It doesn't return anything except if you force the echo parameter to 0 - otherwise calling the function will result of the archive links to be printed.
The after parameter is dependant of the format parameter - it is only used when using the "html" (default - list mode) or "custom" format. What it does is to display the value you pass as parameter after the link. So you don't need to reference the link inside. Use it in combination with the before parameter to achieve what you want to do here.
You don't really need to set the type as monthly, as it is the default value for this parameter. Same for the order parameter, which default is allready DESC.
So a valid call would be:
wp_get_archives(array(
'format' => 'custom',
'before' => '<div class="pull-right">',
'after' => '</div>',
'limit' => 6
));
You probably notice that it doesn't output exactly what you are trying to do, as it miss the class view on your links. You need to add a filter on get_archives_link in order to achieve this (this go in your theme functions.php):
add_filter('get_archives_link', 'get_archive_links_css_class' );
function get_archive_links_css_class($link) {
return str_replace('href', 'class="view" href', $link);
}
This will add the class before the href attribute.

drupal 7 import book set parent/book id

I need to migrate a book (some sort of manual) from an expressionengine website to drupal 7. I will be using the core book module to put the book in. Since this is a one time import I used a simple custom import script based on this one: http://www.group42.ca/creating_and_updating_nodes_programmatically_in_drupal_7.
I want to be able to set the parent id of each book page but I can't find how to do this. I already tried some intuitive things like
`$node->pid = $parentId;`
and others...
However I'm totally lost where I should look for this information on api.drupal.org . Anyone can point me in the right direction?
I don't know if you also need to set the book this page to explicitly or if setting a parent is enough...
Thanks,
Joris
Check this http://api.drupal.org/api/drupal/modules%21book%21book.module/function/book_node_insert/7
and this
http://api.drupal.org/api/drupal/modules%21book%21book.module/function/book_node_update/7
Sergey Litvinenko pointed to important part of code
<?php
define('DRUPAL_ROOT', dirname(__FILE__).'/../../../..');
chdir(DRUPAL_ROOT);
require_once "./includes/bootstrap.inc";
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$node = entity_create(
'node',
array(
'type' => 'book_name',
'title' => 'test',
'body' => array(
'und' => array(
array(
'value' => 'body text here',
'format' => 2, // Full html
),
),
),
'field_name' => ...
)
);
$node->uid = 2; // user id for author
node_save($node);
// book:
$node->book['bid'] = 21; // nid of the Top-Level Element or 'new' to create new top Level Book
$node->book['plid'] = 2607; // mlid of the parent book-page from table 'book' in db
_book_update_outline($node);

Wordpress - Remove submenu from custom post type

I created a custom post type named portfolio with tag taxonomy support.
Since WP does not make a difference between post tags and custom post type tags, I created a menu item Taxonomy under which I want to put categories and post tags. I managed to create the the menu and submenus, and also remove category and post tags from the Post menu, but i didn't manage to remove Post Tags from the custom post type menu.
I tried:
remove_submenu_page( 'edit.php?post_type=portfolio', 'edit-tags.php?taxonomy=post_tag&post_type=portfolio' );
You can use remove_submenu_page() - the trick however is to get the slug exactly right, to do this the easiest way is to dump the global $submenu and check for the menu_slug and submenu_slug.
global $submenu;
var_dump($submenu);
This will give you the array of menus, the top level key is the menu_slug and then the correct submenu_slug can be found in the element 2 of the nested arrays.
So if I had a custom post type called "my_events" and I wanted to remove the tags menu from it, my original menu structure would look like this
...
'edit.php?post_type=my_events' =>
array
5 =>
array
0 => string 'All Events' (length=10)
1 => string 'edit_posts' (length=10)
2 => string 'edit.php?post_type=my_events' (length=28)
10 =>
array
0 => string 'Add New' (length=7)
1 => string 'edit_posts' (length=10)
2 => string 'post-new.php?post_type=my_events' (length=32)
15 =>
array
0 => string 'Tags' (length=4)
1 => string 'manage_categories' (length=17)
2 => string 'edit-tags.php?taxonomy=post_tag&post_type=my_events' (length=55)
...
From this you can see that the menu_slug is 'edit.php?post_type=my_events' and the submenu slug for the tags menu is 'edit-tags.php?taxonomy=post_tag&post_type=my_events'.
So the remove function call would be:
remove_submenu_page('edit.php?post_type=my_events', 'edit-tags.php?taxonomy=post_tag&post_type=my_events');
Note that the submenu slug is html encoded so the ampersand is now & - this is probably that thing that has made it hard for people to work out from first principles what the slug name should be.
This is a bit dirty, but it works:
add_action('admin_menu', 'remove_niggly_bits');
function remove_niggly_bits() {
global $submenu;
unset($submenu['edit.php?post_type=portfolio'][11]);
}
I'm not sure exactly which key number you'll want to remove. Best way to find that is to do:
add_action('admin_menu', 'remove_niggly_bits');
function remove_niggly_bits() {
global $submenu;
//unset($submenu['edit.php?post_type=portfolio'][11]);
print_r($submenu); exit;
}
Everything will break when you load the admin area until you remove that line, but it'll show you the structure and which key you need.
It might be better to just use the 'show_ui' => false.
function car_brand_init() {
// new taxonomy named merk
register_taxonomy(
'merk',
'lease_fop',
array(
'label' => __( 'Merken' ),
'rewrite' => array( 'slug' => 'merken' ),
'update_count_callback' => '_update_post_term_count',
// use this to hide from menu
'show_ui' => false,
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'car_brand_init' );
To build on the answer from benz001:
No need to do the var dump to get the slug right. This worked for me.
remove_submenu_page('edit.php?post_type=POST_TYPE', 'edit-tags.php?taxonomy=TAXONOMY_SLUG&post_type=POST_TYPE')
First argument, replace POST_TYPE with what you see in your URL field at the top of the browser when viewing archive in back end.
Second Argument: Left click on the submenu you want to remove, copy the link, remove the last forward slash and everything to the left of it. Replace the & with &
Second arg example below.
1 Copy and paste submenu link
http://yourwebsite.com/wp-admin/edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post
2 Remove last forward slash and everything left of it.
edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post
3 Replace the & with the HTML entity &
/edit-tags.php?taxonomy=your_custom_taxonomy&post_type=your_custom_post

Drupal FAPI form calls callback twice

First post on stack overflow... so go easy on me!
There doesn't seem to be a suitable solution to the Drupal FAPI multiple callback issue for simple form submissions.
THE PROBLEM: My form, when submitted, adds two entries to the respective database table. Given that there is only one call to add it to the database, I feel it's safe to assume that the query is run twice (hence the dual entries).
The following code may help to provide a basis for a solution. Oh, it's Drupal 7 too, so documentation is still very much D6 centric.
function mymodule_sidebar_form_add_submit(&$form, &$form_state) {
$form_values = $form_state['values'];
$se_title = check_plain(trim($form_values['title']));
$se_link = url(trim($form_values['link']));
$se_content = check_plain(trim($form_values['content']));
$se_image = isset($form_values['image']) ? $form_values['image'] : '';
// The multi-line part below is actually a single line the real code
$query = sprintf("INSERT INTO sidebar_element(title, image_url, content)
VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content);
db_query($query);
drupal_set_message(t('Sidebar Element has been added successfully.'));
}
... and my form function contains a submit button:
$form['submit'] = array(
'#value' => t('Add Sidebar'),
'#type' => 'submit',
'#title' => t('Add Sidebar'),
'#submit' => array('mymodule_sidebar_form_add_submit'),
);
I guess the questions I need answered are:
Why is there a double callback in the first place?
Is there a way to identify the first callback?
Thanks in advance to all.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
$form['#submit'] = array('my_form_submit');
And replace
// The multi-line part below is actually a single line the real code
$query = sprintf("INSERT INTO sidebar_element(title, image_url, content)
VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content);
db_query($query);
with
// The multi-line part below is actually a single line the real code
$query = "INSERT INTO {sidebar_element} (title, image_url, content)
VALUES ('%s', '%s', '%s')";
db_query($query, $se_title, $se_image, $se_content);
For Drupal 7
// Add the buttons.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#access' => my_func(),
'#value' => t('Save'),
'#weight' => 100,
'#submit' => array('my_form_submit'),
);
As example read node_form() code
To find out where the second call is coming from, the easiest way is to install devel.module and use ddebug_backtrace() in your submit callback. You might need to disable the HTTP redirecto to see it, too (exit()).
But more importantly, use the API, Luke!
<?php
db_insert('sidebar_element')
->fields(array(
'title' => $se_title,
'image_url' => $se_image,
'content' => $se_content,
))
->execute():
?>
This is how your insert query should look like, what you are doing is insecure!
And for SELECT, use db_query() with named placeholders:
<?php
$result = db_query('SELECT * FROM {sidebar_element} WHERE title = :title', array(':title' => $something));
?>

Resources