I used to know how to do this, but I can't seem to get sorting to work on a view where filters are exposed in a block. I want to be able to filter by, for example, type, price etc, but then also have sorting options to sort by these items.
How do I get sorting to work like this?
I used that code to override sorting in non-table views
function views_tweak_views_query_alter(&$view, &$query) {
if ($view->name == 'products'){
if (arg(3) == 'pu') $query->orderby[0]='uc_products_sell_price ASC';
if (arg(3) == 'pd') $query->orderby[0]='uc_products_sell_price DESC';
if (arg(3) == 'nu') $query->orderby[0]='node_title ASC';
if (arg(3) == 'nd') $query->orderby[0]='node_title DESC';
}
}
and placing into view template links with those urls
AFAIK you can't expose sort criteria like you can with filters.
I looked a bit around a found this module. The idea is to create several views each with a different sort criteria and link them together with tabs. It's a bit hackish and might not work with exposed filters. The module is still in beta release, and I haven't tested it, so can't say if it's any good.
If you choose to use a table layout, you can sort by columns. That functionality is built into views.
Just in case you cannot find where to set this, look on the left side of the View (in Edit mode), under Basic Settings, select "Table". The click the "settings" (looks like a little gear icon to the right of the "table" selection), and you'll see a list of all the display fields, where you can select which ones are sortable/not, and which is the default sort.
Adding sorting to a view on Drupal less programm code in hooks.
You need to use arguments in display page.
Use taxonomy menu for pages before.. Next:
Create one display page in view with path (for example):
some_path/%/by_totalcount
1.1. Make sort criteria for this display by totalcount
Make another one dislplay page in this view with path (for example):
some_path/%/by_date
2.1. Make sort criteria for this display by date
Create new block with code:
<?php
$url= urldecode($_SERVER['REQUEST_URI']);
switch($url)
{
case '1':
$class = 'top';
$title_h2 = 'top';
break;
case '/taxonomy/term/6 3 ':
$class = 'travel';
$title_h2 = 'travel';
break;
.................
}
global $base_url;
$url_rating = $base_url.'/'.arg(0).'/'.arg(1).'/'.arg(2).'/rating';
$url_created = $base_url.'/'.arg(0).'/'.arg(1).'/'.arg(2);
?>
<div class="<?php print $class; ?>">
<div class="title">
<h2>
<?php print $title_h2; ?></h2>
<p>Sort node: <span class="sort_type">by rate</span> | <span class="sort_type"> by date</span></p>
</div>
</div>
<p> </p>
VOILA
Sorry for my english.. it isn't my native language ))
Related
I have taxonomy terms hierarchy and I am using them as a filter in a view. It is showing hierarchy in the selectbox, but child terms appearing with hyphen(-). I have tried to drop hyphen in form alter, and I did, but I couldn't replace it blank space.
if ($form_id == 'views_exposed_form') {
$i = 0;
foreach ($form['field_region_tid']["#options"] as $op) {
foreach ($op->option as $key => $arr) {
if ($arr != null) {
$form['field_region_tid']["#options"][$i]->option[$key] = str_replace("-"," ", $arr);
}
}
$i++;
}
}
How can I put blank space at the beginning of the child options. Or should I do some other way?
Thanks!
From the form_id I get that it is a form generated by views. If so, the code inside the if condition never gets called because your module is executed after the views module due to the weight of both modules. To fix that:
Open your database manager (PhpMyAdmin in most cases) and open system table.
Then change the weight for your module to something larger than 10, because it is the default weight for the views module and you need you module to be executed after the views module.
Hope this helps... Muhammad.
if the problem is when your hook is called the answer lies in
hook_module_implements_alter()
but it is not as you say. Your code looks fine,
Have you tried
str_replace("-"," ", $arr);
?
basically I have a three-level menu in wordpress and I've got the following code in the front-end to call the third-level menu:
$children = get_pages('child_of='.$include_page_ids[$i]);
if (count($children) > 1) {
$sub = "<ul>";
foreach ($children as $child){
$sub .= "<li><a href='#$child->post_title'>";
$sub .= $child->post_title;
$sub .= "</a></li>";
}
$sub .="</ul>";
echo $sub;
}
This calls a list for the children of a certain page and also makes the the anchors (which I also need). The problem is that right now they are being displayed in an alphabetical order, but I need to be able to set the right order myself (ie to be the same as in the backend menu). Please hepl me with it, how can I achieve this? For example this is the page http://www.eboxlab.net/transbeam/support/support/, youcan see the third level-menu as the box right next to the banner (Acceptable Use Policy to Terms & Conditions). THe order of the blocks which it corresponds to is right, but the menu is alphabetically ordered.
Help really appreciated.
PS: if you need I can provide the template code
It's pretty simple actually. Here is what your $children call should look like:
$children = get_pages('child_of=' . $include_page_ids[$i] . '&orderby=menu_order&sort_order =ASC');
That's all you need to add - this tells the query to order pages in ascending fashion by their "menu_order" column(or the "Order" field under Page attributes). You can see more details on the get_pages function at Function Reference/get pages
wp_nav_menu to the rescue! Assuming you're using WP 3 or newer, it will let you wrap elements in whatever markup you'd like, and will correspond to however you've configured the menu in the admin Dashboard.
I'm really struggling to Google my way out of this issue. I've set-up custom post-types, and I've managed to include those custom types in the index page. But now I really need a way of styling those entries differently to the regular posts. Does anyone know how I can do this? It's not just styling, but also, replacing/adding/removing certain code, as the custom types operate/look differently to regular posts.
Any help would be HUGELY appreciated!
As for the CSS, inside your loop, when you're defining the element that contains your post, try doing something like this
<article class="<?php echo $post->post_type; ?>" ...>
This will give you a class you can hook onto within your CSS file.
You can also use WordPress's built-in post_class function to achieve a similar result
<article <?php post_class($post->post_type); ?>>
That will add the post type to the rest of the default post classes, so you can still style those elements differently.
As for the different code for different post types, one of the answers above me mentioned using an if statement based on the post type:
if ('my_post_type' == $post->post_type)
{
// Code Here
}
I hope this helps you.
Codex Reference for post_class
you can use this code inside the loop:
if ( 'custom_post_type' == get_post_type() ){
//do whatever
}
or if you need to check some post types you can use a switch like:
switch ( get_post_type() ) {
case 'post':
//do whatever;
break;
case 'custom_post_type_1':
//do whatever;
break;
case 'custom_post_type_2':
//do whatever;
break;
}
Can I hide the
Edit | View
tabs on top of each node ?
I've searched for this option in theme settings (both global and standard theme but I couldn't find it).
I still want to be able my customer to edit / administer content, so I cannot just remove the permission for it.
thanks
here is a very easy solution for you. (Drupal 7)
Open your page.tpl.php in your current template and search for the $tabs variable.
Remove the render code if you want to hide it completely.
If you want to display it only to administrators use this code
<?php if ($tabs and $is_admin): ?>
<div class="tabs">
<?php print render($tabs); ?>
</div>
The above code checks if the user is administrator. If it is it will render the tabs. If not it wont render them.
This really is a presentational thing, not a functionality thing, so it should be done at the theme level.
The problem with overriding theme_menu_local_tasks() is that you override/take a hatchet to the entire local task display, when you really just want to get in there with a scalpel to remove two specific local tasks. So, you need to get a little more specific.
theme_menu_local_tasks() gets the current page's local tasks and passes them to menu_local_tasks(). Here, two theme functions are used:
theme_menu_item_link(), which gets the link markup for the task
theme_menu_local_task(), which gets the <li> element for the task.
So, you can get rid of the View and Edit local tasks in a really robust way by overriding theme_menu_item_link() and theme_menu_local_task() to include your check for them:
function mytheme_menu_item_link($link) {
// Local tasks for view and edit nodes shouldn't be displayed.
if ($link['type'] & MENU_LOCAL_TASK && ($link['path'] === 'node/%/edit' || $link['path'] === 'node/%/view')) {
return '';
}
else {
if (empty($link['localized_options'])) {
$link['localized_options'] = array();
}
return l($link['title'], $link['href'], $link['localized_options']);
}
}
function mytheme_menu_local_task($link, $active = FALSE) {
// Don't return a <li> element if $link is empty
if ($link === '') {
return '';
}
else {
return '<li '. ($active ? 'class="active" ' : '') .'>'. $link ."</li>\n";
}
}
This way, you're relying on the menu router path, not modifying the menu router item, and achieving the result you want with minimal changes to core functionality or theming.
On the module side, you could do something that decouples the Edit's menu entry from the local tasks for the node:
function custom_menu_alter(&$items) {
$items['node/%node/edit']['type'] = MENU_CALLBACK;
}
The edit path is still there, but now it is not associated with the View tab. This includes the edit page itself--no View tab there.
there is a module for that: tab tamer allows to hide or disable tabs and rename them as well.
I use the following in template.php by theme (which is perhaps a little hacky, I feel I should be considering unsetting $tabs instead):
function THEME_NAME_menu_local_tasks() {
return '';
}
Or you could ommit:
if ($tabs) echo $tabs;
from your page.tpl.php...
View and Edit are functional features. They have a reason for being there.
The best way to "remove" them, is to "remove" that functionality alltogether. After all: why remove the interface of a piece of functionality, but not the functionality itself?
Besides, simply not printing the tabs, does not remove the url endpoints. In other words: if you don't print the edit tab, people can still access the edit page.
Again: best is to remove that functionality: The fact that you don't want the edit tab, sounds as if you don't want the edit functionality for certain users.
If so, then just remove that permission for that role. That is all. The tabs will be gone.
If, however, you simply wish to display these tabs differently, Drupal is your friends. As you may have noticed, they are called local tasks and not tabs. That is because the theme decides how to render them: The theme is the thing that decides to show them as tabs.
Simply override the theme_menu_local_tasks() to create your own HTML for the "local-tasks". And in your page-tpl, simply move the $tabs variable around to a place, where you want them.
But again: Don't try to change the behavior of the app, by removing interface-elements. That is not the right thing to do: you should change the behavior, in order to change the behavior :)
For all the people stumbling upon this question while looking for a D7 solution: As stated on https://drupal.stackexchange.com/a/77964/15055 it's hook_menu_local_tasks_alter()
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == 'node/%/view') {
unset($data['tabs'][0]['output'][$key]);
}
}
}
This is not the answer to the question of what the author asked. But somehow it might be useful for others user who facing the similar problem with me. Please let me know if this is not suitable to put in here.
I get the answer from #grayside and modified a bit to hide the view | edit tab from node based on the content type I want.
function MYMODULE_menu_alter(&$items) {
$items['node/%node/view']['access callback'] = 'MYMODULE_disable_node_view';
$items['node/%node/view']['access arguments'] = array(1);
}
function MYMODULE_disable_node_view($node){
if($node->type == 'product'){
return false;
}
}
product is the machine name of my content type, I don't want anywant to access it including root user.
The simplest solution to hide the tabs is to add this class in your theme css
.tabs{ display:none;}
Thanks for the last answer. But be aware of this detail: if you try it as-is it cannot work: literals should be just rounded with " or ', not both of them altogether. So it should be:
/**
* Implements hook_menu_local_tasks_alter() to unset unwanted tabs
*/
function MYMODULE_menu_local_tasks_alter(&$data) {
foreach ($data['tabs'][0]['output'] as $key => $value) {
if ($value['#link']['path'] == "node/%/view") {
unset($data['tabs'][0]['output'][$key]);
}
}
}
Once taken care of it, it works :)
D8 solution: If you want to hide all "local" tabs on certain pages, remember that "Tabs" is listed in the block library: find it in the "Content" region and exclude by content type, page URL or user role.
I'd like to pull out just one section of my navigation menu - a single section of the admin menu structure. I can load the entire navigation menu tree, but I can't see an easy way of pulling out just one segment of it.
Is there an easy way to do this, or do I have to do something hacky?
Have a look at function menu_navigation_links. You pass it a menu name (default = navigation) and a level (default = 0).
Not sure if you'd consider this an easy way, but you could try to grab the whole menu tree via menu_tree_data() or menu_tree_page_data(), find and extract the section you're interested in from the resulting tree structure and render the resulting subtree via menu_tree_output().
EDIT: Stumbled over How to rendering a menu subtree in the meantime - looks like my suggestion could work, but I would definitely not consider this being easy ;)
I found this here which works great for me.
<?php
$menus = menu_tree_page_data(menu_get_active_menu_name()); //get menu tree for active menu
$output='';
foreach($menus as $data) {
if(!empty($data['link']['in_active_trail'])){
$link = theme('menu_item_link', $data['link']);
$extra_class = NULL;
if ($data['below']) {
$output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
}
else {
$output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
}
}
}
return theme('menu_tree', $output);
?>
I'm not sure I entirely understand the situation, but you may want to take a look at Menu Block Split which allows you to split levels of navigation into blocks. Here is an excerpt from its project page:
... split any menu block into two different blocks: a first block with the first level menu entries only and a second block with any second level and sub level menu entries. You can have as many splitted blocks as you need.