I am trying to create a block in Drupal7 that is taking information from the page that the block is displayed on.
Something like this:
Please request more information <a href="/webform/info?line=<taxonomy value>&prod=<content field value>">here</a>.
Where the "taxonomy value" is set as a value on the content type that was created. And the same goes for "content field value".
This will allow me to help pre-populate some fields on the linked webform by passing values through the url.
Due to styling requirements for this block, I figured it would be better to create the block via module code.
I have the initial block code setup, where I'm falling short is I'm not sure how to "pull" in the content type values I need.
According to some searches I've done, "node_load()" keeps popping up, but I've also seen some posts saying that "node_load()" is depreciated.
What is the best/supported way to get the content field value and taxonomy value that I need into my block?

Comments
In the code where you
In the code where you generate the html output for your block, try something like:
var_dump($_GET);and see if anything is displayed in your block.
If I understand your question correctly, you want to tailor the output of your block based on a get variable you are sending to the page where your block exists.
You should be able to grab the variable from the $_GET array, and check against it, then output the code to the block that you want.
The code
Hmmm... I don't know if I explained this very well.
So, I have a content type (products) that will have a block displaying on it. The purpose of this block is to allow users to request more information about the product (current page) they are looking at.
Once a user clicks the "here" link (as described in my original post). The user is going to be directed to a page with a form. The link will pass arguments through the url to pre-populate some of the form values.
I know how to have the form pull the url arguments and use them as I see fit.
I'm stuck with getting the content type field values into the block to help build the url dynamically.
Here is my block code:
/
* @file
* Sets up the "Need Help" Block that appears at the bottom of Product pages
*/
/
* List the block on the block listing page
*/
function needhelp_block_info(){
$blocks['needhelp'] = array(
'info' => t('Need Help'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/
* Get the block
*/
function needhelp_block_view($delta = '') {
$block = array();
if ($delta == 'needhelp'){
$block['subject'] = t('Need Help');
$block['content'] = needhelp_block_contents($delta);
return $block;
}
}
/
* Fill the block with copy and links
*/
function needhelp_block_contents($delta){
if ($delta == 'needhelp'){
//get the value of the taxonomy term that is set on the current page the block is displaying on
$content_tax_value = ''; //need help getting this value from the content type
//get the value of the field_robot_product_id that is set on the current page as a select option that the block is displaying on
$content_field_value = ''; //need help getting this value from the content type
// fill block with copy and dynamic links
// there will be more information and divs to setup here per styling requests - but this is getting my purpose across
$thelink = 'Please request more information <a href="/webform/info?line='.$content_tax_value.'&prod='.$content_field_value.'>">here</a>';
return $thelink;
}
}
Just need a little more
Just need a little more clarification, is this form something you built from scratch (HTML form)? Or are you using the webform module?
webform module
I'm using a webform for the form.
My question isn't at the "webform level"... I know what to do with the webform. My concern is with the content type and block working together.
"I'm stuck with getting the
"I'm stuck with getting the content type field values into the block to help build the url dynamically."
Oh, sorry for not catching that...
node_load is definitely NOT deprecated, and is probably the best way to get the object for the node into your block.
http://api.drupal.org/api/drupal/modules!node!node.module/function/node_load/7
You can also use menu_get_object, which seems silly to me, but others use it.
http://api.drupal.org/api/drupal/includes!menu.inc/function/menu_get_object/7
There is also entity_load, which node_load is a wrapper of.
I would say go with node_load. If you are looking for more people to tell you why, take a minute and read this...
http://drupal.stackexchange.com/questions/5764/which-should-i-use-and-wh...
Hope that helps.
Getting Closer...
Okay, So from some feed back I've received on this, I believe I'm getting closer.
On my Content Type, I have a Select list for users to set the "Product ID" of the Product they are entering on the content type.
So my values are something like this:
100|Product A200|Product B
300|Product c
I'm trying to access the values: 100, 200, or 300. Not the human readable names.
With the following block code, I am able to access the human readable name. How do I access the "machine" value (100, 200, 300)?
/
* List the block on the block listing page
*/
function needhelp_block_info(){
$blocks['needhelp'] = array(
'info' => t('Need Help'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/
* Get the block
*/
function needhelp_block_view($delta = '') {
$block = array();
if ($delta == 'needhelp'){
$block['subject'] = t('Need Help');
$block['content'] = needhelp_block_contents($delta);
//$block['content'] = print_r($field);
return $block;
}
}
/**
* Fill the block with copy and links
*/
function needhelp_block_contents($delta){
if ($delta == 'needhelp'){
$node = menu_get_object('node');
if ($node && $node->type === 'products') {
$content_field_value = field_get_items('node', $node, 'field_product_id');
if ($content_field_value) {
$rendered_field_value = field_view_value('node', $node, 'field_product_id', $content_field_value[0]['value']);
}
$thelink = Please request more information <a href="/webform/info?line='.$content_tax_value.'&prod='.$rendered_field_value.'>">here</a>';
return $thelink;
}
}
}
(I haven't tried to get the value for "$content_tax_value" yet. The output for $rendered_field_value is "Product A".)
Got it!
Ah-ha! I got it.
function needhelp_block_contents($delta){
if ($delta == 'needhelp'){
$node = menu_get_object('node');
if ($node && $node->type === 'products') {
$content_field_value = field_get_items('node', $node, 'field_product_id');
$thelink = Please request more information <a href="/webform/info?prod='.$content_field_value[0]['value'].'>">here</a>';
return $thelink;
}
}
}
It turns out had an extra step in there that was grabbing the human readable value instead of the NID.
I removed the lines of code below and just referenced the $content_field_value[0]['value'].
if ($content_field_value) {$rendered_field_value = field_view_value('node', $node, 'field_product_id', $content_field_value[0]['value']);
}
Thank you everybody for your help :)