Users are set up on my D7 site with various term references as profile fields.
I need to be able to show hide certain blocks based on these term references, is this possible?
I cant see a way to link a block with a term reference.
One way to achieve this is to enable php filter and set
Show block on specific pages -> Pages on which this PHP code returns TRUE (experts only)
in the block settings.
then you can access the user profile field terms of the logged in user like this
global $user;
$user = user_load($user->uid);
$term1 = $user->field_term1[LANGUAGE_NONE][0]['value']; //the value should be the tid of the term
$term2 = $user->field_term2[LANGUAGE_NONE][0]['value'];
then you can return true accordingly to control the visibility of the block.
for example
return $term1 == 15 //this is the tid of the term
The above code will show the block only to users with custom field term with tid = 15
Hope I am not confusing you.
Related
What is possible now:
A user can select multiple terms
Desired behaviour:
A user can select only on term per level. (Only one term on the first level and only one term on the second level)
I use custom taxonomies.
First, you can limit your user with some js that you can add only on admin area using admin_enqueue_scripts
And then some code:
jQuery('.cat-checklist input[type="checkbox"]').on('change', function(event){
var checked = jQuery(this).prop('checked');
// Clear all other inputs on this level:
jQuery(this).closest('ul').find('input[type="checkbox"]').removeAttr('checked');
// Set the value:
jQuery(this).prop('checked', checked)
})
Tested on the quick edit screen but you've got the idea.
From the other side, you can also modify the saved data for example using the save_post hook. You'll be able to get the list of taxonomies that are going to be saved and then limit them.
I have been looking for a plugin in wordpress that allows me to insert a page view counter in a different page: I´ll explain;
In page A I have buttom with a contact form to vote a video (it is a video contest). This contact form sends the voter an email with a link to page B so that they can validate their email this way. Every visit to page B will be counted as a vote.
The thing is that I want to count the number times that page B is accessed to show it in page A (to show the number of votes next to the video).
It seems pretty straightforward but I can´t find an easy way to do this. Do you think you could help me?
Thanks!
Guzmán
If a very small number of people will access the page you can just store a value in WordPress and add to it each time. If you expect a lot of users on the page at once you may need a more complex method.
For a lightly trafficed page, with the risk that some visits might not be counted you could leverage the post meta and simply store a counter variable. If two people viewed the page at the same time they could get the same value for the counter variable and so they would not properly increment the variable.
You could add this to the template for the page in question:
$vcount = get_post_meta($post->ID, "view_counter");
add_post_meta($post->ID, "view_counter", $vcount+1, true);
So two people accessing the page would end up with equal $vcount variables. To display the count you'd just need the post id for the post you saved the meta to: print get_post_meta(<POSTIDHERE>, "view_counter", true);
If you need to have a more robust solution, you'd want to store a unique value for each visitor and then to report the total you would count the values and display it. So you could store the visitor's IP address as a multi-value meta:
add_post_meta($post->ID, "view_counter", $_SERVER['REMOTE_ADDR'], false);
Then to display the count you'd need to count the number of post meta values stored with the view_counter key:
$ips = get_post_meta(<POSTIDHERE>, "view_counter", false);
$views = count($ips);
print "Total views: $views";
This is just a sample solution, you should probably check to see if an IP already exists and consider throttling individual IPs. You could also risk filling your database if you have a small shared server unless you write some additional code to keep this from being abused since it adds data to your database on every page view. Finally this will not work correctly if you have any type of caching enabled.
Depending on the plugin that you are using for your contact form you may also be able to report the number of times that the contact form was submitted by querying the database directly.
For more information see the documentation for add_post_meta and get_post_meta
(disclaimer, code written from memory and documentation, not tested)
In Views, I have an exposed filter that looks at the UID (User ID / Author), but is there a way to limit this to "ONLY" the users who have posted in this content type?
I tried adding a "Content: Author" relationship and hit Apply. I'm not exactly sure why, but it wasn't until this point that I could go back into the relationships and see MORE options, like: "User: Content authored" (which must then be dependent on the first relationship?) so I selected that one too and set it up like so:
Now I was able to go to the exposed filter and select the relationship:
But this didn't work-- the exposed filter continues to show all the registered users.
I also tried putting in a User Reference field (to this content type) and attaching that to a relationship, but it didn't allow anything to show and gave this SQL warning:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'field_data_field_hidden_name.uid' in 'on clause'
How can I limit this author exposed filter to "ONLY" the users who have posted in this content type?
There are a few drupal modules that may help you out. Such as Corresponding node references
However, if you are good with php code, you can create the logic to query the database and return the desired results (which would be the columns of the table). Create a contextual filter, then select a field (any field should work, but best select something in either content type). Then edit it, WHEN THE FILTER VALUE IS NOT AVAILABLE -> Provide default value -> PHP Code
$nid = arg(1);// current node id
$query = "SELECT <desired_field_column_name> FROM {<field_data_table_name>} c
WHERE c.<column_that_is_same_as_nid> = :nid";
$result = db_query($query, array(':nid' =>$nid));
$id = array();
while ($row = $result->fetch())
{
array_push($id, $row->field_curator_target_id);//put each node id that's referenced in array
}
$separated = implode("+", $id); // separate them by + for AND , for OR
return $separated; //eg.32 + 30 would represent nodes that you want.
In Linked theme there is answer about Views Selective Filters (aka "Views Selective Exposed Filters", aka views_filters_selective, aka views_selective_filters).
This module adds filters that have suffix "(selective)" in their names – add one you need instead of filter w/o the suffix.
I have about 6 different groups, eg Team A, Team B etc. I would like to display a block for one of these groups. Was thinking something like checking if the logged in users gid = X then display the block. That way I can setup 6 different blocks all targeting the different groups so that when the user is logged in, it will display the relevant block for them. Does that make sense? How would I implement it?
Depending on your exact setup, it looks like the Context module may help you.
Here's how you can do that.
Create your 6 separate blocks
Download and install the context module
Create a new context at admin/structure/context/add
Fill in the Conditions section based on one of my options below
Fill in the Reactions section, choose to add 'Blocks' and then select the exact block you want to show for the condition selected. You can show more than one, so add any that you want to appear.
Create a separate context for each of your groups (so 6 in total). You can show multiple blocks per each group.
Creating a new context allows you to show certain blocks for only CERTAIN CONTEXTS. Example contexts are showing blocks on only certain pages (via the Path context) or only for users of a certain role (via the User role context) or even on certain node types or on pages that have a certain term attached, etc.
In your case, if you are using the Organic Groups module to implement your user groups, context will integrate with that. That means that when you create your context, there will be an option under the 'Conditions' section to select the Organic Group you want to show certain blocks for. You choose the exact blocks you want to show in the 'Reaction' section.
Let us know if that helps!
After more than a week of research and playing around, I found a little bit of code and modified it below to what I needed.
<?php
global $user;
$uid = $user->uid;
$result = db_query ( "SELECT * FROM {og_membership}
WHERE etid = :uid
and entity_type = 'user'
order by gid DESC", array (':uid' => $uid ) );
foreach ( $result as $row ) {
$gid = $row->gid;
break;
}
?>
<?php if ($gid == "GROUP ID HERE"): ?>
(load block here)
<?php endif; ?>
I have a Content-Type with taxonomy-terms. It's a select-list, so it can have only one taxonomy - at least of that vocabulary. Now I need to build a view that lists all nodes with the same taxonomy.
Thought that this wouldn't be too hard since it sounds pretty basic. Yet I can't get it working. Any ideas? I'm trying to avoid writing a module for this.
While this was technically possible with Views 2 as described in some of the other answers, Views 3 has this integration built in.
You can create an argument for the taxonomy term id and then choose "Provide default argument". This will then give you an option for "Taxonomy Term ID from URL" and "Load default argument from node page..."
This will allow you to take the taxonomy of a page and pass that as an argument to your view block.
Note: Views 3 is currently in Alpha 3 but in my experience is at a relatively stable state and I am using it on production sites. If it has features like the one above that you find useful please use it, test it and submit bugs/patches if you encounter any issues!
This answer works in Views version 2 or higher. First you need to install Views attach ( http://drupal.org/project/views_attach ). Please read about Views attach before proceeding further. Views attach is best explained by this video http://mustardseedmedia.com/podcast/episode37
Now we get to Views attach. Please enable the views attach module before proceeding. Essentially Views attach attaches a view at the end of the node. In our case our view will be a listing of other articles with the same term.
We will essentially need to "pass" the taxonomy term of the node to the view. Let the name of your Vocabulary be called MyVocab.
Steps to make the view.
Lets call the view display_other_articles_with_same_taxonomy.
Add a display of type Node Content (available after enabling Views attach). This is a display just like block and page displays but with special ability of attaching itself to the node.
Make the following settings in the Node Content Display
Node content settings
Node types: [select the content types you are interested in seeing the list of nodes with same taxonomy term]
Build modes: Teaser, Full node
Arguments: token
Show title: No
You should select Use tokens from the node the view is attached to under Arguments. Let the token be [term-id] This is the "ID of top taxonomy term". This is very important!! Essentially you are going to be passing the taxonomy term of the node from the MyVocab (See http://groups.drupal.org/node/11788#comment-38332). If it has the lowest weight, the taxonomy vocabulary will be the first vocabulary in the taxonomy section of your node edit form. Add an argument Taxonomy: Term Id.
Add the fields you are interested in e.g. Node: Title. Make sure the Node: Title is made into a hyperlink by ticking Link this field to its Node
So what this view is going to do is:
Take the taxonomy term from the MyVocab vocabulary in the Node that is currently being viewed
Pass that as argument to the view display_other_articles_with_same_taxonomy
List all the nodes that have the same taxonomy term as the node being displayed.
Thats it!
If you're using Views 3 (currently at alpha3 at the time of writing) and you want a block (right now the articles have same taxonomy term come at the end of node body) you can do it in the following fashion:
Forget about views attach... its not required
Add a block view. It should contain the same argument, fields and filters as the instructions above for the Node Content display.
You need to modify the settings for the argument Taxononomy: Term Id just slightly: Under Action to take if argument is not present: choose [x] Provide Default Argument. Choose [x] Taxonomy Term ID from URL. Now make sure [] Load default argument from term page is unselected and [x] Load default argument from node page, thats good for related taxonomy blocks. Also [x]Limit terms by vocabulary and choose the Series vocabulary.
Make sure you name the block and put it in the appropriate region.
Note: This answer is subset of the answer I provided at How to just show NodeQueue block on included nodes? In that scenario the requirement was that the related articles are explicitly selected and listed in a particular order. Its a little more complex and uses Nodequeues which we don't need here at all.
Use relationships
Node 1 -> Relationship 1 -> Term // This will be relationships->taxonomy->term
Term -> Relationship 2 -> Node 2 // This will be relationships->node->node
Argument NID to filter NODE 1
Fields or node full view on Relationship 2 (you will see select box on field adding form, to determinate what NODE to use)
Views gives you the options to add filters. Click the plus sign in the filters area of the views admin UI, select Taxonomy from the list, check either Term or ID, and fill in the value that you need to filter by.
EDIT (for explicit instructions):
First, add the necessary fields that you want under the fields section that you want to display from the nodes that you are trying to filter such as node title, etc.
Under Arguments, click the plus sign and select Taxonomy
Check Taxonomy: Term and click Add
Click Update
In your preview area test it out by adding an argument and clicking preview. If it doesn't work, your nodes do not have attached taxonomy, your views module is corrupted, or you didn't follow directions.